/pg-tsquery

Parse user input to a valid PostgreSQL tsquery

Primary LanguageJavaScript

pg text-search sanitizer Build Status

Like plainto_tsquery operator but allows more syntax (OR, NOT) without letting pg throws

const tsquery = require('pg-tsquery');

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

Examples of inputs

  • foo bar is foo&bar
  • foo -bar is like foo !bar, foo + !bar and foo&!bar
  • foo bar,bip is like foo+bar | bip and foo&bar|bip
  • foo (bar,bip) is like foo+(bar|bip) and foo&(bar|bip)

Notes:

  • <: are ignored
  • it's safe to add :* at the end of the result of tsquery, if it's not empty and not ending with ), for substring matching