diff --git a/index.js b/index.js index 7dee2d5..d19f546 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,10 @@ var functionOperatorMap = { AND_NOTIN: 'andWhereNotIn', AND_ISNULL: 'andWhereNull', AND_NOTNULL: 'andWhereNotNull', + /* ---- */ + RAW: 'whereRaw', + OR_RAW: 'orWhereRaw', + AND_RAW: 'whereRaw', }; var aliases = { @@ -58,11 +62,19 @@ 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); + } } } - return q[functionOperatorMap[val[0]]].apply(q, val.slice(1)); + var args = val[0].includes('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..2e9cfd7 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,19 @@ 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))' + }, + { + 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%\'))' + } ];