9 Commits
5 changed files with 75 additions and 26 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.
+32 -19
View File
@@ -1,36 +1,49 @@
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',
}; };
function addCondition (q, field, val) { function addCondition (q, field, val) {
var conditionFn, conditionName; if (Array.isArray(val[0])) {
return q.where(function () {
return val.forEach(addCondition.bind(null, this, field));
});
}
if (!Array.isArray(val)) { if (!Array.isArray(val)) {
return q.where( field, val ); val = ['AND', field, val ];
} } else if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
conditionName = val[0]; val = [ val[0], field ].concat(val.slice(1));
if( simpleOperators.indexOf( conditionName ) !== -1 ){ } else {
return q.where.apply(q, [field].concat(val) ); val = [ 'AND', field ].concat(val);
}
if( functionOperators.indexOf(conditionName) !== -1 ){
return q[functionOperatorMap[conditionName] ]( field, val.slice(1) );
} }
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.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": {
"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"
} }
} }
+33 -2
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,6 +12,31 @@ 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',
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 },
@@ -18,7 +44,7 @@ var conditions= [
}, },
{ {
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,6 +58,11 @@ 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 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 } ],
@@ -39,7 +70,7 @@ var conditions= [
}, },
{ {
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))'
}, },
{ {