mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Fix. Do not mutate the input raw Query
This commit is contained in:
@@ -6,6 +6,8 @@ var functionOperators = [
|
||||
/* ---- */
|
||||
'OR',
|
||||
'AND',
|
||||
'EQ',
|
||||
'=',
|
||||
/* ---- */
|
||||
'OR_BETWEEN',
|
||||
'OR_IN',
|
||||
@@ -20,6 +22,8 @@ var functionOperatorMap = {
|
||||
/* ---- */
|
||||
OR: 'orWhere',
|
||||
AND: 'where',
|
||||
EQ: 'where',
|
||||
'=': 'where',
|
||||
/* ---- */
|
||||
OR_BETWEEN: 'orWhereBetween',
|
||||
OR_IN: 'orWhereIn',
|
||||
@@ -33,19 +37,19 @@ var functionOperatorMap = {
|
||||
function addCondition (q, field, val) {
|
||||
if (Array.isArray(val[0])) {
|
||||
return q.where(function () {
|
||||
return val.forEach(addCondition.bind(null, this, field))
|
||||
})
|
||||
return val.forEach(addCondition.bind(null, this, field));
|
||||
});
|
||||
}
|
||||
|
||||
if (!Array.isArray(val)) {
|
||||
val = ['AND', field, val ]
|
||||
val = ['AND', field, val ];
|
||||
} else if (functionOperators.indexOf(val[0]) !== -1) {
|
||||
val.splice(1, 0, field)
|
||||
val = [ val[0], field ].concat(val.slice(1));
|
||||
} else {
|
||||
val = [ 'AND', field ].concat(val)
|
||||
val = [ 'AND', field ].concat(val);
|
||||
}
|
||||
|
||||
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1))
|
||||
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,16 @@ var conditions= [
|
||||
input:{ f1: 10, f2: 20, f3: 30 },
|
||||
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle simple and conditions',
|
||||
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
|
||||
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple and conditions',
|
||||
input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 },
|
||||
output: 'select * where (("f1" = \'A\' and "f1" LIKE \'B\') and "f2" = 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle basic operators :: greater-than less-than',
|
||||
input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 },
|
||||
|
||||
Reference in New Issue
Block a user