From 0f93490366de7635905165e0624703081ddc62f3 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Fri, 16 Nov 2018 10:17:06 +0000 Subject: [PATCH] Add raw statement for special handling like postgres operators --- index.js | 5 ++++- test.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7dee2d5..8f38ed9 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,8 @@ var functionOperatorMap = { AND_NOTIN: 'andWhereNotIn', AND_ISNULL: 'andWhereNull', AND_NOTNULL: 'andWhereNotNull', + /* ---- */ + RAW: 'whereRaw' }; var aliases = { @@ -62,7 +64,8 @@ function addCondition (q, field, val) { val = [ 'AND', field ].concat(val); } } - return q[functionOperatorMap[val[0]]].apply(q, val.slice(1)); + var args = val[0] === 'RAW' ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) + return q[functionOperatorMap[val[0]]].apply(q, args); } diff --git a/test.js b/test.js index 730bb04..2d33f3d 100644 --- a/test.js +++ b/test.js @@ -72,6 +72,11 @@ var conditions= [ input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 }, output: 'select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)' }, + { + name: 'handle simple and condition with raw statement', + input: { f1: { $raw: '@> ANY(ARRAY(1,2,3))' }, f2: 20 }, + output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)' + }, /* -----------Tests for or conditions---------- */ { name: 'handle simple or condition', @@ -125,9 +130,14 @@ var conditions= [ }, { name: 'handle simple or condition with nin statement :MongoQuery', - input:[ { f1: {$nin:[ 50, 60 ]} }, { f2: 20 }, { f3: 30 } ], + input:[ { f1: { $nin: [ 50, 60 ] } }, { f2: 20 }, { f3: 30 } ], output: 'select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))' }, + { + name: 'handle simple or condition with raw statement', + input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ], + output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))' + }, ];