-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
214 lines (173 loc) · 6.67 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
'use strict';
const {
handleBasicComparator,
handleBetweenOperation,
handleInComparator
} = require('./operations');
class SequelizeQS {
#opMaps = {
'$in': handleInComparator,
'$nin': handleInComparator,
'$': handleBasicComparator,
'!': handleBasicComparator,
'|': handleBetweenOperation,
'^': handleBasicComparator,
'~': handleBasicComparator,
'>=': handleBasicComparator,
'>': handleBasicComparator,
'<=': handleBasicComparator,
'<': handleBasicComparator
};
constructor(options) {
// check and configure all the options are okay
this.opt = options || {}
if (!this.opt.ops || !Array.isArray(this.opt.ops)) {
this.opt.ops = Object.keys(this.#opMaps);
}
if (!this.opt.alias || typeof(this.opt.alias) !== 'object') {
this.opt.alias = {};
}
if (!this.opt.blacklist || !Array.isArray(this.opt.blacklist)) {
this.opt.blacklist = [];
}
if (!this.opt.customHandlers || typeof(this.opt.customHandlers) !== 'object') {
this.opt.customHandlers = {};
}
if (!this.opt.defaultPaginationLimit || isNaN(this.opt.defaultPaginationLimit)) {
this.opt.defaultPaginationLimit = 25;
}
if (!this.opt.maximumPageSize || isNaN(this.opt.maximumPageSize)) {
this.opt.maximumPageSize = 100;
}
if (!this.opt.dateFields || !Array.isArray(this.opt.dateFields)) {
this.opt.dateFields = ['createdAt', 'updatedAt'];
}
if (!this.opt.dateOnlyCompare || typeof(this.opt.dateOnlyCompare) !== 'boolean') {
this.opt.dateOnlyCompare = false;
}
}
parse(queryObj) {
const { page, limit, sort, ...rest } = queryObj;
// parse & build the where condition from the query object
const where = this.#parseWhereProperties(rest);
let retval = { where };
// check the ordering
if (sort) {
var order = this.#parseSort(sort);
retval['order'] = order;
}
// check if we are including some kind of pagination
if (page || limit) {
const pagination = this.#parsePagination(page, limit);
retval['offset'] = ((pagination.page - 1) * pagination.limit);
retval['limit'] = pagination.limit;
}
return retval;
}
#parsePagination(page, limit) {
const { defaultPaginationLimit, maximumPageSize } = this.opt;
let realPage = page || 1;
let realLimit = limit || defaultPaginationLimit;
// prevent negative page number
try {
realPage = parseInt(page);
if (isNaN(realPage) || realPage < 1) {
realPage = 1;
}
}
catch {
realPage = 1;
}
// prevent negative limit value or trying to get too many
try {
realLimit = parseInt(limit);
if (isNaN(realLimit) || realLimit < 1 || (maximumPageSize && realLimit > maximumPageSize)) {
realLimit = defaultPaginationLimit;
}
}
catch {
realLimit = defaultPaginationLimit;
}
return { page: realPage, limit: realLimit };
}
#parseSort(sort) {
let retval = [];
let aSort = sort.split('|');
for (var i = 0; i < aSort.length; i++) {
console.log('aSort', aSort[i]);
let value = aSort[i];
let direction = value.startsWith('!') ? 'DESC' : 'ASC';
if (value.startsWith('!')) {
retval = [ ...retval, [ value.substring(1), direction ] ];
}
else {
retval = [ ...retval, [ value, direction ] ];
}
}
return retval;
}
#parseWhereProperties(properties) {
let where = {};
let columns = Object.keys(properties);
const { alias, blacklist, customHandlers, ops } = this.opt;
for (const column of columns) {
let key = alias[column] || column; // handles an alias
let value = properties[column];
// check the blacklist
if (blacklist.some(el => el === key)) {
break;
}
// TODO: do we need to check for whitelist & let more dangerous query run?
// handles a custom function when we want to override the base functionality
if (customHandlers[key] && typeof(customHandlers[key]) === 'function') {
// call the custom handler function and get the operation to apply
let customOperation = customHandlers[key](key, value, this.opt);
where = {
...where,
...customOperation
};
break;
}
// handle when no value is supplied (e.g. `?maxAge=`)
if (!value || value.length === 0 || (typeof(value) === 'string' && value.toLowerCase() === 'null')) {
let { operation } = handleBasicComparator('is', key, null, this.opt);
where = {
...where,
[key]: operation
};
break;
}
// handle when a NOT value is supplied (so when not null sort of a thing) (e.g. `?maxAge=!` or `?maxAge=!null`)
if (typeof(value) === 'string' && (value === '!' || value.toLowerCase() === '!null')) {
let { operation } = handleBasicComparator('is not', key, null, this.opt);
where = {
...where,
[key]: operation
};
break;
}
// check if we have an operator to handle a special type (not a basic `=` equals basically)
if (typeof(value) === 'string' && ops.some(op => value.startsWith(op))) {
let op = ops.find(op => value.startsWith(op));
let fn = this.#opMaps[op];
if (!fn || !typeof (fn) === 'function') {
throw 'Operation is not a function';
}
let { operation } = fn(op, key, value, this.opt);
where = {
...where,
[key]: operation
};
break;
}
// finally, we will just default to the basic equals operation
let { operation } = handleBasicComparator('=', key, value, this.opt);
where = {
...where,
[key]: operation
};
}
return where;
}
}
module.exports = SequelizeQS;