Skip to content

Commit eee8f1c

Browse files
committed
* Added documentation
* Release V 1.0.0
1 parent 53058d5 commit eee8f1c

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

Diff for: README.md

+145
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,148 @@
11
[![Build Status](https://travis-ci.org/harish2704/knex-json-query.svg?branch=master)](https://travis-ci.org/harish2704/knex-json-query)
22
# knex-json-query
33
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+

Diff for: index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
1+
"use strict";
32

43
var functionOperatorMap = {
54
BETWEEN: 'whereBetween',

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "knex-json-query",
3-
"version": "0.0.5",
3+
"version": "1.0.0",
44
"description": "A high-level utility which will will generate Knex query from a single JSON object.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)