-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsequelizeQueryParser.js
280 lines (244 loc) · 8.85 KB
/
sequelizeQueryParser.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
/*
* File: sequelizeQueryParser.js
* File Created: Wednesday, 17th October 2018 12:18:11 pm
* Author: Twaha Mukammel (t.mukammel@gmail.com)
*/
'use strict';
const Promise = require('bluebird');
/**
* Pass `db` connection object which has `Sequelize.Op`
* @param {*} db
* @returns object {parse}
*/
module.exports = (db) => {
const Op = db.Sequelize.Op;
const operators = {
gt: Op.gt,
gte: Op.gte,
lt: Op.lt,
lte: Op.lte,
ne: Op.ne,
eq: Op.eq,
not: Op.not,
like: Op.like, // LIKE '%hat'
notLike: Op.notLike, // NOT LIKE '%hat'
// iLike: Op.iLike, // ILIKE '%hat' (case insensitive) (PG only)
// notILike: Op.notILike, // NOT ILIKE '%hat' (PG only)
regexp: Op.regexp, // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
notRegexp: Op.notRegexp, // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
// iRegexp: Op.iRegexp, // ~* '^[h|a|t]' (PG only)
// notIRegexp: Op.notIRegexp // !~* '^[h|a|t]' (PG only)
and: Op.and, // AND (a = 5)
or: Op.or, // (a = 5 OR a = 6)
between: Op.between, // BETWEEN 6 AND 10
notBetween: Op.notBetween, // NOT BETWEEN 11 AND 15
in: Op.in, // IN [1, 2]
notIn: Op.notIn // NOT IN [1, 2]
// overlap: Op.overlap, // && [1, 2] (PG array overlap operator)
// contains: Op.contains, // @> [1, 2] (PG array contains operator)
// contained: Op.contained, // <@ [1, 2] (PG array contained by operator)
// col: Op.col // = "user"."organization_id", with dialect specific column identifiers, PG in this example
// any: Op.any // ANY ARRAY[2, 3]::INTEGER (PG only)
};
/**
* Split '.' or ',' seperated strings to array
* @param {JSON} obj
* @param {array} array
*/
const splitStringAndBuildArray = (obj, array) => {
const elements = obj.split(',');
elements.forEach((element) => {
const fields = element.split('.');
if (fields && fields.length > 0) {
array.push(fields);
}
});
};
/**
* Parse query params
* @param {string|Array} query
* @returns {Array} sequelize formatted DB query array
*/
const parseFields = (query) => {
let array = null;
if (query !== null) {
array = [];
if (Array.isArray(query) == true) {
query.forEach((obj) => {
splitStringAndBuildArray(obj, array);
});
} else {
splitStringAndBuildArray(query, array);
}
}
return array;
};
/**
* Replaces operator (json object key) with Sequelize operator.
* @param {JSON} json
* @param {string} key
* @param {Sequelize.op} op
*/
const replaceKeyWithOperator = (json, key, op) => {
const value = json[key];
delete json[key];
json[op] = value;
};
/**
* Iteratively replace json keys with Sequelize formatted query operators.
* @param {JSON} json next json
*/
const iterativeReplace = (json) => {
Object.keys(json).forEach((key) => {
if (json[key] !== null && typeof json[key] === 'object') {
const op = operators[key];
if (op) {
replaceKeyWithOperator(json, key, op);
iterativeReplace(json[op]);
} else {
iterativeReplace(json[key]);
}
} else if (key == 'model' && db[json[key]] != null) {
json.model = db[json[key]];
} else {
const op = operators[key];
if (op) replaceKeyWithOperator(json, key, op);
}
});
};
/**
* Unescape escaped sequences in string.
* @param {*} query string with escape sequence
* @returns {string} unescaped string
*/
const unescapeEscapedQuery = (query) => {
const queryString = query.toString();
return unescape(queryString);
};
/**
* Parse and build Sequelize format query
* @param {JSON} query
* @returns {JSON} sequelize formatted DB query params JSON
*/
const parseQueryFields = (query) => {
const json = JSON.parse(unescapeEscapedQuery(query));
iterativeReplace(json);
return json;
};
/**
* Parse and build Sequelize format query
* @param {JSON} query
* @returns {JSON} sequelize formatted DB include params JSON
*/
const parseIncludeFields = (query) => {
const json = JSON.parse(unescapeEscapedQuery(query));
iterativeReplace(json);
return json;
};
/**
* Parse single query parameter
* @param {string} query
* @returns {string|JSON} sequelize formatted DB query param
*/
const parseQueryParam = (query) => {
const elements = query.split(/:(.+)/);
if (elements?.length > 1) {
const param = {};
const elementsArray = elements[1].split(',');
if (elementsArray) {
param[operators[elements[0]]] = elementsArray.length > 1 ? elementsArray : elementsArray[0];
return param;
}
}
return elements[0];
};
// Max page size limit is set to 200
const pageSizeLimit = 200;
/**
* Sequelize Query Parser is a very simple package that
* turns your RESTful APIs into a basic set of Graph APIs.
*
* Parses - filter, query, sorting, paging, group, relational object queries
*
* fields=field01,field02...
*
* limit=value&&offset=value
*
* sort_by=field01.asc|field02.desc
*
* group_by=field01,field02
*
* query=JSON
*
* include=JSON
*
* filedName=unaryOperator:value
*
* @param {JSON} req
* @returns {JSON} sequelize formatted DB query
*/
function parse(req) {
console.debug('Request query: ', req.query);
return new Promise((resolve, reject) => {
try {
let offset = 0;
let limit = pageSizeLimit;
const dbQuery = {
where: {},
offset,
limit
};
for (const key in req.query) {
switch (key) {
// Fields
case 'fields':
// split the field names (attributes) and assign to an array
const fields = req.query.fields.split(',');
// assign fields array to .attributes
if (fields && fields.length > 0) dbQuery.attributes = fields;
break;
// pagination page size
case 'limit':
dbQuery.limit = Math.min(Math.max(parseInt(req.query.limit), 1), pageSizeLimit);
limit = dbQuery.limit;
break;
// pagination page offset
case 'offset':
offset = Math.max(parseInt(req.query.offset), 0);
break;
// Sort by field order
case 'sort_by':
dbQuery.order = parseFields(req.query.sort_by);
break;
// Group by field
// TODO: Check array
case 'group_by':
dbQuery.group = parseFields(req.query.group_by);
break;
// JSON (nested) query
case 'query':
const parsed = parseQueryFields(req.query.query);
dbQuery.where = { ...dbQuery.where, ...parsed };
break;
// include and query on associated tables
case 'include':
dbQuery.include = parseIncludeFields(req.query.include);
break;
// Simple filter query
default:
dbQuery.where[key] = parseQueryParam(req.query[key]);
break;
}
}
dbQuery.offset = offset * limit;
console.debug('Final sequelize query:');
console.debug(JSON.stringify(dbQuery, null, 4));
resolve(dbQuery);
} catch (error) {
console.debug('Error: ', error.message);
reject([{ msg: error.message }]);
}
});
}
return { parse };
};