10 Commits
Author SHA1 Message Date
harish2704 13427005fe V0.0.5
Added support for mongo like query. Fully backward compatible
2017-03-29 13:03:15 +05:30
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
harish2704 48c8b8d059 Version bump 2017-02-09 14:59:37 +05:30
harish2704 ff5b938f22 Fixed the query structure to integrate multiple "and-condtions" and "or conditions" for a single field 2017-02-09 14:58:02 +05:30
harish2704 aec35e5daf Added Ci-status icon 2017-02-08 17:03:19 +05:30
harish2704 89e522be15 Fix traviz nodejs version 2017-02-08 17:00:06 +05:30
harish2704 991999d767 Fix test command 2017-02-08 16:57:59 +05:30
harish2704 2f16ae789f Added files for CI 2017-02-08 16:54:03 +05:30
5 changed files with 122 additions and 25 deletions
+3
View File
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "4.4"
+1
View File
@@ -1,2 +1,3 @@
[![Build Status](https://travis-ci.org/harish2704/knex-json-query.svg?branch=master)](https://travis-ci.org/harish2704/knex-json-query)
# knex-json-query # knex-json-query
A high-level utility which will will generate Knex query from a single JSON object. A high-level utility which will will generate Knex query from a single JSON object.
+50 -19
View File
@@ -1,36 +1,67 @@
var simpleOperators = [
'LIKE',
'>',
'<',
];
var functionOperators = [
'BETWEEN',
'IN'
];
var functionOperatorMap = { var functionOperatorMap = {
BETWEEN: 'whereBetween', BETWEEN: 'whereBetween',
IN: 'whereIn' IN: 'whereIn',
ISNULL: 'whereNull',
NOTNULL: 'whereNotNull',
/* ---- */
OR: 'orWhere',
AND: 'where',
EQ: 'where',
'=': 'where',
/* ---- */
OR_BETWEEN: 'orWhereBetween',
OR_IN: 'orWhereIn',
OR_ISNULL: 'orWhereNull',
IR_NOTNULL: 'orWhereNotNull',
/* ---- */
AND_BETWEEN: 'andWhereBetween',
AND_IN: 'andWhereIn',
AND_ISNULL: 'andWhereNull',
AND_NOTNULL: 'andWhereNotNull',
};
var aliases = {
REGEX: 'REGEXP'
}; };
function addCondition( q, field, val ){ function addCondition (q, field, val) {
var conditionFn, conditionName; if( val.constructor.name === 'Object' ){
if( !Array.isArray(val)){ delete val.$options;
return q.where( field, val ); val = Object.keys(val).map( function(key){
return [ key.slice(1).toUpperCase(), val[key] ];
});
if( val.length === 1){
val = val[0];
}
} }
conditionName = val[0]; if (Array.isArray(val[0])) {
if( simpleOperators.indexOf( conditionName ) !== -1 ){ return q.where(function () {
return q.where.apply(q, [field].concat(val) ); return val.forEach(addCondition.bind(null, this, field));
});
} }
if( functionOperators.indexOf(conditionName) !== -1 ){
return q[functionOperatorMap[conditionName] ]( field, val.slice(1) ); if (!Array.isArray(val)) {
// Simple string or number value
val = ['AND', field, val ];
} else {
val[0] = aliases[ val[0] ] || val[0];
if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
// SQL operator
val = [ val[0], field ].concat(val.slice(1));
} else {
// other cases like ( '>', '10' ) Greater than 10
val = [ 'AND', field ].concat(val);
}
} }
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1));
} }
function getWhereCondition( cond ){ function getWhereCondition( cond ){
if( Array.isArray(cond) ){ if( Array.isArray(cond) ){
return function(){ return function(){
+4 -3
View File
@@ -1,10 +1,10 @@
{ {
"name": "knex-json-query", "name": "knex-json-query",
"version": "0.0.1", "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": {
"test": "mocha -R spec" "test": "node test"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -22,6 +22,7 @@
}, },
"homepage": "https://github.com/harish2704/knex-json-query#readme", "homepage": "https://github.com/harish2704/knex-json-query#readme",
"devDependencies": { "devDependencies": {
"knex": "^0.12.6" "knex": "^0.12.6",
"simple-mocha": "0.0.9"
} }
} }
+64 -3
View File
@@ -2,6 +2,7 @@
var assert = require('assert'); var assert = require('assert');
var knex = require('knex')({}); var knex = require('knex')({});
var knexJsonQuery = require('./'); var knexJsonQuery = require('./');
require('simple-mocha');
var conditions= [ var conditions= [
@@ -11,14 +12,49 @@ 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 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',
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 },
output: 'select * where ("f1" > 10 and "f2" < 20 and "f3" = 30)'
},
{
name: 'handle basic operators :: in',
input:{ f1: ['IN', [10,50]], f2: [ '<', 20 ], f3: 30 },
output: 'select * where ("f1" in (10, 50) and "f2" < 20 and "f3" = 30)'
},
{
name: 'handle multiple conditions in one field',
input:{ f1: [ [ 'LIKE', 20 ], 21, [ 'AND_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
output: 'select * where (("f1" LIKE 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
},
{ {
name: 'handle simple and condition with like statement', name: 'handle simple and condition with like statement',
input:{ f1: ['LIKE', 20], f2: 20, f3: 30 }, input:{ f1: [ 'LIKE', 20 ], f2: 20, f3: 30 },
output: 'select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)' output: 'select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)'
}, },
{ {
name: 'handle simple and condition with between statement', name: 'handle simple and 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 and "f2" = 20 and "f3" = 30)' output: 'select * where ("f1" between 50 and 60 and "f2" = 20 and "f3" = 30)'
}, },
{ {
@@ -32,14 +68,34 @@ var conditions= [
input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ], input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ],
output: 'select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))' output: 'select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))'
}, },
{
name: 'handle multiple conditions in one field',
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 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))'
},
{
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))' output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))'
}, },
{ {
@@ -47,6 +103,11 @@ var conditions= [
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))'
},
]; ];