9 Commits
Author SHA1 Message Date
harish2704 b7438e04b7 Merge branch 'master' of github.com:harish2704/knex-json-query
* 'master' of github.com:harish2704/knex-json-query:
  Fix for column names in raw
2021-07-19 00:59:52 +05:30
harish2704 8a4008e49a Cleanup 2021-07-19 00:59:40 +05:30
harish2704 8d0da36dd2 Update test for #4 2021-07-19 00:59:06 +05:30
harish2704andGitHub 90cd115dc5 Merge pull request #4 from esalazarv/fix/raw-quotes
Fix for column names in raw. Especially for non postgres drivers
2021-07-19 00:57:56 +05:30
Eduardo Salazar 9f2c6336c8 Fix for column names in raw
Use raw method to wrap column name with ticks
2021-07-12 20:10:57 -05:00
harish2704 205e8d1de4 Fix case sensitivity etc of unit test 2020-02-25 15:27:59 +05:30
harish2704andGitHub a5fa7f142f Merge pull request #3 from harish2704/dependabot/npm_and_yarn/knex-0.20.10
Bump knex from 0.12.9 to 0.20.10
2020-02-25 15:10:45 +05:30
harish2704andGitHub 969ceb6dcd Update node version for travis 2020-02-25 15:08:22 +05:30
dependabot[bot]andGitHub aba8ec63a5 Bump knex from 0.12.9 to 0.20.10
Bumps [knex](https://github.com/knex/knex) from 0.12.9 to 0.20.10.
- [Release notes](https://github.com/knex/knex/releases)
- [Changelog](https://github.com/knex/knex/blob/master/CHANGELOG.md)
- [Commits](https://github.com/knex/knex/commits)

Signed-off-by: dependabot[bot] <support@github.com>
2020-02-23 17:39:06 +00:00
5 changed files with 53 additions and 30 deletions
+1
View File
@@ -36,3 +36,4 @@ jspm_packages
# Optional REPL history
.node_repl_history
.vscode/*
.idea
+1 -1
View File
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "4.4"
- "12"
+1 -1
View File
@@ -78,7 +78,7 @@ function addCondition (q, field, val) {
}
}
}
var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : 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);
}
+1 -1
View File
@@ -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"
}
}
+42 -20
View File
@@ -1,11 +1,16 @@
/* global describe, it */
var assert = require('assert');
var knex = require('knex')({client: 'sqlite' });
var Knex = require('knex');
var knexJsonQuery = require('./');
require('simple-mocha');
var testData = {
mysql:{},
pg:{},
};
var conditions= [
testData.pg.knex = Knex({client: 'pg' });
testData.pg.conditions = [
/* -----------Tests for and conditions---------- */
{
name: 'handle simple and conditions',
@@ -14,23 +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*\')'
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*\')'
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',
@@ -49,13 +54,13 @@ var conditions= [
},
{
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',
@@ -91,23 +96,23 @@ 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)'
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))'
output: 'select * where (("f1" like 10) or ("f2" = 20) or ("f3" = 30))'
},
{
name: 'handle simple or condition with between statement',
@@ -146,8 +151,8 @@ var conditions= [
},
{
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%\'))'
input: { f1: [['ilike', 'awesome'], ['OR_ilike', '%super%'] ] },
output: 'select * where (("f1" ilike \'awesome\' or "f1" ilike \'%super%\'))'
},
{
name: 'handle $or grouping',
@@ -157,6 +162,21 @@ var conditions= [
];
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(){
@@ -166,3 +186,5 @@ describe('SQL query generation from json query', function(){
});
});
})