-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
425 lines (340 loc) · 9.92 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"use strict"
class LiQuery {
table = null
primary = 'id'
search = 'search'
default = null
keywords = {}
aliases = {}
processors = {}
allowed = null
select = ["*"]
count = [`COUNT(${this.primary}) AS count`]
limit = 100
scope = null // sql filter to narrow scope
debug = false
constructor(params={}) {
// configuration
for(const key in params)
this[key] = params[key]
}
createContainer(opts) {
return {
input: null,
query: null,
sql: {
select: null,
count: null,
},
page: opts.page || 1,
pages: function(count) { return Math.ceil(count / this.limit) },
offset: 0,
limit: opts.limit || this.limit,
errors: [],
// internal
parsed: {
query: { // user query
keywords: [],
tags: [],
tagsOr: [],
tagsNot: [],
filters: [],
sort: [],
},
keywords: { // expanded keywords
tags: [],
tagsOr : [],
tagsNot: [],
filters: [],
sort: [],
},
default: { // defaults
tags: [],
tagsOr : [],
tagsNot: [],
filters: [],
sort: [],
}
},
}
}
parse(query, opts={}) {
/** Process supplied input. We can reuse configured instance by passing new query **/
const obj = this.createContainer(opts)
obj.query = query
obj.input = query // store original
// process user query
if(obj.query){
// clean input
obj.query = obj.query
.replace(/\+/g, ' ') // replace + for space, for when it comes from the url
.replace(/\s+/g, ' ') // remove extra whitespace
.trim() // remove leading/trailing spaces
this.processPart(obj, obj.query, obj.parsed.query, this.keywords, true)
}
// expand and process keywords
for(const keyword of obj.parsed.query.keywords)
this.processPart(obj, this.keywords[keyword], obj.parsed.keywords)
// process resource default
if(this.default)
this.processPart(obj, this.default, obj.parsed.default)
// normalize query, put valid parts in right order
this.normalize(obj)
// generate clean, canonical query
this.generateQuery(obj)
// generate sql statements
this.generateSQL(obj, opts)
// drop internal fields
if(!this.debug)
delete obj.parsed
// dump errors, if any
if(this.debug && obj.errors.length)
console.error('LiQuery errors:', obj.errors)
return obj
}
processPart(obj, input, container, keywords={}, dedupe) {
// split to parts and figure their type
let parts = input.split(' ')
// remove duplicates, only for user query
if(dedupe)
parts = parts.filter( (value, index, self) => self.indexOf(value) === index )
// verify that field use is allowed
const useField = field => this.allowed ? (this.allowed.includes(field) || this.aliases[field] ? true : false) : true
// identify parts of the query and put them to relevant buckets
let match = false
for(const part of parts){
// skip empty
if(part == ''){
continue
}
// random sort
else if(part == 'random'){
container.sort.push({order:'random'})
}
// sort type
else if(match = part.match(/([a-z0-9_]+):(asc|desc)/)){
if(useField(match[1]))
container.sort.push({
field: match[1],
order: match[2]
})
else
obj.errors.push(match[1])
}
// filter: range type (numeric only)
else if(match = part.match(/([a-z0-9_]+):([0-9\.]+)-([0-9\.]+)/)){
if(useField(match[1]))
container.filters.push({
field: match[1],
operator: '-',
value: [match[2], match[3]],
})
else
obj.errors.push(match[1])
}
// filter: value type, eqal|not equal
else if(match = part.match(/([a-z0-9_]+)(:|!)([a-z0-9-_/\.,\{\}]+)/)){
if(useField(match[1]))
container.filters.push({
field: match[1],
operator: match[2],
// multivalue?
value: match[3].includes(',') ? match[3].split(',') : match[3],
})
else
obj.errors.push(match[1])
}
// filter: value type, less, greater
else if(match = part.match(/([a-z0-9_]+)(<|>|:<|:>)([a-z0-9-_/\.\{\}]+)/)){ // comma is reserved
if(useField(match[1]))
container.filters.push({
field: match[1],
operator: match[2],
value: match[3],
})
else
obj.errors.push(match[1])
}
// keyword
else if(keywords[part]){
container.keywords.push(part)
}
// tag - or
else if(match = part.match(/\|/)){
// split on | and remove empty elems, in case of bad input
const or_parts = part.split('|').filter(Boolean)
if(or_parts.length > 1)
container.tagsOr.push(or_parts)
else if(or_parts.length > 0)
container.tags.push(or_parts[0])
}
// tag - inclusive or negated
else if(match = part.match(/^(!?)([a-z0-9\.]+)$/)){
if(match[1]){
// negated tag
container.tagsNot.push(match[2])
} else {
// inclusive tag
container.tags.push(match[2])
}
}
// page number
else if(match = part.match(/^@(\d+)$/)){
obj.page = parseInt(match[1])
}
else {
obj.errors.push(part)
}
}
}
normalize(obj) {
/** Sort parsed user query elements of each type in alpha order **/
const query = obj.parsed.query
// sort keywords
query.keywords.sort()
// sort tags
query.tags.sort()
// sort tags OR
for(const tagsOrGroup of query.tagsOr){
tagsOrGroup.sort()
}
// sort tags NOT
query.tagsNot.sort()
// sort filters
query.filters.sort((a,b) => a.field > b.field ? 1 : (b.field > a.field ? -1 : 0) )
}
generateQuery(obj) {
/** Generate clean, normalized query that has parts in right order **/
const query = obj.parsed.query
// add keywords and tags
const clean = [].concat(query.keywords, query.tags)
// tags OR
for(const tagsOr of query.tagsOr)
clean.push(tagsOr.join('|'))
// tags NOT
for(const tagNot of query.tagsNot)
clean.push('!' + tagNot)
// filter
for(const filter of query.filters){
if(Array.isArray(filter.value))
// multivalue or range filter, between
if(filter.operator == '-')
// range
clean.push(`${filter.field}:${filter.value[0]}-${filter.value[1]}`)
else
// multivalue
clean.push(`${filter.field}${filter.operator}${filter.value.join(',')}`)
else
// normal filter eq, gt, lt
clean.push(filter.field + filter.operator + filter.value)
}
// sort
let sorting = []
for(const sort of query.sort) {
if(sort.order == 'random'){
// random sort cancels any other
sorting = ['random']
break;
} else {
sorting.push(sort.field + ':' + sort.order)
}
}
// done
obj.query = [].concat(clean, sorting).join(' ')
}
generateSQL(obj, opts={}) {
/** Generate SELECT and COUNT sql queries **/
const where = []
// add hard scope
if(this.scope)
where.push(this.scope)
// add request scope
if(opts.scope)
where.push(opts.scope)
// TAGS
// - include all from each source
for(const source of ['query', 'keywords', 'default']) {
for(const tagAnd of obj.parsed[source].tags)
where.push(`${this.search} LIKE '%${tagAnd}%'`)
for(const tagOrGroup of obj.parsed[source].tagsOr){
const parts = []
for(const tagOr of tagOrGroup)
parts.push(`${this.search} LIKE '%${tagOr}%'`)
where.push(`(${parts.join(' OR ')})`)
}
for(const tagNot of obj.parsed[source].tagsNot)
where.push(`${this.search} NOT LIKE '%${tagNot}%'`)
}
// FILTER
// - include user query and keywords
// - add default if not overriden by user query
// track applied filters, used to determine if default filter on that field should be included
const activeFilters = {}
for(const source of ['query', 'keywords', 'default']){
for(const filter of obj.parsed[source].filters){
// use real field name, translate alias if it exists
const realName = this.aliases[filter.field] || filter.field
// skip default if already filtered on that field
if(source == 'default' && activeFilters[realName])
continue;
activeFilters[realName] = true
where.push(this.processFilter(realName, filter))
}
}
// SORT
// - only one source, order of precedence: query => keyword => default
let sorting = []
sortLoop: // label for breaking from nested loop
for(const source of ['query', 'keywords', 'default']){
if(!obj.parsed[source].sort.length)
continue;
for(const sort of obj.parsed[source].sort){
if(sort.order == 'random'){
sorting = ['RANDOM()']
// random cancels all other sort methods
break sortLoop;
}
else
// add elem to order clause, mind the alias
sorting.push((this.aliases[sort.field] || sort.field) + ' ' + sort.order.toUpperCase())
}
// only one source of sort is allowed
// if we are here, that means we've got something, and we bolt
break;
}
const sort = sorting.length ? 'ORDER BY ' + sorting.join(', ') : ''
// LIMIT & OFFSET
obj.offset = (obj.page - 1) * obj.limit
const offset = `LIMIT ${obj.limit} OFFSET ${obj.offset}`
// BUILD QUERY
const select = this.select.join(', ')
const count = this.count.join(', ')
const whereStr = where.length ? 'WHERE ' + where.join(' AND ') : ''
obj.sql.select = `SELECT ${select} FROM ${this.table} ${whereStr} ${sort} ${offset}`
obj.sql.count = `SELECT ${count} FROM ${this.table} ${whereStr}`
if(this.debug)
obj.where = where
}
operators = {
':':'=', '<':'<', '>':'>',
'!':'!=', ':<':'<=', ':>':'>=',
}
processFilter(fieldName, filter) {
// get original or processed value, if processor is available
const value = this.processors[fieldName] ? this.processors[fieldName](filter.value) : filter.value
if(Array.isArray(value)) {
// multivalue or range filter
if(filter.operator == '-')
return `${fieldName} BETWEEN ${value[0]} AND ${value[1]}`
else
return `${fieldName} ${filter.operator == ':' ? 'IN' : 'NOT IN'} (${value.map(val => isNaN(val) ? `'${val}'` : val).join(',')})`
} else {
// null value
if(value == 'null')
return fieldName + ' ISNULL'
// normal filter
return `${fieldName} ${this.operators[filter.operator]} ${isNaN(value) ? `'${value}'` : value}`
}
}
}
module.exports = LiQuery