mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48c8b8d059 | ||
|
|
ff5b938f22 | ||
|
|
aec35e5daf | ||
|
|
89e522be15 | ||
|
|
991999d767 | ||
|
|
2f16ae789f |
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4.4"
|
||||
@@ -1,2 +1,3 @@
|
||||
[](https://travis-ci.org/harish2704/knex-json-query)
|
||||
# knex-json-query
|
||||
A high-level utility which will will generate Knex query from a single JSON object.
|
||||
|
||||
@@ -1,36 +1,54 @@
|
||||
|
||||
var simpleOperators = [
|
||||
'LIKE',
|
||||
'>',
|
||||
'<',
|
||||
];
|
||||
|
||||
var functionOperators = [
|
||||
'BETWEEN',
|
||||
'IN'
|
||||
'IN',
|
||||
/* ---- */
|
||||
'OR',
|
||||
'AND',
|
||||
/* ---- */
|
||||
'OR_BETWEEN',
|
||||
'OR_IN',
|
||||
/* ---- */
|
||||
'AND_BETWEEN',
|
||||
'AND_IN'
|
||||
];
|
||||
|
||||
var functionOperatorMap = {
|
||||
BETWEEN: 'whereBetween',
|
||||
IN: 'whereIn'
|
||||
IN: 'whereIn',
|
||||
/* ---- */
|
||||
OR: 'orWhere',
|
||||
AND: 'where',
|
||||
/* ---- */
|
||||
OR_BETWEEN: 'orWhereBetween',
|
||||
OR_IN: 'orWhereIn',
|
||||
/* ---- */
|
||||
AND_BETWEEN: 'andWhereBetween',
|
||||
AND_IN: 'andWhereIn'
|
||||
};
|
||||
|
||||
|
||||
|
||||
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)) {
|
||||
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) );
|
||||
val = ['AND', field, val ]
|
||||
} else if (functionOperators.indexOf(val[0]) !== -1) {
|
||||
val.splice(1, 0, field)
|
||||
} else {
|
||||
val = [ 'AND', field ].concat(val)
|
||||
}
|
||||
|
||||
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1))
|
||||
}
|
||||
|
||||
|
||||
function getWhereCondition( cond ){
|
||||
if( Array.isArray(cond) ){
|
||||
return function(){
|
||||
|
||||
+4
-3
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "knex-json-query",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"description": "A high-level utility which will will generate Knex query from a single JSON object.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "mocha -R spec"
|
||||
"test": "node test"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -22,6 +22,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/harish2704/knex-json-query#readme",
|
||||
"devDependencies": {
|
||||
"knex": "^0.12.6"
|
||||
"knex": "^0.12.6",
|
||||
"simple-mocha": "0.0.9"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
var assert = require('assert');
|
||||
var knex = require('knex')({});
|
||||
var knexJsonQuery = require('./');
|
||||
require('simple-mocha');
|
||||
|
||||
|
||||
var conditions= [
|
||||
@@ -11,6 +12,21 @@ var conditions= [
|
||||
input:{ f1: 10, f2: 20, f3: 30 },
|
||||
output: 'select * where ("f1" = 10 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',
|
||||
input:{ f1: [ 'LIKE', 20 ], f2: 20, f3: 30 },
|
||||
@@ -18,7 +34,7 @@ var conditions= [
|
||||
},
|
||||
{
|
||||
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)'
|
||||
},
|
||||
{
|
||||
@@ -32,6 +48,11 @@ var conditions= [
|
||||
input:[ { f1: 10 }, { f2: 20 }, { 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',
|
||||
input:[ { f1: ['LIKE',10] }, { f2: 20 }, { f3: 30 } ],
|
||||
@@ -39,7 +60,7 @@ var conditions= [
|
||||
},
|
||||
{
|
||||
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))'
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user