Browse Source

Support '$or' & '$and' based grouping for queries

pull/3/head
Harish.K 6 years ago
parent
commit
53058d58bf
  1. 6
      index.js
  2. 13
      test.js

6
index.js

@ -38,6 +38,12 @@ var aliases = {
function addCondition (q, field, val) {
if( field === '$or' ){
return q.orWhere( getWhereCondition( val ))
}
if( field === '$and' ){
return q.where( getWhereCondition( val ))
}
if( val.constructor.name === 'Object' ){
delete val.$options;
val = Object.keys(val).map( function(key){

13
test.js

@ -78,6 +78,12 @@ var conditions= [
output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)'
},
/* -----------Tests for or conditions---------- */
{
name: 'handle $and grouping',
input:{ f1: 10, f2: 20, f3: 30, $and: [ { f4: 55 }, { f5: 66} ] },
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 and (("f4" = 55) or ("f5" = 66)))'
},
/* -----------Tests for or conditoins---------- */
{
name: 'handle simple or condition',
input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ],
@ -142,7 +148,12 @@ var conditions= [
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%\'))'
}
},
{
name: 'handle $or grouping',
input:{ f1: 10, f2: 20, f3: 30, $or: [ { f4: 55 }, { f5: 66} ] },
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 or (("f4" = 55) or ("f5" = 66)))'
},
];

Loading…
Cancel
Save