From 0f93490366de7635905165e0624703081ddc62f3 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Fri, 16 Nov 2018 10:17:06 +0000 Subject: [PATCH 1/3] 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))' + }, ]; From d96011dc19ffbcc7d562b003856e34fd3120f4c8 Mon Sep 17 00:00:00 2001 From: joselcvarela Date: Wed, 28 Nov 2018 08:44:46 +0000 Subject: [PATCH 2/3] Add or_raw statement --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8f38ed9..029fc1d 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,9 @@ var functionOperatorMap = { AND_ISNULL: 'andWhereNull', AND_NOTNULL: 'andWhereNotNull', /* ---- */ - RAW: 'whereRaw' + RAW: 'whereRaw', + OR_RAW: 'orWhereRaw', + AND_RAW: 'whereRaw', }; var aliases = { @@ -64,7 +66,7 @@ function addCondition (q, field, val) { val = [ 'AND', field ].concat(val); } } - var args = val[0] === 'RAW' ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) + var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) return q[functionOperatorMap[val[0]]].apply(q, args); } From 4422e9e4242188fdaf9e5d5916937a22fbd02591 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Fri, 30 Nov 2018 09:34:27 +0000 Subject: [PATCH 3/3] Handle conditional arrays --- index.js | 11 +++++++++-- test.js | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 029fc1d..d19f546 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,15 @@ function addCondition (q, field, val) { // SQL operator val = [ val[0], field ].concat(val.slice(1)); } else { - // other cases like ( '>', '10' ) Greater than 10 - val = [ 'AND', field ].concat(val); + // Cases when we have something like 'OR_ILIKE' or 'AND_@>' + var operators = /(\w+)_(\w+)/.exec(val[0]) + var operatorsExist = operators && operators.constructor === Array && operators.length >= 3 + if (operatorsExist) { + val = [operators[1], field].concat([operators[2]], val.slice(1)); + } else { + // other cases like ( '>', '10' ) Greater than 10 + val = [ 'AND', field ].concat(val); + } } } var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) diff --git a/test.js b/test.js index 2d33f3d..2e9cfd7 100644 --- a/test.js +++ b/test.js @@ -138,6 +138,11 @@ var conditions= [ input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ], output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))' }, + { + name: 'handle simple or condition with conditional array', + input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] }, + output: 'select * where (("f1" ILIKE \'awesome\' or "f1" ILIKE \'%super%\'))' + } ];