Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 66ddbc5

Browse files
committed
#48 Full-Text Search Filters
1 parent 499a0c8 commit 66ddbc5

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/Builder.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ const advancedFilters = [
236236
'ilike',
237237
'is',
238238
'in',
239+
'fts',
240+
'plfts',
241+
'phfts',
242+
'wfts',
239243
'cs',
240244
'cd',
241245
'ova',

src/Request.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ class Request extends SuperAgent {
9090
}
9191
}
9292

93+
if (
94+
['fts', 'plfts', 'phfts', 'wfts'].includes(operator) &&
95+
typeof criteria.queryText == 'undefined'
96+
) {
97+
return {
98+
body: null,
99+
status: 400,
100+
statusCode: 400,
101+
statusText: `.${operator}() can only be invoked with a criteria that is an Object with key queryText.`,
102+
}
103+
}
104+
93105
let newQuery = Filters[`_${operator.toLowerCase()}`](columnName, criteria)
94106
return this.query(newQuery)
95107
}
@@ -298,6 +310,10 @@ const filters = [
298310
'ilike',
299311
'is',
300312
'in',
313+
'fts',
314+
'plfts',
315+
'phfts',
316+
'wfts',
301317
'cs',
302318
'cd',
303319
'ova',

src/utils/Filters.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,98 @@ export function _neq(columnName, filterValue) {
199199
return `${columnName}=neq.${filterValue}`
200200
}
201201

202+
/**
203+
* Finds all rows whose tsvector value on the stated columnName matches to_tsquery(queryText).
204+
* @param {string} columnName Name of the database column
205+
* @param { object } filterObject query text and optionally config to base the match on
206+
* @name fts
207+
* @function
208+
* @returns {string}
209+
*
210+
* @example
211+
* _fts('phrase', {queryText: 'The Fat Cats'})
212+
* //=>
213+
* 'phrase=fts.The Fat Cats'
214+
*
215+
* @example
216+
* _fts('phrase', {queryText: 'The Fat Cats', config: 'english'})
217+
* //=>
218+
* 'phrase=fts(english).The Fat Cats'
219+
*/
220+
export function _fts(columnName, filterObject) {
221+
if(typeof filterObject.config == 'undefined') return `${columnName}=fts.${filterObject.queryText}`
222+
return `${columnName}=fts(${filterObject.config}).${filterObject.queryText}`
223+
}
224+
225+
/**
226+
* Finds all rows whose tsvector value on the stated columnName matches plainto_tsquery(queryText).
227+
* @param {string} columnName Name of the database column
228+
* @param { object } filterObject query text and optionally config to base the match on
229+
* @name plfts
230+
* @function
231+
* @returns {string}
232+
*
233+
* @example
234+
* _plfts('phrase', {queryText: 'The Fat Cats'})
235+
* //=>
236+
* 'phrase=plfts.The Fat Cats'
237+
*
238+
* @example
239+
* _plfts('phrase', {queryText: 'The Fat Cats', config: 'english'})
240+
* //=>
241+
* 'phrase=plfts(english).The Fat Cats'
242+
*/
243+
export function _plfts(columnName, filterObject) {
244+
if(typeof filterObject.config == 'undefined') return `${columnName}=plfts.${filterObject.queryText}`
245+
return `${columnName}=plfts(${filterObject.config}).${filterObject.queryText}`
246+
}
247+
248+
/**
249+
* Finds all rows whose tsvector value on the stated columnName matches phraseto_tsquery(queryText).
250+
* @param {string} columnName Name of the database column
251+
* @param { object } filterObject query text and optionally config to base the match on
252+
* @name phfts
253+
* @function
254+
* @returns {string}
255+
*
256+
* @example
257+
* _phfts('phrase', {queryText: 'The Fat Cats'})
258+
* //=>
259+
* 'phrase=phfts.The Fat Cats'
260+
*
261+
* @example
262+
* _phfts('phrase', {queryText: 'The Fat Cats', config: 'english'})
263+
* //=>
264+
* 'phrase=phfts(english).The Fat Cats'
265+
*/
266+
export function _phfts(columnName, filterObject) {
267+
if(typeof filterObject.config == 'undefined') return `${columnName}=phfts.${filterObject.queryText}`
268+
return `${columnName}=phfts(${filterObject.config}).${filterObject.queryText}`
269+
}
270+
271+
/**
272+
* Finds all rows whose tsvector value on the stated columnName matches websearch_to_tsquery(queryText).
273+
* @param {string} columnName Name of the database column
274+
* @param { object } filterObject query text and optionally config to base the match on
275+
* @name wfts
276+
* @function
277+
* @returns {string}
278+
*
279+
* @example
280+
* _wfts('phrase', {queryText: 'The Fat Cats'})
281+
* //=>
282+
* 'phrase=wfts.The Fat Cats'
283+
*
284+
* @example
285+
* _wfts('phrase', {queryText: 'The Fat Cats', config: 'english'})
286+
* //=>
287+
* 'phrase=wfts(english).The Fat Cats'
288+
*/
289+
export function _wfts(columnName, filterObject) {
290+
if(typeof filterObject.config == 'undefined') return `${columnName}=wfts.${filterObject.queryText}`
291+
return `${columnName}=wfts(${filterObject.config}).${filterObject.queryText}`
292+
}
293+
202294
/**
203295
* Finds all rows whose json || array || range value on the stated columnName contains the values specified in the filterObject.
204296
* @param {string} columnName Name of the database column

0 commit comments

Comments
 (0)