mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Support '$or' & '$and' based grouping for queries
This commit is contained in:
@@ -38,6 +38,12 @@ var aliases = {
|
|||||||
|
|
||||||
|
|
||||||
function addCondition (q, field, val) {
|
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' ){
|
if( val.constructor.name === 'Object' ){
|
||||||
delete val.$options;
|
delete val.$options;
|
||||||
val = Object.keys(val).map( function(key){
|
val = Object.keys(val).map( function(key){
|
||||||
|
|||||||
@@ -78,6 +78,12 @@ var conditions= [
|
|||||||
output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)'
|
output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)'
|
||||||
},
|
},
|
||||||
/* -----------Tests for or conditions---------- */
|
/* -----------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',
|
name: 'handle simple or condition',
|
||||||
input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ],
|
input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ],
|
||||||
@@ -142,7 +148,12 @@ var conditions= [
|
|||||||
name: 'handle simple or condition with conditional array',
|
name: 'handle simple or condition with conditional array',
|
||||||
input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] },
|
input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] },
|
||||||
output: 'select * where (("f1" ILIKE \'awesome\' or "f1" 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)))'
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user