diff --git a/lib/database.js b/lib/database.js index 5fdf0c7..050452b 100644 --- a/lib/database.js +++ b/lib/database.js @@ -4,9 +4,13 @@ const { Pool } = require('pg'); const OPERATORS = ['>=', '<=', '<>', '>', '<']; +const TRANSFORMS = { + '@@': (value) => `to_tsquery(${value})`, +}; + const whereValue = (value) => { if (typeof value === 'string') { - for (const op of OPERATORS) { + for (const op of OPERATORS.concat(Object.keys(TRANSFORMS))) { const len = op.length; if (value.startsWith(op)) { return [op, value.substring(len)]; @@ -39,7 +43,11 @@ const buildWhere = (conditions, firstArgIndex = 1) => { for (const key of keys) { const [operator, value] = whereValue(where[key]); if (value !== undefined) { - conjunction.push(`"${key}" ${operator} $${i++}`); + conjunction.push( + `"${key}" ${operator} ${ + operator in TRANSFORMS ? TRANSFORMS[operator]('$' + i++) : '$' + i++ + }`, + ); args.push(value); } } diff --git a/test/sql.js b/test/sql.js index 13c0f46..89298d3 100644 --- a/test/sql.js +++ b/test/sql.js @@ -62,6 +62,13 @@ const metadomain = require('metadomain'); test.strictEqual(name, 'Beijing'); const res4 = await db.select('City', { cityId: '3', name: undefined }); test.strictEqual(res4[0], { cityId: '3', name: 'Kiev', countryId: '1' }); + const res5 = await db.select('Country', { name: '@@Soviet | China' }); + test.strictEqual(res5.length, 2); + const res6 = await db.select('Country', { name: '@@People & China' }); + test.strictEqual(res6[0], { + countryId: '2', + name: "People's Republic of China", + }); test.end(); });