mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13427005fe | ||
|
|
68bdbf750c | ||
|
|
498ac917d4 |
@@ -1,24 +1,11 @@
|
|||||||
|
|
||||||
|
|
||||||
var functionOperators = [
|
|
||||||
'BETWEEN',
|
|
||||||
'IN',
|
|
||||||
/* ---- */
|
|
||||||
'OR',
|
|
||||||
'AND',
|
|
||||||
'EQ',
|
|
||||||
'=',
|
|
||||||
/* ---- */
|
|
||||||
'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',
|
||||||
@@ -27,14 +14,31 @@ var functionOperatorMap = {
|
|||||||
/* ---- */
|
/* ---- */
|
||||||
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',
|
||||||
|
};
|
||||||
|
|
||||||
|
var aliases = {
|
||||||
|
REGEX: 'REGEXP'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function addCondition (q, field, val) {
|
function addCondition (q, field, val) {
|
||||||
|
if( val.constructor.name === 'Object' ){
|
||||||
|
delete val.$options;
|
||||||
|
val = Object.keys(val).map( function(key){
|
||||||
|
return [ key.slice(1).toUpperCase(), val[key] ];
|
||||||
|
});
|
||||||
|
if( val.length === 1){
|
||||||
|
val = val[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
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));
|
||||||
@@ -42,13 +46,18 @@ function addCondition (q, field, val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(val)) {
|
if (!Array.isArray(val)) {
|
||||||
|
// Simple string or number value
|
||||||
val = ['AND', field, val ];
|
val = ['AND', field, val ];
|
||||||
} else if (functionOperators.indexOf(val[0]) !== -1) {
|
} else {
|
||||||
|
val[0] = aliases[ val[0] ] || val[0];
|
||||||
|
if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
|
||||||
|
// SQL operator
|
||||||
val = [ val[0], field ].concat(val.slice(1));
|
val = [ val[0], field ].concat(val.slice(1));
|
||||||
} else {
|
} else {
|
||||||
|
// other cases like ( '>', '10' ) Greater than 10
|
||||||
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
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "knex-json-query",
|
"name": "knex-json-query",
|
||||||
"version": "0.0.2",
|
"version": "0.0.5",
|
||||||
"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": {
|
||||||
|
|||||||
@@ -17,6 +17,16 @@ var conditions= [
|
|||||||
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
|
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
|
||||||
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
|
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'handle regex query: MongoQuery',
|
||||||
|
input:{"firstName":{ $regex: "H*" }},
|
||||||
|
output: 'select * where ("firstName" REGEXP \'H*\')'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'handle regex query and should omit $options: MongoQuery',
|
||||||
|
input:{"firstName":{ $regex: "H*", $options: 'i' }},
|
||||||
|
output: 'select * where ("firstName" REGEXP \'H*\')'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'handle simple and conditions',
|
name: 'handle simple and conditions',
|
||||||
input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 },
|
input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 },
|
||||||
@@ -63,21 +73,41 @@ var conditions= [
|
|||||||
input:{ f1: [ [ 'LIKE', 20 ], [ 'OR', 21 ], [ 'OR_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
input:{ f1: [ [ 'LIKE', 20 ], [ 'OR', 21 ], [ 'OR_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
||||||
output: 'select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
output: 'select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'handle multiple conditions in one field: MongoQuery',
|
||||||
|
input:{ f1: { $like: 20, $or: 21, $or_between: [ 40, 45 ] }, f2: 20, f3: 30 },
|
||||||
|
output: 'select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'handle simple or condition with like statement',
|
name: 'handle simple or condition with like statement',
|
||||||
input:[ { f1: ['LIKE',10] }, { f2: 20 }, { f3: 30 } ],
|
input:[ { f1: ['LIKE',10] }, { f2: 20 }, { f3: 30 } ],
|
||||||
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'handle simple or condition with like statement: MongoQuery',
|
||||||
|
input:[ { f1: { $like: 10 } }, { f2: 20 }, { f3: 30 } ],
|
||||||
|
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'handle simple or condition with between statement',
|
name: 'handle simple or condition with between statement',
|
||||||
input:[ { f1: ['BETWEEN',[50, 60] ] }, { f2: 20 }, { f3: 30 } ],
|
input:[ { f1: ['BETWEEN',[50, 60] ] }, { f2: 20 }, { f3: 30 } ],
|
||||||
output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))'
|
output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'handle simple or condition with between statement :MongoQuery',
|
||||||
|
input:[ { f1: { $between:[50, 60] } }, { f2: 20 }, { f3: 30 } ],
|
||||||
|
output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'handle simple or condition with in statement',
|
name: 'handle simple or condition with in statement',
|
||||||
input:[ { f1: ['IN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ],
|
input:[ { f1: ['IN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ],
|
||||||
output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))'
|
output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'handle simple or condition with in statement :MongoQuery',
|
||||||
|
input:[ { f1: {$in:[ 50, 60 ]} }, { f2: 20 }, { f3: 30 } ],
|
||||||
|
output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))'
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user