mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 04:41:07 +00:00
Handle conditional arrays
This commit is contained in:
committed by
joselcvarela
parent
d96011dc19
commit
4422e9e424
@@ -62,8 +62,15 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1)
|
||||
|
||||
@@ -138,6 +138,11 @@ var conditions= [
|
||||
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%\'))'
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user