# @noim/pg-tsquery

> Parse user input into valid text search queries

Latest version **8.2.0** (published 2020-07-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install @noim/pg-tsquery
pnpm add @noim/pg-tsquery
yarn add @noim/pg-tsquery
bun add @noim/pg-tsquery
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.2.0 |
| Published | 2020-07-22 |
| First published | 2020-07-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 60 |
| Author | caub |
| Maintainers | thenoim |
| Keywords | pg, postgres, PostgreSQL, tsquery, text, search, parser |

## Links

- npm: https://www.npmjs.com/package/@noim/pg-tsquery
- Repository: https://github.com/caub/pg-tsquery
- Homepage: https://github.com/caub/pg-tsquery#readme
- Issues: https://github.com/caub/pg-tsquery/issues
- npm.io page: https://npm.io/package/@noim/pg-tsquery

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 8.2.0 (latest) — 2020-07-22

## README

# Text-Search parser for PostgreSQL

[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![coverage status][codecov-image]][codecov-url]

### 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](index.js#L52-L61)

### usage
```js
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:
parse.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:*` |


### [Demo](https://caub.github.io/pg-tsquery)

[npm-image]: https://img.shields.io/npm/v/pg-tsquery.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/pg-tsquery
[travis-image]: https://img.shields.io/travis/caub/pg-tsquery.svg?style=flat-square
[travis-url]: https://travis-ci.org/caub/pg-tsquery
[codecov-image]: https://img.shields.io/codecov/c/github/caub/pg-tsquery.svg?style=flat-square
[codecov-url]: https://codecov.io/gh/caub/pg-tsquery

---
_Source: https://npm.io/package/@noim/pg-tsquery · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
