mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7438e04b7 | ||
|
|
8a4008e49a | ||
|
|
8d0da36dd2 | ||
|
|
90cd115dc5 | ||
|
|
9f2c6336c8 | ||
|
|
205e8d1de4 | ||
|
|
a5fa7f142f | ||
|
|
969ceb6dcd | ||
|
|
aba8ec63a5 | ||
|
|
eee8f1c975 | ||
|
|
53058d58bf | ||
|
|
ae0663d314 | ||
|
|
4422e9e424 | ||
|
|
d96011dc19 | ||
|
|
0f93490366 | ||
|
|
91301c839a | ||
|
|
e44ebb33f2 | ||
|
|
26a9b57e90 | ||
|
|
13427005fe | ||
|
|
68bdbf750c | ||
|
|
498ac917d4 |
@@ -35,3 +35,5 @@ jspm_packages
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
.vscode/*
|
||||
.idea
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4.4"
|
||||
- "12"
|
||||
|
||||
@@ -1,3 +1,148 @@
|
||||
[](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.
|
||||
|
||||
it is a simple function with just ~80 lines of code.
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
var knex = require('knex');
|
||||
//var KnexJsonQuery = require('knex-json-query');
|
||||
var KnexJsonQuery = require('./');
|
||||
var jsonInput = {f1:{ $like: 'test%' }, $and: [{f2:22, f3: 33}] };
|
||||
var queryBuilderFunction = KnexJsonQuery( jsonInput );
|
||||
|
||||
var sql = knex.where( queryBuilderFunction ).toString()
|
||||
console.log( sql );
|
||||
|
||||
```
|
||||
|
||||
**Output**
|
||||
```
|
||||
select * where ("f1" LIKE 'test%' and (("f2" = 22 and "f3" = 33)))
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
For more details, see the [test.js](./test.js)
|
||||
|
||||
**Examples from Unit test file:**
|
||||
|
||||
1. simple and conditions
|
||||
- *input JSON*: `{"f1":10,"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
2. simple and conditions
|
||||
- *input JSON*: `{"firstName":[["LIKE","H%"],["OR","hemanth"]]}`
|
||||
- *output SQL*: `select * where (("firstName" LIKE 'H%' or "firstName" = 'hemanth'))`
|
||||
|
||||
3. regex query: MongoQuery
|
||||
- *input JSON*: `{"firstName":{"$regex":"H*"}}`
|
||||
- *output SQL*: `select * where ("firstName" REGEXP 'H*')`
|
||||
|
||||
4. regex query and should omit $options: MongoQuery
|
||||
- *input JSON*: `{"firstName":{"$regex":"H*","$options":"i"}}`
|
||||
- *output SQL*: `select * where ("firstName" REGEXP 'H*')`
|
||||
|
||||
5. simple and conditions
|
||||
- *input JSON*: `{"f1":[["A"],["LIKE","B"]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where (("f1" = 'A' and "f1" LIKE 'B') and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
6. basic operators :: greater-than less-than
|
||||
- *input JSON*: `{"f1":[">",10],"f2":["<",20],"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" > 10 and "f2" < 20 and "f3" = 30)`
|
||||
|
||||
7. basic operators :: in
|
||||
- *input JSON*: `{"f1":["IN",[10,50]],"f2":["<",20],"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" in (10, 50) and "f2" < 20 and "f3" = 30)`
|
||||
|
||||
8. basic operators :: notin
|
||||
- *input JSON*: `{"f1":["NOTIN",[10,50]],"f2":["<",20],"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" not in (10, 50) and "f2" < 20 and "f3" = 30)`
|
||||
|
||||
9. multiple conditions in one field
|
||||
- *input JSON*: `{"f1":[["LIKE",20],21,["AND_BETWEEN",[40,45]]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where (("f1" LIKE 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
10. simple and condition with like statement
|
||||
- *input JSON*: `{"f1":["LIKE",20],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
11. simple and condition with between statement
|
||||
- *input JSON*: `{"f1":["BETWEEN",[50,60]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" between 50 and 60 and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
12. simple and condition with in statement
|
||||
- *input JSON*: `{"f1":["IN",[50,60]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" in (50, 60) and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
13. simple and condition with not in statement
|
||||
- *input JSON*: `{"f1":["NOTIN",[50,60]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
14. simple and condition with raw statement
|
||||
- *input JSON*: `{"f1":{"$raw":"@> ANY(ARRAY(1,2,3))"},"f2":20}`
|
||||
- *output SQL*: `select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)`
|
||||
|
||||
15. $and grouping
|
||||
- *input JSON*: `{"f1":10,"f2":20,"f3":30,"$and":[{"f4":55},{"f5":66}]}`
|
||||
- *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 and (("f4" = 55) or ("f5" = 66)))`
|
||||
|
||||
16. simple or condition
|
||||
- *input JSON*: `[{"f1":10},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
17. multiple conditions in one field
|
||||
- *input JSON*: `{"f1":[["LIKE",20],["OR",21],["OR_BETWEEN",[40,45]]],"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
18. multiple conditions in one field: MongoQuery
|
||||
- *input JSON*: `{"f1":{"$like":20,"$or":21,"$or_between":[40,45]},"f2":20,"f3":30}`
|
||||
- *output SQL*: `select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)`
|
||||
|
||||
19. simple or condition with like statement
|
||||
- *input JSON*: `[{"f1":["LIKE",10]},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
20. simple or condition with like statement: MongoQuery
|
||||
- *input JSON*: `[{"f1":{"$like":10}},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
21. simple or condition with between statement
|
||||
- *input JSON*: `[{"f1":["BETWEEN",[50,60]]},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
22. simple or condition with between statement :MongoQuery
|
||||
- *input JSON*: `[{"f1":{"$between":[50,60]}},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
23. simple or condition with in statement
|
||||
- *input JSON*: `[{"f1":["IN",[50,60]]},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
24. simple or condition with not in statement
|
||||
- *input JSON*: `[{"f1":["NOTIN",[50,60]]},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
25. simple or condition with in statement :MongoQuery
|
||||
- *input JSON*: `[{"f1":{"$in":[50,60]}},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
26. simple or condition with nin statement :MongoQuery
|
||||
- *input JSON*: `[{"f1":{"$nin":[50,60]}},{"f2":20},{"f3":30}]`
|
||||
- *output SQL*: `select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))`
|
||||
|
||||
27. simple or condition with raw statement
|
||||
- *input JSON*: `[{"f1":{"$raw":"@> ANY(ARRAY(1,2,3))"}},{"f2":20}]`
|
||||
- *output SQL*: `select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))`
|
||||
|
||||
28. simple or condition with conditional array
|
||||
- *input JSON*: `{"f1":[["ILIKE","awesome"],["OR_ILIKE","%super%"]]}`
|
||||
- *output SQL*: `select * where (("f1" ILIKE 'awesome' or "f1" ILIKE '%super%'))`
|
||||
|
||||
29. $or grouping
|
||||
- *input JSON*: `{"f1":10,"f2":20,"f3":30,"$or":[{"f4":55},{"f5":66}]}`
|
||||
- *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 or (("f4" = 55) or ("f5" = 66)))`
|
||||
|
||||
|
||||
|
||||
@@ -1,24 +1,12 @@
|
||||
|
||||
|
||||
var functionOperators = [
|
||||
'BETWEEN',
|
||||
'IN',
|
||||
/* ---- */
|
||||
'OR',
|
||||
'AND',
|
||||
'EQ',
|
||||
'=',
|
||||
/* ---- */
|
||||
'OR_BETWEEN',
|
||||
'OR_IN',
|
||||
/* ---- */
|
||||
'AND_BETWEEN',
|
||||
'AND_IN'
|
||||
];
|
||||
"use strict";
|
||||
|
||||
var functionOperatorMap = {
|
||||
BETWEEN: 'whereBetween',
|
||||
IN: 'whereIn',
|
||||
NIN: 'whereNotIn',
|
||||
NOTIN: 'whereNotIn',
|
||||
ISNULL: 'whereNull',
|
||||
NOTNULL: 'whereNotNull',
|
||||
/* ---- */
|
||||
OR: 'orWhere',
|
||||
AND: 'where',
|
||||
@@ -27,14 +15,43 @@ var functionOperatorMap = {
|
||||
/* ---- */
|
||||
OR_BETWEEN: 'orWhereBetween',
|
||||
OR_IN: 'orWhereIn',
|
||||
OR_NOTIN: 'orWhereNotIn',
|
||||
OR_ISNULL: 'orWhereNull',
|
||||
IR_NOTNULL: 'orWhereNotNull',
|
||||
/* ---- */
|
||||
AND_BETWEEN: 'andWhereBetween',
|
||||
AND_IN: 'andWhereIn'
|
||||
AND_IN: 'andWhereIn',
|
||||
AND_NOTIN: 'andWhereNotIn',
|
||||
AND_ISNULL: 'andWhereNull',
|
||||
AND_NOTNULL: 'andWhereNotNull',
|
||||
/* ---- */
|
||||
RAW: 'whereRaw',
|
||||
OR_RAW: 'orWhereRaw',
|
||||
AND_RAW: 'whereRaw',
|
||||
};
|
||||
|
||||
var aliases = {
|
||||
REGEX: 'REGEXP'
|
||||
};
|
||||
|
||||
|
||||
|
||||
function addCondition (q, field, val) {
|
||||
if( field === '$or' ){
|
||||
return q.orWhere( getWhereCondition( val ))
|
||||
}
|
||||
if( field === '$and' ){
|
||||
return q.where( getWhereCondition( 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])) {
|
||||
return q.where(function () {
|
||||
return val.forEach(addCondition.bind(null, this, field));
|
||||
@@ -42,14 +59,27 @@ function addCondition (q, field, val) {
|
||||
}
|
||||
|
||||
if (!Array.isArray(val)) {
|
||||
// Simple string or number value
|
||||
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));
|
||||
} else {
|
||||
// Cases when we have something like 'OR_ILIKE' or 'AND_@>'
|
||||
var operators = /(\w+)_(\w+)/.exec(val[0])
|
||||
var operatorsExist = operators && operators.constructor === Array && operators.length >= 3
|
||||
if (operatorsExist) {
|
||||
val = [operators[1], field].concat([operators[2]], 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));
|
||||
}
|
||||
}
|
||||
var args = val[0].includes('RAW') ? [ q.client.raw('??', val[1]) + ' ' + val[2] ] : val.slice(1);
|
||||
return q[functionOperatorMap[val[0]]].apply(q, args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "knex-json-query",
|
||||
"version": "0.0.2",
|
||||
"version": "1.0.0",
|
||||
"description": "A high-level utility which will will generate Knex query from a single JSON object.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@@ -22,7 +22,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/harish2704/knex-json-query#readme",
|
||||
"devDependencies": {
|
||||
"knex": "^0.12.6",
|
||||
"knex": "^0.20.10",
|
||||
"simple-mocha": "0.0.9"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
/* global describe, it */
|
||||
var assert = require('assert');
|
||||
var knex = require('knex')({});
|
||||
var Knex = require('knex');
|
||||
var knexJsonQuery = require('./');
|
||||
require('simple-mocha');
|
||||
|
||||
var testData = {
|
||||
mysql:{},
|
||||
pg:{},
|
||||
};
|
||||
|
||||
var conditions= [
|
||||
/* -----------Tests for and conditoins---------- */
|
||||
testData.pg.knex = Knex({client: 'pg' });
|
||||
testData.pg.conditions = [
|
||||
/* -----------Tests for and conditions---------- */
|
||||
{
|
||||
name: 'handle simple and conditions',
|
||||
input:{ f1: 10, f2: 20, f3: 30 },
|
||||
@@ -14,13 +19,23 @@ var conditions= [
|
||||
},
|
||||
{
|
||||
name: 'handle simple and conditions',
|
||||
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
|
||||
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
|
||||
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)'
|
||||
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',
|
||||
@@ -32,15 +47,20 @@ var conditions= [
|
||||
input:{ f1: ['IN', [10,50]], f2: [ '<', 20 ], f3: 30 },
|
||||
output: 'select * where ("f1" in (10, 50) and "f2" < 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle basic operators :: notin',
|
||||
input:{ f1: ['NOTIN', [10,50]], f2: [ '<', 20 ], f3: 30 },
|
||||
output: 'select * where ("f1" not 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)'
|
||||
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 },
|
||||
output: 'select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)'
|
||||
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',
|
||||
@@ -52,6 +72,22 @@ var conditions= [
|
||||
input:{ f1: ['IN', [ 50, 60 ] ], f2: 20, f3: 30 },
|
||||
output: 'select * where ("f1" in (50, 60) and "f2" = 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle simple and condition with not in statement',
|
||||
input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 },
|
||||
output: 'select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle simple and condition with raw statement',
|
||||
input: { f1: { $raw: '@> ANY(ARRAY(1,2,3))' }, f2: 20 },
|
||||
output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)'
|
||||
},
|
||||
/* -----------Tests for or conditions---------- */
|
||||
{
|
||||
name: 'handle $and grouping',
|
||||
input:{ f1: 10, f2: 20, f3: 30, $and: [ { f4: 55 }, { f5: 66} ] },
|
||||
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 and (("f4" = 55) or ("f5" = 66)))'
|
||||
},
|
||||
/* -----------Tests for or conditoins---------- */
|
||||
{
|
||||
name: 'handle simple or condition',
|
||||
@@ -60,33 +96,95 @@ var conditions= [
|
||||
},
|
||||
{
|
||||
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)'
|
||||
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',
|
||||
input:[ { f1: ['LIKE',10] }, { f2: 20 }, { f3: 30 } ],
|
||||
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
||||
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 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',
|
||||
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))'
|
||||
},
|
||||
{
|
||||
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))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with not in statement',
|
||||
input:[ { f1: ['NOTIN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ],
|
||||
output: 'select * where (("f1" not 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))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with nin statement :MongoQuery',
|
||||
input:[ { f1: { $nin: [ 50, 60 ] } }, { f2: 20 }, { f3: 30 } ],
|
||||
output: 'select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with raw statement',
|
||||
input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ],
|
||||
output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with conditional array',
|
||||
input: { f1: [['ilike', 'awesome'], ['OR_ilike', '%super%'] ] },
|
||||
output: 'select * where (("f1" ilike \'awesome\' or "f1" ilike \'%super%\'))'
|
||||
},
|
||||
{
|
||||
name: 'handle $or grouping',
|
||||
input:{ f1: 10, f2: 20, f3: 30, $or: [ { f4: 55 }, { f5: 66} ] },
|
||||
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 or (("f4" = 55) or ("f5" = 66)))'
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
describe('SQL query generation from json query', function(){
|
||||
testData.mysql.knex = Knex({client: 'mysql' });
|
||||
testData.mysql.conditions = [
|
||||
{
|
||||
name: 'Mysql: handle "and" condition with raw statement',
|
||||
input: { f1: { $raw: '< `use_limit`' }, f2: 20 },
|
||||
output: 'select * where (`f1` < `use_limit` and `f2` = 20)'
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
Object.keys( testData ).forEach(function(driver){
|
||||
var data = testData[driver];
|
||||
var conditions = data.conditions;
|
||||
var knex = data.knex;
|
||||
|
||||
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 );
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user