Burane / pg-tsquery

:mag: Parse user input into a valid PostgreSQL tsquery

Home Page:https://caub.github.io/pg-tsquery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Text-Search parser for PostgreSQL

npm version build status coverage status

Why?

Using pg's to_tsquery directly with user input can throw errors. plainto_tsquery sanitizes the user input, but it's very limited (it just puts an and between words), websearch_to_tsquery extends this behavior a little further only between double-quotes, with followedBy operator and negations.

This module allows customizable text-search operators: and, or, followedBy, not, prefix, parentheses, quoted text (same behavior than websearch_to_tsquery).

See the options defaults values

usage

const tsquery = require('pg-tsquery')(/* options can be passed to override the defaults */);

pool.query('SELECT * FROM tabby WHERE to_tsvector(col) @@ to_tsquery($1)', [tsquery(str)]);

// or get a reusable instance
const {Tsquery} = require('pg-tsquery');

const parser = new Tsquery(/* options can be passed to override the defaults */);

// then one of those equivalent ways to process your input:
parser.parseAndStringify(str);
`${parser.parse(str) || ''}`;
inputs output
foo bar foo&bar
foo -bar, foo !bar, foo + !bar foo&!bar
foo bar,bip, foo+bar | bip foo&bar|bip
foo (bar,bip), foo+(bar|bip) foo&(bar|bip)
foo>bar>bip foo<->bar<->bip
foo*,bar* bana:* foo:*|bar:*&bana:*

About

:mag: Parse user input into a valid PostgreSQL tsquery

https://caub.github.io/pg-tsquery


Languages

Language:JavaScript 87.9%Language:HTML 12.1%