forked from thomas4019/mongo-query-to-postgres-jsonb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
198 lines (181 loc) · 8.22 KB
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var assert = require('chai').assert
var convert = require('../index')
describe('string equality', function () {
it('should use ->>', function () {
assert.equal('data->>\'name\'=\'thomas\'', convert('data', {name: 'thomas'}))
})
it('should work with multiple', function () {
assert.equal('(data->>\'a\'=\'a\' and data->>\'b\'=\'b\')', convert('data', {a: 'a', b: 'b'}))
})
it('nesting does exact document matching', function() {
assert.equal('data->\'test\'=\'{"cat":{"name":"oscar"}}\'::jsonb', convert('data', {test: {cat: {name: 'oscar'}} }))
})
it('should support nesting using the dot operator', function() {
assert.equal('data->\'test\'->\'cat\'->>\'name\'=\'oscar\'', convert('data', {'test.cat.name': 'oscar'}))
})
})
describe('array equality', function () {
it('should use =', function () {
assert.equal('data->\'roles\'=\'["Admin"]\'::jsonb', convert('data', {'roles': ['Admin']}))
})
it('should matching numeric indexes', function() {
assert.equal('data->\'roles\'->>0=\'Admin\'', convert('data', {'roles.0': 'Admin'}))
})
it('support element matching', function() {
assert.equal('data->>\'roles\'=\'Admin\'', convert('data', {'roles': {$elemMatch: 'Admin'}}))
})
})
describe('boolean equality', function () {
it('should use ->', function () {
assert.equal('data->\'hidden\'=\'false\'::jsonb', convert('data', {'hidden': false}))
})
})
describe('number equality', function () {
it('should use ->', function () {
assert.equal('data->\'age\'=\'5\'::jsonb', convert('data', {'age': 5}))
})
})
describe('$or', function () {
it('errors with no parameters', function () {
assert.throws(() => convert('data', { $or: [] }), '$and/$or/$nor must be a nonempty array')
})
it('work with one parameter', function () {
assert.equal('(data->>\'name\'=\'thomas\')', convert('data', {$or: [{name: 'thomas'}]}))
})
it('work with two parameters', function () {
assert.equal('(data->>\'name\'=\'thomas\' OR data->>\'name\'=\'hansen\')', convert('data', {$or: [{name: 'thomas'}, {name: 'hansen'}]}))
})
})
describe('$nor', function () {
it('work with two parameters', function () {
assert.equal('((NOT data->>\'name\'=\'thomas\') AND (NOT data->>\'name\'=\'hansen\'))', convert('data', { $nor: [{ name: 'thomas' }, { name: 'hansen' }] }))
})
})
describe('$and', function () {
it('errors with no parameters', function () {
assert.throws(() => convert('data', { $and: [] }), '$and/$or/$nor must be a nonempty array')
})
it('work with one parameter', function () {
assert.equal('(data->>\'name\'=\'thomas\')', convert('data', {$and: [{name: 'thomas'}]}))
})
it('work with two parameters', function () {
assert.equal('(data->>\'name\'=\'thomas\' AND data->>\'name\'=\'hansen\')', convert('data', {$and: [{name: 'thomas'}, {name: 'hansen'}]}))
})
it('should work implicitly', function () {
assert.equal('(data->>\'type\'=\'food\' and data->\'price\'<\'9.95\'::jsonb)', convert('data', { type: 'food', price: { $lt: 9.95 } }))
})
})
describe('$in', function () {
it('should work with strings', function () {
assert.equal('data->>\'type\' IN (\'food\', \'snacks\')', convert('data', { type: { $in: [ 'food', 'snacks' ] } }))
})
it('should work with numbers', function () {
assert.equal('data->\'count\' IN (\'1\'::jsonb, \'5\'::jsonb)', convert('data', { count: { $in: [ 1, 5 ] } }))
})
})
describe('$nin', function () {
it('should work with strings', function () {
assert.equal('data->>\'type\' NOT IN (\'food\', \'snacks\')', convert('data', { type: { $nin: [ 'food', 'snacks' ] } }))
})
it('should work with numbers', function () {
assert.equal('data->\'count\' NOT IN (\'1\'::jsonb, \'5\'::jsonb)', convert('data', { count: { $nin: [ 1, 5 ] } }))
})
})
describe('$not', function () {
it('should add NOT and wrap in paratheses', function () {
assert.equal('(NOT data->>\'name\' IN (\'thomas\', \'test\'))', convert('data', { name: { $not : {$in: ['thomas', 'test'] } } }))
})
xit('should use != for string comparison', function () {
assert.equal('data->>\'name\'!=\'thomas\'', convert('data', { $not : {name: 'thomas'} }))
})
})
describe('comparision operators', function() {
it('$eq', function () {
assert.equal('data->>\'type\'=\'food\'', convert('data', { type: { $eq : 'food' } }))
})
it('$ne', function () {
assert.equal('data->>\'type\' IS DISTINCT FROM \'food\'', convert('data', { type: { $ne : 'food' } }))
})
it('$gt', function () {
assert.equal('data->\'count\'>\'5\'::jsonb', convert('data', { count: { $gt : 5 } }))
})
it('$gte', function () {
assert.equal('data->\'count\'>=\'5\'::jsonb', convert('data', { count: { $gte : 5 } }))
})
it('$lt', function () {
assert.equal('data->\'count\'<\'5\'::jsonb', convert('data', { count: { $lt : 5 } }))
})
it('$lte', function () {
assert.equal('data->\'count\'<=\'5\'::jsonb', convert('data', { count: { $lte : 5 } }))
})
})
describe('regular expressions', function() {
it('basic', function () {
assert.equal('data->>\'type\' ~ \'(?p)food\'', convert('data', { type: { $regex : 'food' } }))
})
it('case insensitive', function () {
assert.equal('data->>\'type\' ~* \'(?p)food\'', convert('data', { type: { $regex : 'food', $options: 'i' } }))
})
it('js regex', function () {
assert.equal('data->>\'type\' ~ \'food\'', convert('data', { type: /food/ }))
})
it('make dot match multiline', function () {
assert.equal('data->>\'type\' ~* \'food\'', convert('data', { type: { $regex : 'food', $options: 'si' } }))
})
})
describe('combined tests', function () {
it('should handle ANDs and ORs together', function() {
assert.equal('(data->>\'type\'=\'food\' and (data->\'qty\'>\'100\'::jsonb OR data->\'price\'<\'9.95\'::jsonb))', convert('data', {
type: 'food',
$or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
}))
})
it('should add NOT and wrap in paratheses', function () {
assert.equal('(data->>\'city\'=\'provo\' and data->\'pop\'>\'1000\'::jsonb)', convert('data', {city: 'provo', pop : { $gt : 1000 } }))
})
})
describe('$size', function () {
it('match array sizes', function() {
assert.equal('jsonb_array_length(data->\'arr\')=3', convert('data', { arr: { $size: 3 } }))
})
it('fail for strings', function() {
assert.throws(() => convert('data', { arr: { $size: 'abc' } }), '$size only supports positive integer')
})
it('fail for decimals', function() {
assert.throws(() => convert('data', { arr: { $size: 3.5 } }), '$size only supports positive integer')
})
})
describe('$type', function () {
it('match strings', function() {
assert.equal('jsonb_typeof(data->\'var\')=\'string\'', convert('data', { var: { $type : 'string' } }))
})
})
describe('$exists', function () {
it('work at top level', function() {
assert.equal('data ? \'name\'', convert('data', { name: { $exists: true } }))
})
it('with dot paths', function() {
assert.equal('data->\'name\' ? \'first\'', convert('data', { 'name.first': { $exists: true } }))
})
})
describe('$mod', function () {
it('basic support', function() /**/{
assert.equal('cast(data->>\'age\' AS numeric) % 2=1', convert('data', { age: { $mod: [2, 1] } }))
})
})
describe('Match a Field Without Specifying Array Index', function () {
it('basic case', function() {
assert.equal("(data->'courses'->>'distance'='5K' OR EXISTS (SELECT * FROM jsonb_array_elements(data->'courses') WHERE jsonb_typeof(data->'courses')='array' AND value->>'distance'='5K'))", convert('data', { 'courses.distance': '5K' }, ['courses']))
})
it('direct match', function() {
assert.equal('(data->>\'roles\'=\'Admin\' OR EXISTS (SELECT * FROM jsonb_array_elements(data->\'roles\') WHERE jsonb_typeof(data->\'roles\')=\'array\' AND value #>>\'{}\'=\'Admin\'))', convert('data', { 'roles': 'Admin' }, ['roles']))
})
it('$in', function() {
assert.equal("(data->>'roles' IN ('Test', 'Admin') OR EXISTS (SELECT * FROM jsonb_array_elements(data->'roles') WHERE jsonb_typeof(data->'roles')='array' AND value #>>'{}' IN ('Test', 'Admin')))", convert('data', { 'roles': { $in: ["Test", "Admin"] } }, ['roles']))
})
})
describe('special cases', function () {
it('should return true when passed no parameters', function() {
assert.equal('TRUE', convert('data', {}))
})
})