Browse Source

Merge pull request #2 from joselcvarela/master

Add raw statement for special handling like postgres operators
pull/3/head
Harish K 7 years ago
committed by GitHub
parent
commit
ae0663d314
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      index.js
  2. 17
      test.js

18
index.js

@ -25,6 +25,10 @@ var functionOperatorMap = {
AND_NOTIN: 'andWhereNotIn', AND_NOTIN: 'andWhereNotIn',
AND_ISNULL: 'andWhereNull', AND_ISNULL: 'andWhereNull',
AND_NOTNULL: 'andWhereNotNull', AND_NOTNULL: 'andWhereNotNull',
/* ---- */
RAW: 'whereRaw',
OR_RAW: 'orWhereRaw',
AND_RAW: 'whereRaw',
}; };
var aliases = { var aliases = {
@ -58,11 +62,19 @@ function addCondition (q, field, val) {
// SQL operator // SQL operator
val = [ val[0], field ].concat(val.slice(1)); val = [ val[0], field ].concat(val.slice(1));
} else { } 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);
} }

17
test.js

@ -72,6 +72,11 @@ var conditions= [
input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 }, input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 },
output: 'select * where ("f1" not in (50, 60) and "f2" = 20 and "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---------- */ /* -----------Tests for or conditions---------- */
{ {
name: 'handle simple or condition', name: 'handle simple or condition',
@ -125,9 +130,19 @@ var conditions= [
}, },
{ {
name: 'handle simple or condition with nin statement :MongoQuery', 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))' 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%\'))'
}
]; ];

Loading…
Cancel
Save