nodester 0.4.8 → 0.5.0
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/application/index.js +14 -5
- package/lib/http/request/index.js +0 -3
- package/lib/loggers/console.js +17 -2
- package/lib/query/traverse/index.js +1 -1
- package/lib/structures/Filter.js +21 -3
- package/package.json +4 -8
- package/tests/index.test.js +4 -4
- package/lib/utils/queries.util.js +0 -240
package/lib/application/index.js
CHANGED
|
@@ -478,17 +478,26 @@ module.exports = class NodesterApplication extends Emitter {
|
|
|
478
478
|
/**
|
|
479
479
|
* Stops server
|
|
480
480
|
*
|
|
481
|
+
* @param {Function} callback
|
|
482
|
+
*
|
|
481
483
|
* @access public
|
|
482
484
|
*/
|
|
483
|
-
stop() {
|
|
485
|
+
stop(callback) {
|
|
484
486
|
if (this._isListening !== true) {
|
|
485
|
-
|
|
487
|
+
consl.warn('nothing to stop. Server is not listening.');
|
|
486
488
|
return;
|
|
487
489
|
}
|
|
488
490
|
|
|
489
|
-
this.server.close()
|
|
490
|
-
|
|
491
|
+
this.server.close(() => {
|
|
492
|
+
this._isListening = false;
|
|
493
|
+
this._router.unlock();
|
|
491
494
|
|
|
492
|
-
|
|
495
|
+
if (typeof callback === 'function') {
|
|
496
|
+
callback();
|
|
497
|
+
}
|
|
498
|
+
else if (!!callback) {
|
|
499
|
+
consl.warn(`argument 'callback' in stop() must be of type: function.`);
|
|
500
|
+
}
|
|
501
|
+
});
|
|
493
502
|
}
|
|
494
503
|
}
|
|
@@ -8,11 +8,8 @@
|
|
|
8
8
|
const { IncomingMessage } = require('http');
|
|
9
9
|
|
|
10
10
|
const isIP = require('net').isIP;
|
|
11
|
-
const typeis = require('type-is');
|
|
12
11
|
const fresh = require('fresh');
|
|
13
|
-
const parseRange = require('range-parser');
|
|
14
12
|
const parse = require('parseurl');
|
|
15
|
-
const proxyaddr = require('proxy-addr');
|
|
16
13
|
|
|
17
14
|
const { defineGetter } = require('./utils');
|
|
18
15
|
|
package/lib/loggers/console.js
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
exports = module.exports = {
|
|
10
|
-
error: _error
|
|
10
|
+
error: _error,
|
|
11
|
+
warn: _warn
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -19,5 +20,19 @@ exports = module.exports = {
|
|
|
19
20
|
* @access public
|
|
20
21
|
*/
|
|
21
22
|
function _error(err) {
|
|
22
|
-
|
|
23
|
+
console.error(err.stack || err.toString());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Log warning with nodester prefix using console.warning.
|
|
29
|
+
*
|
|
30
|
+
* @param {Error} err
|
|
31
|
+
*
|
|
32
|
+
* @alias error
|
|
33
|
+
* @access public
|
|
34
|
+
*/
|
|
35
|
+
function _warn(...args) {
|
|
36
|
+
const prefix = '[nodester] warning:';
|
|
37
|
+
console.warn(prefix, ...args);
|
|
23
38
|
}
|
|
@@ -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
|
@@ -11,6 +11,9 @@ const CLAUSES = require('../constants/Clauses');
|
|
|
11
11
|
const { isModel } = require('../utils/models');
|
|
12
12
|
const { ensure } = require('nodester/validators/arguments');
|
|
13
13
|
|
|
14
|
+
// Console:
|
|
15
|
+
const consl = require('nodester/loggers/console');
|
|
16
|
+
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* @class
|
|
@@ -91,6 +94,7 @@ module.exports = class NodesterFilter {
|
|
|
91
94
|
const { associations } = this.model;
|
|
92
95
|
for (const [ includeName, includeFilter ] of Object.entries(includes)) {
|
|
93
96
|
const association = associations[includeName];
|
|
97
|
+
|
|
94
98
|
// Validate association by name:
|
|
95
99
|
if (association === undefined) {
|
|
96
100
|
const error = new TypeError(`No include named '${ includeName }'.`);
|
|
@@ -98,10 +102,24 @@ module.exports = class NodesterFilter {
|
|
|
98
102
|
throw error;
|
|
99
103
|
}
|
|
100
104
|
|
|
105
|
+
const { associationType } = association;
|
|
106
|
+
|
|
101
107
|
// If singular association:
|
|
102
|
-
// Fix of "Only HasMany associations support include.separate"
|
|
103
|
-
if ('HasMany' !==
|
|
104
|
-
// Empty bounds
|
|
108
|
+
// (Fix of sequilize error "Only HasMany associations support include.separate")
|
|
109
|
+
if ('HasMany' !== associationType) {
|
|
110
|
+
// Empty bounds:
|
|
111
|
+
if (!!includeFilter.statics.clauses.limit) {
|
|
112
|
+
const msg = [
|
|
113
|
+
`include "${ includeName }" has association type of`,
|
|
114
|
+
`"${ associationType }", but has a filter clause "limit",`,
|
|
115
|
+
`which is forbidden on any association type except for "HasMany".`,
|
|
116
|
+
`It was automatically removed from clauses.`,
|
|
117
|
+
`Consider also removing it from your code.`
|
|
118
|
+
].join(' ');
|
|
119
|
+
consl.warn(msg);
|
|
120
|
+
}
|
|
121
|
+
delete includeFilter.statics.clauses.limit;
|
|
122
|
+
|
|
105
123
|
includeFilter.noBounds = true;
|
|
106
124
|
}
|
|
107
125
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodester",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A versatile REST framework for Node.js",
|
|
5
5
|
"directories": {
|
|
6
6
|
"docs": "docs",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"@js-temporal/polyfill": "^0.4.3",
|
|
80
80
|
"body-parser": "^1.20.2",
|
|
81
81
|
"common-js-file-extensions": "^1.0.4",
|
|
82
|
-
"cookie": "^0.
|
|
82
|
+
"cookie": "^1.0.0",
|
|
83
83
|
"cookie-signature": "^1.2.0",
|
|
84
84
|
"debug": "^4.3.4",
|
|
85
85
|
"etag": "^1.8.1",
|
|
@@ -92,17 +92,13 @@
|
|
|
92
92
|
"mysql2": "^3.6.0",
|
|
93
93
|
"pg": "^8.11.3",
|
|
94
94
|
"pg-hstore": "^2.3.4",
|
|
95
|
-
"proxy-addr": "^2.0.7",
|
|
96
95
|
"qs": "^6.11.0",
|
|
97
|
-
"
|
|
98
|
-
"sequelize": "^6.6.5",
|
|
99
|
-
"type-is": "^1.6.18",
|
|
100
|
-
"vary": "^1.1.2"
|
|
96
|
+
"sequelize": "^6.6.5"
|
|
101
97
|
},
|
|
102
98
|
"devDependencies": {
|
|
103
99
|
"eslint": "^8.55.0",
|
|
104
100
|
"eslint-plugin-jsdoc": "^46.9.0",
|
|
105
|
-
"jest": "^29.
|
|
101
|
+
"jest": "^29.7.0"
|
|
106
102
|
},
|
|
107
103
|
"engines": {
|
|
108
104
|
"node": ">= 12.17.0"
|
package/tests/index.test.js
CHANGED
|
@@ -31,10 +31,10 @@ describe('nodester application', () => {
|
|
|
31
31
|
expect(app.isListening).toBe(true);
|
|
32
32
|
expect(app.middlewaresStack.length).toBe(4);
|
|
33
33
|
|
|
34
|
-
app.stop()
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
app.stop(() => {
|
|
35
|
+
expect(app.isLocked).toBe(false);
|
|
36
|
+
expect(app.isListening).toBe(false);
|
|
37
|
+
});
|
|
38
38
|
});
|
|
39
39
|
});
|
|
40
40
|
});
|
|
@@ -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
|
-
}
|