JSPM

  • Created
  • Published
  • Downloads 29910
  • Score
    100M100P100Q151849F
  • License ISC

Parse user input into valid text search queries

Package Exports

  • pg-tsquery

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pg-tsquery) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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), similarly websearch_to_tsquery does this, except between quotes.

This module allows customizable text-search operators (and, or, followedBy, not, prefix, parentheses, quoted text), while also preserving quoted text just like websearch_to_tsquery.

See the full options and defaults

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)])
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:*

Demo

Supports Nodejs>=10. For lower versions, you might need if (!String.prototype.trimStart) String.prototype.trimStart = String.prototype.trim;