3 Commits
Author SHA1 Message Date
harish2704 68bdbf750c Added support for isnull and notnull query 2017-02-16 18:00:04 +05:30
harish2704 498ac917d4 Version 2017-02-09 16:13:39 +05:30
harish2704 e850edcdda Fix. Do not mutate the input raw Query 2017-02-09 16:11:32 +05:30
3 changed files with 27 additions and 22 deletions
+16 -21
View File
@@ -1,31 +1,26 @@
var functionOperators = [
'BETWEEN',
'IN',
/* ---- */
'OR',
'AND',
/* ---- */
'OR_BETWEEN',
'OR_IN',
/* ---- */
'AND_BETWEEN',
'AND_IN'
];
var functionOperatorMap = { var functionOperatorMap = {
BETWEEN: 'whereBetween', BETWEEN: 'whereBetween',
IN: 'whereIn', IN: 'whereIn',
ISNULL: 'whereNull',
NOTNULL: 'whereNotNull',
/* ---- */ /* ---- */
OR: 'orWhere', OR: 'orWhere',
AND: 'where', AND: 'where',
EQ: 'where',
'=': 'where',
/* ---- */ /* ---- */
OR_BETWEEN: 'orWhereBetween', OR_BETWEEN: 'orWhereBetween',
OR_IN: 'orWhereIn', OR_IN: 'orWhereIn',
OR_ISNULL: 'orWhereNull',
IR_NOTNULL: 'orWhereNotNull',
/* ---- */ /* ---- */
AND_BETWEEN: 'andWhereBetween', AND_BETWEEN: 'andWhereBetween',
AND_IN: 'andWhereIn' AND_IN: 'andWhereIn',
AND_ISNULL: 'andWhereNull',
AND_NOTNULL: 'andWhereNotNull',
}; };
@@ -33,19 +28,19 @@ var functionOperatorMap = {
function addCondition (q, field, val) { function addCondition (q, field, val) {
if (Array.isArray(val[0])) { if (Array.isArray(val[0])) {
return q.where(function () { return q.where(function () {
return val.forEach(addCondition.bind(null, this, field)) return val.forEach(addCondition.bind(null, this, field));
}) });
} }
if (!Array.isArray(val)) { if (!Array.isArray(val)) {
val = ['AND', field, val ] val = ['AND', field, val ];
} else if (functionOperators.indexOf(val[0]) !== -1) { } else if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
val.splice(1, 0, field) val = [ val[0], field ].concat(val.slice(1));
} else { } 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));
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "knex-json-query", "name": "knex-json-query",
"version": "0.0.2", "version": "0.0.4",
"description": "A high-level utility which will will generate Knex query from a single JSON object.", "description": "A high-level utility which will will generate Knex query from a single JSON object.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
+10
View File
@@ -12,6 +12,16 @@ var conditions= [
input:{ f1: 10, f2: 20, f3: 30 }, input:{ f1: 10, f2: 20, f3: 30 },
output: 'select * where ("f1" = 10 and "f2" = 20 and "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', name: 'handle basic operators :: greater-than less-than',
input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 }, input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 },