3 changed files with 140 additions and 0 deletions
@ -0,0 +1,52 @@ |
|||||
|
|
||||
|
var simpleOperators = [ |
||||
|
'LIKE', |
||||
|
'>', |
||||
|
'<', |
||||
|
]; |
||||
|
|
||||
|
var functionOperators = [ |
||||
|
'BETWEEN', |
||||
|
'IN' |
||||
|
]; |
||||
|
|
||||
|
var functionOperatorMap = { |
||||
|
BETWEEN: 'whereBetween', |
||||
|
IN: 'whereIn' |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
|
||||
|
function addCondition( q, field, val ){ |
||||
|
var conditionFn, conditionName; |
||||
|
if( !Array.isArray(val)){ |
||||
|
return q.where( field, val ); |
||||
|
} |
||||
|
conditionName = val[0]; |
||||
|
if( simpleOperators.indexOf( conditionName ) !== -1 ){ |
||||
|
return q.where.apply(q, [field].concat(val) ); |
||||
|
} |
||||
|
if( functionOperators.indexOf(conditionName) !== -1 ){ |
||||
|
return q[functionOperatorMap[conditionName] ]( field, val.slice(1) ); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function getWhereCondition( cond ){ |
||||
|
if( Array.isArray(cond) ){ |
||||
|
return function(){ |
||||
|
cond.forEach( function(v){ |
||||
|
this.orWhere( getWhereCondition(v) ); |
||||
|
}, this ); |
||||
|
}; |
||||
|
} else { |
||||
|
return function(){ |
||||
|
var field; |
||||
|
for( field in cond ){ |
||||
|
addCondition( this, field, cond[field] ); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
module.exports = getWhereCondition; |
@ -0,0 +1,27 @@ |
|||||
|
{ |
||||
|
"name": "knex-json-query", |
||||
|
"version": "0.0.1", |
||||
|
"description": "A high-level utility which will will generate Knex query from a single JSON object.", |
||||
|
"main": "index.js", |
||||
|
"scripts": { |
||||
|
"test": "mocha -R spec" |
||||
|
}, |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/harish2704/knex-json-query.git" |
||||
|
}, |
||||
|
"keywords": [ |
||||
|
"knex", |
||||
|
"sql", |
||||
|
"json-query" |
||||
|
], |
||||
|
"author": "Harish.K <harish2704@gmail.com>", |
||||
|
"license": "MIT", |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/harish2704/knex-json-query/issues" |
||||
|
}, |
||||
|
"homepage": "https://github.com/harish2704/knex-json-query#readme", |
||||
|
"devDependencies": { |
||||
|
"knex": "^0.12.6" |
||||
|
} |
||||
|
} |
@ -0,0 +1,61 @@ |
|||||
|
/* global describe, it */ |
||||
|
var assert = require('assert'); |
||||
|
var knex = require('knex')({}); |
||||
|
var knexJsonQuery = require('./'); |
||||
|
|
||||
|
|
||||
|
var conditions= [ |
||||
|
/* -----------Tests for and conditoins---------- */ |
||||
|
{ |
||||
|
name: 'handle simple and conditions', |
||||
|
input:{ f1: 10, f2: 20, f3: 30 }, |
||||
|
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'handle simple and condition with like statement', |
||||
|
input:{ f1: ['LIKE', 20], f2: 20, f3: 30 }, |
||||
|
output: 'select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'handle simple and condition with between statement', |
||||
|
input:{ f1: ['BETWEEN', 50, 60 ], f2: 20, f3: 30 }, |
||||
|
output: 'select * where ("f1" between 50 and 60 and "f2" = 20 and "f3" = 30)' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'handle simple and condition with in statement', |
||||
|
input:{ f1: ['IN', [ 50, 60 ] ], f2: 20, f3: 30 }, |
||||
|
output: 'select * where ("f1" in (50, 60) and "f2" = 20 and "f3" = 30)' |
||||
|
}, |
||||
|
/* -----------Tests for or conditoins---------- */ |
||||
|
{ |
||||
|
name: 'handle simple or condition', |
||||
|
input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ], |
||||
|
output: 'select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'handle simple or condition with like statement', |
||||
|
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', |
||||
|
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', |
||||
|
input:[ { f1: ['IN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ], |
||||
|
output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))' |
||||
|
}, |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
describe('SQL query generation from json query', function(){ |
||||
|
conditions.forEach(function(v){ |
||||
|
it( 'should ' + v.name , function(){ |
||||
|
var expectedOut = knex.where( knexJsonQuery(v.input) ) + ''; |
||||
|
assert.equal( v.output, expectedOut ); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
Loading…
Reference in new issue