From 53058d58bffcc5b73c3a89a4d905099b624f3605 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sun, 23 Feb 2020 21:50:33 +0530 Subject: [PATCH] Support '$or' & '$and' based grouping for queries --- index.js | 6 ++++++ test.js | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d19f546..3931172 100644 --- a/index.js +++ b/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){ diff --git a/test.js b/test.js index 2e9cfd7..2aecd4f 100644 --- a/test.js +++ b/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)))' + }, ];