├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | .vscode/* 39 | .idea 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "12" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Harish K 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/harish2704/knex-json-query.svg?branch=master)](https://travis-ci.org/harish2704/knex-json-query) 2 | # knex-json-query 3 | A high-level utility which will will generate Knex query from a single JSON object. 4 | 5 | it is a simple function with just ~80 lines of code. 6 | 7 | ## Usage 8 | 9 | ```javascript 10 | var knex = require('knex'); 11 | //var KnexJsonQuery = require('knex-json-query'); 12 | var KnexJsonQuery = require('./'); 13 | var jsonInput = {f1:{ $like: 'test%' }, $and: [{f2:22, f3: 33}] }; 14 | var queryBuilderFunction = KnexJsonQuery( jsonInput ); 15 | 16 | var sql = knex.where( queryBuilderFunction ).toString() 17 | console.log( sql ); 18 | 19 | ``` 20 | 21 | **Output** 22 | ``` 23 | select * where ("f1" LIKE 'test%' and (("f2" = 22 and "f3" = 33))) 24 | ``` 25 | 26 | ## Examples 27 | 28 | For more details, see the [test.js](./test.js) 29 | 30 | **Examples from Unit test file:** 31 | 32 | 1. simple and conditions 33 | - *input JSON*: `{"f1":10,"f2":20,"f3":30}` 34 | - *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)` 35 | 36 | 2. simple and conditions 37 | - *input JSON*: `{"firstName":[["LIKE","H%"],["OR","hemanth"]]}` 38 | - *output SQL*: `select * where (("firstName" LIKE 'H%' or "firstName" = 'hemanth'))` 39 | 40 | 3. regex query: MongoQuery 41 | - *input JSON*: `{"firstName":{"$regex":"H*"}}` 42 | - *output SQL*: `select * where ("firstName" REGEXP 'H*')` 43 | 44 | 4. regex query and should omit $options: MongoQuery 45 | - *input JSON*: `{"firstName":{"$regex":"H*","$options":"i"}}` 46 | - *output SQL*: `select * where ("firstName" REGEXP 'H*')` 47 | 48 | 5. simple and conditions 49 | - *input JSON*: `{"f1":[["A"],["LIKE","B"]],"f2":20,"f3":30}` 50 | - *output SQL*: `select * where (("f1" = 'A' and "f1" LIKE 'B') and "f2" = 20 and "f3" = 30)` 51 | 52 | 6. basic operators :: greater-than less-than 53 | - *input JSON*: `{"f1":[">",10],"f2":["<",20],"f3":30}` 54 | - *output SQL*: `select * where ("f1" > 10 and "f2" < 20 and "f3" = 30)` 55 | 56 | 7. basic operators :: in 57 | - *input JSON*: `{"f1":["IN",[10,50]],"f2":["<",20],"f3":30}` 58 | - *output SQL*: `select * where ("f1" in (10, 50) and "f2" < 20 and "f3" = 30)` 59 | 60 | 8. basic operators :: notin 61 | - *input JSON*: `{"f1":["NOTIN",[10,50]],"f2":["<",20],"f3":30}` 62 | - *output SQL*: `select * where ("f1" not in (10, 50) and "f2" < 20 and "f3" = 30)` 63 | 64 | 9. multiple conditions in one field 65 | - *input JSON*: `{"f1":[["LIKE",20],21,["AND_BETWEEN",[40,45]]],"f2":20,"f3":30}` 66 | - *output SQL*: `select * where (("f1" LIKE 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)` 67 | 68 | 10. simple and condition with like statement 69 | - *input JSON*: `{"f1":["LIKE",20],"f2":20,"f3":30}` 70 | - *output SQL*: `select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)` 71 | 72 | 11. simple and condition with between statement 73 | - *input JSON*: `{"f1":["BETWEEN",[50,60]],"f2":20,"f3":30}` 74 | - *output SQL*: `select * where ("f1" between 50 and 60 and "f2" = 20 and "f3" = 30)` 75 | 76 | 12. simple and condition with in statement 77 | - *input JSON*: `{"f1":["IN",[50,60]],"f2":20,"f3":30}` 78 | - *output SQL*: `select * where ("f1" in (50, 60) and "f2" = 20 and "f3" = 30)` 79 | 80 | 13. simple and condition with not in statement 81 | - *input JSON*: `{"f1":["NOTIN",[50,60]],"f2":20,"f3":30}` 82 | - *output SQL*: `select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)` 83 | 84 | 14. simple and condition with raw statement 85 | - *input JSON*: `{"f1":{"$raw":"@> ANY(ARRAY(1,2,3))"},"f2":20}` 86 | - *output SQL*: `select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)` 87 | 88 | 15. $and grouping 89 | - *input JSON*: `{"f1":10,"f2":20,"f3":30,"$and":[{"f4":55},{"f5":66}]}` 90 | - *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 and (("f4" = 55) or ("f5" = 66)))` 91 | 92 | 16. simple or condition 93 | - *input JSON*: `[{"f1":10},{"f2":20},{"f3":30}]` 94 | - *output SQL*: `select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))` 95 | 96 | 17. multiple conditions in one field 97 | - *input JSON*: `{"f1":[["LIKE",20],["OR",21],["OR_BETWEEN",[40,45]]],"f2":20,"f3":30}` 98 | - *output SQL*: `select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)` 99 | 100 | 18. multiple conditions in one field: MongoQuery 101 | - *input JSON*: `{"f1":{"$like":20,"$or":21,"$or_between":[40,45]},"f2":20,"f3":30}` 102 | - *output SQL*: `select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)` 103 | 104 | 19. simple or condition with like statement 105 | - *input JSON*: `[{"f1":["LIKE",10]},{"f2":20},{"f3":30}]` 106 | - *output SQL*: `select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))` 107 | 108 | 20. simple or condition with like statement: MongoQuery 109 | - *input JSON*: `[{"f1":{"$like":10}},{"f2":20},{"f3":30}]` 110 | - *output SQL*: `select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))` 111 | 112 | 21. simple or condition with between statement 113 | - *input JSON*: `[{"f1":["BETWEEN",[50,60]]},{"f2":20},{"f3":30}]` 114 | - *output SQL*: `select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))` 115 | 116 | 22. simple or condition with between statement :MongoQuery 117 | - *input JSON*: `[{"f1":{"$between":[50,60]}},{"f2":20},{"f3":30}]` 118 | - *output SQL*: `select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))` 119 | 120 | 23. simple or condition with in statement 121 | - *input JSON*: `[{"f1":["IN",[50,60]]},{"f2":20},{"f3":30}]` 122 | - *output SQL*: `select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))` 123 | 124 | 24. simple or condition with not in statement 125 | - *input JSON*: `[{"f1":["NOTIN",[50,60]]},{"f2":20},{"f3":30}]` 126 | - *output SQL*: `select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))` 127 | 128 | 25. simple or condition with in statement :MongoQuery 129 | - *input JSON*: `[{"f1":{"$in":[50,60]}},{"f2":20},{"f3":30}]` 130 | - *output SQL*: `select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))` 131 | 132 | 26. simple or condition with nin statement :MongoQuery 133 | - *input JSON*: `[{"f1":{"$nin":[50,60]}},{"f2":20},{"f3":30}]` 134 | - *output SQL*: `select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))` 135 | 136 | 27. simple or condition with raw statement 137 | - *input JSON*: `[{"f1":{"$raw":"@> ANY(ARRAY(1,2,3))"}},{"f2":20}]` 138 | - *output SQL*: `select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))` 139 | 140 | 28. simple or condition with conditional array 141 | - *input JSON*: `{"f1":[["ILIKE","awesome"],["OR_ILIKE","%super%"]]}` 142 | - *output SQL*: `select * where (("f1" ILIKE 'awesome' or "f1" ILIKE '%super%'))` 143 | 144 | 29. $or grouping 145 | - *input JSON*: `{"f1":10,"f2":20,"f3":30,"$or":[{"f4":55},{"f5":66}]}` 146 | - *output SQL*: `select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 or (("f4" = 55) or ("f5" = 66)))` 147 | 148 | 149 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var functionOperatorMap = { 4 | BETWEEN: 'whereBetween', 5 | IN: 'whereIn', 6 | NIN: 'whereNotIn', 7 | NOTIN: 'whereNotIn', 8 | ISNULL: 'whereNull', 9 | NOTNULL: 'whereNotNull', 10 | /* ---- */ 11 | OR: 'orWhere', 12 | AND: 'where', 13 | EQ: 'where', 14 | '=': 'where', 15 | /* ---- */ 16 | OR_BETWEEN: 'orWhereBetween', 17 | OR_IN: 'orWhereIn', 18 | OR_NOTIN: 'orWhereNotIn', 19 | OR_ISNULL: 'orWhereNull', 20 | IR_NOTNULL: 'orWhereNotNull', 21 | /* ---- */ 22 | AND_BETWEEN: 'andWhereBetween', 23 | AND_IN: 'andWhereIn', 24 | AND_NOTIN: 'andWhereNotIn', 25 | AND_ISNULL: 'andWhereNull', 26 | AND_NOTNULL: 'andWhereNotNull', 27 | /* ---- */ 28 | RAW: 'whereRaw', 29 | OR_RAW: 'orWhereRaw', 30 | AND_RAW: 'whereRaw', 31 | }; 32 | 33 | var aliases = { 34 | REGEX: 'REGEXP' 35 | }; 36 | 37 | 38 | 39 | function addCondition (q, field, val) { 40 | if( field === '$or' ){ 41 | return q.orWhere( getWhereCondition( val )) 42 | } 43 | if( field === '$and' ){ 44 | return q.where( getWhereCondition( val )) 45 | } 46 | if( val.constructor.name === 'Object' ){ 47 | delete val.$options; 48 | val = Object.keys(val).map( function(key){ 49 | return [ key.slice(1).toUpperCase(), val[key] ]; 50 | }); 51 | if( val.length === 1){ 52 | val = val[0]; 53 | } 54 | } 55 | if (Array.isArray(val[0])) { 56 | return q.where(function () { 57 | return val.forEach(addCondition.bind(null, this, field)); 58 | }); 59 | } 60 | 61 | if (!Array.isArray(val)) { 62 | // Simple string or number value 63 | val = ['AND', field, val ]; 64 | } else { 65 | val[0] = aliases[ val[0] ] || val[0]; 66 | if (functionOperatorMap.hasOwnProperty( val[0] ) ) { 67 | // SQL operator 68 | val = [ val[0], field ].concat(val.slice(1)); 69 | } else { 70 | // Cases when we have something like 'OR_ILIKE' or 'AND_@>' 71 | var operators = /(\w+)_(\w+)/.exec(val[0]) 72 | var operatorsExist = operators && operators.constructor === Array && operators.length >= 3 73 | if (operatorsExist) { 74 | val = [operators[1], field].concat([operators[2]], val.slice(1)); 75 | } else { 76 | // other cases like ( '>', '10' ) Greater than 10 77 | val = [ 'AND', field ].concat(val); 78 | } 79 | } 80 | } 81 | var args = val[0].includes('RAW') ? [ q.client.raw('??', val[1]) + ' ' + val[2] ] : val.slice(1); 82 | return q[functionOperatorMap[val[0]]].apply(q, args); 83 | } 84 | 85 | 86 | function getWhereCondition( cond ){ 87 | if( Array.isArray(cond) ){ 88 | return function(){ 89 | cond.forEach( function(v){ 90 | this.orWhere( getWhereCondition(v) ); 91 | }, this ); 92 | }; 93 | } else { 94 | return function(){ 95 | var field; 96 | for( field in cond ){ 97 | addCondition( this, field, cond[field] ); 98 | } 99 | }; 100 | } 101 | } 102 | 103 | 104 | module.exports = getWhereCondition; 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "knex-json-query", 3 | "version": "1.0.0", 4 | "description": "A high-level utility which will will generate Knex query from a single JSON object.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/harish2704/knex-json-query.git" 12 | }, 13 | "keywords": [ 14 | "knex", 15 | "sql", 16 | "json-query" 17 | ], 18 | "author": "Harish.K ", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/harish2704/knex-json-query/issues" 22 | }, 23 | "homepage": "https://github.com/harish2704/knex-json-query#readme", 24 | "devDependencies": { 25 | "knex": "^0.20.10", 26 | "simple-mocha": "0.0.9" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | var assert = require('assert'); 3 | var Knex = require('knex'); 4 | var knexJsonQuery = require('./'); 5 | require('simple-mocha'); 6 | 7 | var testData = { 8 | mysql:{}, 9 | pg:{}, 10 | }; 11 | 12 | testData.pg.knex = Knex({client: 'pg' }); 13 | testData.pg.conditions = [ 14 | /* -----------Tests for and conditions---------- */ 15 | { 16 | name: 'handle simple and conditions', 17 | input:{ f1: 10, f2: 20, f3: 30 }, 18 | output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)' 19 | }, 20 | { 21 | name: 'handle simple and conditions', 22 | input:{"firstName":[["like","H%"],["OR","hemanth"]]}, 23 | output: 'select * where (("firstName" like \'H%\' or "firstName" = \'hemanth\'))' 24 | }, 25 | { 26 | name: 'handle regex query: MongoQuery', 27 | input:{"firstName":{ $regex: "H*" }}, 28 | output: 'select * where ("firstName" regexp \'H*\')' 29 | }, 30 | { 31 | name: 'handle regex query and should omit $options: MongoQuery', 32 | input:{"firstName":{ $regex: "H*", $options: 'i' }}, 33 | output: 'select * where ("firstName" regexp \'H*\')' 34 | }, 35 | { 36 | name: 'handle simple and conditions', 37 | input:{ f1: [[ 'A' ],['like', 'B' ]], f2: 20, f3: 30 }, 38 | output: 'select * where (("f1" = \'A\' and "f1" like \'B\') and "f2" = 20 and "f3" = 30)' 39 | }, 40 | { 41 | name: 'handle basic operators :: greater-than less-than', 42 | input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 }, 43 | output: 'select * where ("f1" > 10 and "f2" < 20 and "f3" = 30)' 44 | }, 45 | { 46 | name: 'handle basic operators :: in', 47 | input:{ f1: ['IN', [10,50]], f2: [ '<', 20 ], f3: 30 }, 48 | output: 'select * where ("f1" in (10, 50) and "f2" < 20 and "f3" = 30)' 49 | }, 50 | { 51 | name: 'handle basic operators :: notin', 52 | input:{ f1: ['NOTIN', [10,50]], f2: [ '<', 20 ], f3: 30 }, 53 | output: 'select * where ("f1" not in (10, 50) and "f2" < 20 and "f3" = 30)' 54 | }, 55 | { 56 | name: 'handle multiple conditions in one field', 57 | input:{ f1: [ [ 'like', 20 ], 21, [ 'AND_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 }, 58 | output: 'select * where (("f1" like 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)' 59 | }, 60 | { 61 | name: 'handle simple and condition with like statement', 62 | input:{ f1: [ 'like', 20 ], f2: 20, f3: 30 }, 63 | output: 'select * where ("f1" like 20 and "f2" = 20 and "f3" = 30)' 64 | }, 65 | { 66 | name: 'handle simple and condition with between statement', 67 | input:{ f1: ['BETWEEN', [50, 60] ], f2: 20, f3: 30 }, 68 | output: 'select * where ("f1" between 50 and 60 and "f2" = 20 and "f3" = 30)' 69 | }, 70 | { 71 | name: 'handle simple and condition with in statement', 72 | input:{ f1: ['IN', [ 50, 60 ] ], f2: 20, f3: 30 }, 73 | output: 'select * where ("f1" in (50, 60) and "f2" = 20 and "f3" = 30)' 74 | }, 75 | { 76 | name: 'handle simple and condition with not in statement', 77 | input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 }, 78 | output: 'select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)' 79 | }, 80 | { 81 | name: 'handle simple and condition with raw statement', 82 | input: { f1: { $raw: '@> ANY(ARRAY(1,2,3))' }, f2: 20 }, 83 | output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)' 84 | }, 85 | /* -----------Tests for or conditions---------- */ 86 | { 87 | name: 'handle $and grouping', 88 | input:{ f1: 10, f2: 20, f3: 30, $and: [ { f4: 55 }, { f5: 66} ] }, 89 | output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 and (("f4" = 55) or ("f5" = 66)))' 90 | }, 91 | /* -----------Tests for or conditoins---------- */ 92 | { 93 | name: 'handle simple or condition', 94 | input:[ { f1: 10 }, { f2: 20 }, { f3: 30 } ], 95 | output: 'select * where (("f1" = 10) or ("f2" = 20) or ("f3" = 30))' 96 | }, 97 | { 98 | name: 'handle multiple conditions in one field', 99 | input:{ f1: [ [ 'like', 20 ], [ 'OR', 21 ], [ 'OR_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 }, 100 | output: 'select * where (("f1" like 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)' 101 | }, 102 | { 103 | name: 'handle multiple conditions in one field: MongoQuery', 104 | input:{ f1: { $like: 20, $or: 21, $or_between: [ 40, 45 ] }, f2: 20, f3: 30 }, 105 | output: 'select * where (("f1" like 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)' 106 | }, 107 | { 108 | name: 'handle simple or condition with like statement', 109 | input:[ { f1: ['like',10] }, { f2: 20 }, { f3: 30 } ], 110 | output: 'select * where (("f1" like 10) or ("f2" = 20) or ("f3" = 30))' 111 | }, 112 | { 113 | name: 'handle simple or condition with like statement: MongoQuery', 114 | input:[ { f1: { $like: 10 } }, { f2: 20 }, { f3: 30 } ], 115 | output: 'select * where (("f1" like 10) or ("f2" = 20) or ("f3" = 30))' 116 | }, 117 | { 118 | name: 'handle simple or condition with between statement', 119 | input:[ { f1: ['BETWEEN',[50, 60] ] }, { f2: 20 }, { f3: 30 } ], 120 | output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))' 121 | }, 122 | { 123 | name: 'handle simple or condition with between statement :MongoQuery', 124 | input:[ { f1: { $between:[50, 60] } }, { f2: 20 }, { f3: 30 } ], 125 | output: 'select * where (("f1" between 50 and 60) or ("f2" = 20) or ("f3" = 30))' 126 | }, 127 | { 128 | name: 'handle simple or condition with in statement', 129 | input:[ { f1: ['IN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ], 130 | output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))' 131 | }, 132 | { 133 | name: 'handle simple or condition with not in statement', 134 | input:[ { f1: ['NOTIN',[ 50, 60 ]] }, { f2: 20 }, { f3: 30 } ], 135 | output: 'select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))' 136 | }, 137 | { 138 | name: 'handle simple or condition with in statement :MongoQuery', 139 | input:[ { f1: {$in:[ 50, 60 ]} }, { f2: 20 }, { f3: 30 } ], 140 | output: 'select * where (("f1" in (50, 60)) or ("f2" = 20) or ("f3" = 30))' 141 | }, 142 | { 143 | name: 'handle simple or condition with nin statement :MongoQuery', 144 | input:[ { f1: { $nin: [ 50, 60 ] } }, { f2: 20 }, { f3: 30 } ], 145 | output: 'select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))' 146 | }, 147 | { 148 | name: 'handle simple or condition with raw statement', 149 | input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ], 150 | output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))' 151 | }, 152 | { 153 | name: 'handle simple or condition with conditional array', 154 | input: { f1: [['ilike', 'awesome'], ['OR_ilike', '%super%'] ] }, 155 | output: 'select * where (("f1" ilike \'awesome\' or "f1" ilike \'%super%\'))' 156 | }, 157 | { 158 | name: 'handle $or grouping', 159 | input:{ f1: 10, f2: 20, f3: 30, $or: [ { f4: 55 }, { f5: 66} ] }, 160 | output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30 or (("f4" = 55) or ("f5" = 66)))' 161 | }, 162 | ]; 163 | 164 | 165 | testData.mysql.knex = Knex({client: 'mysql' }); 166 | testData.mysql.conditions = [ 167 | { 168 | name: 'Mysql: handle "and" condition with raw statement', 169 | input: { f1: { $raw: '< `use_limit`' }, f2: 20 }, 170 | output: 'select * where (`f1` < `use_limit` and `f2` = 20)' 171 | }, 172 | ] 173 | 174 | 175 | Object.keys( testData ).forEach(function(driver){ 176 | var data = testData[driver]; 177 | var conditions = data.conditions; 178 | var knex = data.knex; 179 | 180 | describe('SQL query generation from json query', function(){ 181 | conditions.forEach(function(v){ 182 | it( 'should ' + v.name , function(){ 183 | var expectedOut = knex.where( knexJsonQuery(v.input) ) + ''; 184 | assert.equal( v.output, expectedOut ); 185 | }); 186 | }); 187 | }); 188 | 189 | }) 190 | 191 | --------------------------------------------------------------------------------