nodester 0.4.8 → 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/query/traverse/index.js +1 -1
- package/lib/structures/Filter.js +17 -3
- package/package.json +1 -1
- package/lib/utils/queries.util.js +0 -240
|
@@ -345,7 +345,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
|
|
|
345
345
|
|
|
346
346
|
// Combine included orders into one at the top level:
|
|
347
347
|
// - Why?
|
|
348
|
-
// - Sequelize
|
|
348
|
+
// - Sequelize ignores included orders for association types like:
|
|
349
349
|
// • HasMany
|
|
350
350
|
_traverseIncludedOrders(newQuery, _model);
|
|
351
351
|
|
package/lib/structures/Filter.js
CHANGED
|
@@ -91,6 +91,7 @@ module.exports = class NodesterFilter {
|
|
|
91
91
|
const { associations } = this.model;
|
|
92
92
|
for (const [ includeName, includeFilter ] of Object.entries(includes)) {
|
|
93
93
|
const association = associations[includeName];
|
|
94
|
+
|
|
94
95
|
// Validate association by name:
|
|
95
96
|
if (association === undefined) {
|
|
96
97
|
const error = new TypeError(`No include named '${ includeName }'.`);
|
|
@@ -98,10 +99,23 @@ module.exports = class NodesterFilter {
|
|
|
98
99
|
throw error;
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
const { associationType } = association;
|
|
103
|
+
|
|
101
104
|
// If singular association:
|
|
102
|
-
// Fix of "Only HasMany associations support include.separate"
|
|
103
|
-
if ('HasMany' !==
|
|
104
|
-
// Empty bounds
|
|
105
|
+
// (Fix of sequilize error "Only HasMany associations support include.separate")
|
|
106
|
+
if ('HasMany' !== associationType) {
|
|
107
|
+
// Empty bounds:
|
|
108
|
+
if (!!includeFilter.statics.clauses.limit) {
|
|
109
|
+
const msg = [
|
|
110
|
+
`(nodester) warning: include "${ includeName }" has`,
|
|
111
|
+
`association type of "${ associationType }", but has a filter clause "limit",`,
|
|
112
|
+
`which is forbidden on any association type except for "HasMany".`,
|
|
113
|
+
`It was automatically removed from clauses.`
|
|
114
|
+
].join(' ');
|
|
115
|
+
console.warn(msg);
|
|
116
|
+
}
|
|
117
|
+
delete includeFilter.statics.clauses.limit;
|
|
118
|
+
|
|
105
119
|
includeFilter.noBounds = true;
|
|
106
120
|
}
|
|
107
121
|
|
package/package.json
CHANGED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
// Constants.
|
|
2
|
-
const SubIncludesQueryRegex = /\([^)]*\)/g;
|
|
3
|
-
|
|
4
|
-
// Sequelize.
|
|
5
|
-
const Op = require('sequelize').Op;
|
|
6
|
-
|
|
7
|
-
// Utils.
|
|
8
|
-
const {
|
|
9
|
-
splitByDot,
|
|
10
|
-
splitByAmpersand
|
|
11
|
-
} = require('nodester/utils/strings.util');
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
module.exports = {
|
|
15
|
-
parseQueryParams: _parseQueryParams,
|
|
16
|
-
deleteQuerySortParams: _deleteQuerySortParams,
|
|
17
|
-
|
|
18
|
-
// SubIncludes Query:
|
|
19
|
-
hasSubIncludesQuery: _hasSubIncludesQuery,
|
|
20
|
-
cutSubIncludesQuery: _cutSubIncludesQuery,
|
|
21
|
-
parseSubIncludesQuery: _parseSubIncludesQuery
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function _parseQueryParams(
|
|
25
|
-
requestQueryObject={},
|
|
26
|
-
sequilizeQuery=null
|
|
27
|
-
) {
|
|
28
|
-
const skip = parseInt(requestQueryObject?.skip ?? 0);
|
|
29
|
-
const limit = parseInt(requestQueryObject?.limit ?? 50);
|
|
30
|
-
|
|
31
|
-
sequilizeQuery.offset = skip;
|
|
32
|
-
sequilizeQuery.limit = limit;
|
|
33
|
-
|
|
34
|
-
const order = requestQueryObject?.order;
|
|
35
|
-
|
|
36
|
-
// If order is set:
|
|
37
|
-
if (!!order) {
|
|
38
|
-
const orderBy = requestQueryObject?.order_by ?? 'id';
|
|
39
|
-
|
|
40
|
-
sequilizeQuery.order = [
|
|
41
|
-
[ orderBy, order ]
|
|
42
|
-
];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Clear sort params.
|
|
46
|
-
_deleteQuerySortParams(requestQueryObject);
|
|
47
|
-
|
|
48
|
-
// Get include names.
|
|
49
|
-
const _includes = sequilizeQuery?.include?.map( include => include.association ) ?? [];
|
|
50
|
-
|
|
51
|
-
// Count query keys.
|
|
52
|
-
const keysCount = Object.keys(requestQueryObject).length;
|
|
53
|
-
|
|
54
|
-
// If query has no keys,
|
|
55
|
-
// stop further execution:
|
|
56
|
-
if (keysCount === 0) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// This container is a reference to current "where".
|
|
61
|
-
let container = null;
|
|
62
|
-
let isContainerArray = false;
|
|
63
|
-
|
|
64
|
-
// If query has only 1 key:
|
|
65
|
-
if (keysCount === 1) {
|
|
66
|
-
// Define empty query's where.
|
|
67
|
-
sequilizeQuery.where = {};
|
|
68
|
-
container = sequilizeQuery.where;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
// Define conjuction of params in query's where.
|
|
72
|
-
sequilizeQuery.where = {
|
|
73
|
-
[Op.and]: []
|
|
74
|
-
};
|
|
75
|
-
container = sequilizeQuery.where[Op.and];
|
|
76
|
-
isContainerArray = true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Go through query's keys:
|
|
80
|
-
Object.keys(requestQueryObject)
|
|
81
|
-
.forEach((queryKey) => {
|
|
82
|
-
|
|
83
|
-
// Parse value of this key:
|
|
84
|
-
const value = requestQueryObject[queryKey];
|
|
85
|
-
// If value is not a number, parse it further.
|
|
86
|
-
const parsedValue = isNaN( value ) ? _parseHTTPQueryValue( `${ requestQueryObject[queryKey] }` ) : value;
|
|
87
|
-
|
|
88
|
-
// If we got nested key:
|
|
89
|
-
if (queryKey.indexOf('.') !== -1) {
|
|
90
|
-
|
|
91
|
-
// If this key is included as association:
|
|
92
|
-
const associationIndex = _includes.indexOf(queryKey.split('.')[0]);
|
|
93
|
-
if (associationIndex > -1) {
|
|
94
|
-
sequilizeQuery.include[associationIndex].where = { [`$${queryKey}$`]: parsedValue };
|
|
95
|
-
}
|
|
96
|
-
// Use special sequelize syntax for nested SELECT:
|
|
97
|
-
else if (isContainerArray) {
|
|
98
|
-
const selectObject = { [`$${queryKey}$`]: parsedValue };
|
|
99
|
-
container.push(selectObject);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
container[`$${queryKey}$`] = parsedValue;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// On regular key, set regular key-value pair:
|
|
106
|
-
else {
|
|
107
|
-
if (isContainerArray) {
|
|
108
|
-
const selectObject = { [queryKey]: parsedValue };
|
|
109
|
-
container.push(selectObject);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
container[queryKey] = parsedValue;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function _parseHTTPQueryValue(
|
|
119
|
-
value='',
|
|
120
|
-
isNumber=false
|
|
121
|
-
) {
|
|
122
|
-
// If value matches "and()":
|
|
123
|
-
if (value.slice(0, 4) === 'and(') {
|
|
124
|
-
// Remove "and()".
|
|
125
|
-
const clearValuesString = value.substr(4, value.length - 'and()'.length);
|
|
126
|
-
|
|
127
|
-
const clearValues = clearValuesString.split(',');
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
[Op.and]: clearValues.map(cv => _parseHTTPQueryValue(cv))
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// If value matches "like(value)":
|
|
134
|
-
else if (value.slice(0, 5) === 'like(') {
|
|
135
|
-
// Remove "like()".
|
|
136
|
-
const clearValue = value.substr(5, value.length - 'like()'.length);
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
[Op.like]: `%${ _parseHTTPQueryValue(clearValue) }%`
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// If value matches "notLike(value)":
|
|
143
|
-
else if (value.slice(0, 8) === 'notLike(') {
|
|
144
|
-
// Remove "notLike()".
|
|
145
|
-
const clearValue = value.substr(8, value.length - 'notLike()'.length);
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
[Op.notLike]: `%${ _parseHTTPQueryValue(clearValue) }%`
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
// If value matches "not(value)":
|
|
152
|
-
else if (value.slice(0, 4) === 'not(') {
|
|
153
|
-
// Remove "not()".
|
|
154
|
-
const clearValue = value.substr(4, value.length - 'not()'.length);
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
[Op.not]: _parseHTTPQueryValue(clearValue)
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
// If value matches "or()":
|
|
161
|
-
else if (value.slice(0, 3) === 'or(') {
|
|
162
|
-
// Remove "or()".
|
|
163
|
-
const clearValuesString = value.substr(3, value.length - 'or()'.length);
|
|
164
|
-
|
|
165
|
-
const clearValues = clearValuesString.split(',');
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
[Op.or]: clearValues.map(cv => _parseHTTPQueryValue(cv))
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
// If value is a number:
|
|
172
|
-
// else if (!isNaN(value)) {
|
|
173
|
-
// const number = parseFloat(value);
|
|
174
|
-
// return number;
|
|
175
|
-
// }
|
|
176
|
-
// For default, just set this value in "where"
|
|
177
|
-
return value;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function _deleteQuerySortParams(requestQueryObject={}) {
|
|
181
|
-
delete requestQueryObject.skip;
|
|
182
|
-
delete requestQueryObject.limit;
|
|
183
|
-
delete requestQueryObject.order;
|
|
184
|
-
delete requestQueryObject.order_by;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// If string has nested query "()":
|
|
188
|
-
function _hasSubIncludesQuery(string='') {
|
|
189
|
-
return SubIncludesQueryRegex.test(string);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function _cutSubIncludesQuery(string='') {
|
|
193
|
-
const query = string.match(SubIncludesQueryRegex)[0];
|
|
194
|
-
const newString = string.replace(SubIncludesQueryRegex, '');
|
|
195
|
-
|
|
196
|
-
return [ query, newString ];
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function _parseSubIncludesQuery(
|
|
200
|
-
associationName,
|
|
201
|
-
subIncludesQuery='',
|
|
202
|
-
sequilizeQuery=null
|
|
203
|
-
) {
|
|
204
|
-
const query = {};
|
|
205
|
-
|
|
206
|
-
// Magic trick to make everything work (DO NOT REMOVE).
|
|
207
|
-
sequilizeQuery.separate = true;
|
|
208
|
-
|
|
209
|
-
// If first "("" & ")" are set, cut them:
|
|
210
|
-
const subIncludesQueryRegex = /\([^)]*\)/g;
|
|
211
|
-
const clearQuery = subIncludesQueryRegex.test(subIncludesQuery) ?
|
|
212
|
-
subIncludesQuery.substr(1, subIncludesQuery.length - 2)
|
|
213
|
-
:
|
|
214
|
-
subIncludesQuery;
|
|
215
|
-
|
|
216
|
-
const keyValues = splitByAmpersand(clearQuery).map(kv => {
|
|
217
|
-
const keyValue = kv.split('=');
|
|
218
|
-
query[keyValue[0]] = keyValue[1];
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (!!query.skip) {
|
|
223
|
-
const skip = parseInt(query.skip ?? 0);
|
|
224
|
-
sequilizeQuery[`$${ associationName }.offset$`] = skip;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (!!query.limit) {
|
|
228
|
-
const limit = parseInt(query.limit ?? 50);
|
|
229
|
-
sequilizeQuery[`$${ associationName }.limit$`] = limit;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// If order is set:
|
|
233
|
-
if (!!query.order) {
|
|
234
|
-
const orderBy = query?.order_by ?? 'id';
|
|
235
|
-
|
|
236
|
-
sequilizeQuery.order = [
|
|
237
|
-
[ orderBy, query.order ]
|
|
238
|
-
];
|
|
239
|
-
}
|
|
240
|
-
}
|