nodester 0.2.6 → 0.2.8
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/.eslintrc.js +13 -0
- package/Readme.md +8 -4
- package/lib/application/index.js +61 -53
- package/lib/body/extract.js +4 -4
- package/lib/controllers/methods/index.js +29 -14
- package/lib/controllers/mixins/index.js +13 -13
- package/lib/database/connection.js +78 -17
- package/lib/database/migration.js +9 -3
- package/lib/errors/CustomError.js +7 -2
- package/lib/errors/NodesterError.js +10 -2
- package/lib/errors/NodesterQueryError.js +9 -2
- package/lib/errors/index.js +2 -2
- package/lib/facades/methods/index.js +15 -15
- package/lib/facades/mixins/index.js +12 -12
- package/lib/factories/responses/html.js +20 -9
- package/lib/factories/responses/rest.js +21 -19
- package/lib/http/codes/descriptions.js +4 -3
- package/lib/http/codes/index.js +3 -2
- package/lib/http/codes/symbols.js +3 -2
- package/lib/http/request/index.js +20 -250
- package/lib/http/request/utils.js +4 -4
- package/lib/http/response/headers.js +16 -19
- package/lib/http/response/index.js +25 -28
- package/lib/http/response/utils.js +7 -6
- package/lib/loggers/console.js +3 -4
- package/lib/loggers/dev.js +3 -4
- package/lib/middlewares/404/index.js +38 -0
- package/lib/middlewares/SearchParams/index.js +3 -3
- package/lib/middlewares/cookies/index.js +2 -2
- package/lib/middlewares/etag/index.js +10 -8
- package/lib/middlewares/formidable/index.js +2 -2
- package/lib/middlewares/ql/sequelize/index.js +2 -2
- package/lib/middlewares/ql/sequelize/interpreter/ModelsTree.js +12 -2
- package/lib/middlewares/ql/sequelize/interpreter/QueryLexer.js +10 -2
- package/lib/middlewares/render/index.js +3 -3
- package/lib/models/associate.js +3 -2
- package/lib/models/define.js +37 -18
- package/lib/models/mixins.js +9 -9
- package/lib/query/traverse.js +11 -3
- package/lib/router/handlers.util.js +5 -5
- package/lib/router/index.js +84 -70
- package/lib/router/markers.js +5 -5
- package/lib/router/route.js +18 -19
- package/lib/router/routes.util.js +4 -4
- package/lib/router/utils.js +2 -2
- package/lib/stacks/MarkersStack.js +11 -9
- package/lib/stacks/MiddlewaresStack.js +25 -21
- package/lib/structures/Enum.js +8 -2
- package/lib/structures/Filter.js +22 -20
- package/lib/structures/Params.js +3 -3
- package/lib/tools/nql.tool.js +10 -2
- package/lib/tools/sql.tool.js +19 -2
- package/lib/utils/json.util.js +28 -27
- package/lib/utils/models.js +3 -2
- package/lib/utils/objects.util.js +10 -11
- package/lib/utils/path.util.js +9 -3
- package/lib/utils/sanitizations.util.js +3 -2
- package/lib/utils/types.util.js +11 -4
- package/lib/validators/arguments.js +28 -9
- package/lib/validators/dates.js +4 -5
- package/lib/validators/numbers.js +4 -4
- package/package.json +43 -39
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -14,29 +14,31 @@ const fs = require('fs');
|
|
|
14
14
|
const getFileStats = promisify(fs.stat);
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
module.exports = initETagMiddleware;
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* Initialize `etag` middleware.
|
|
19
21
|
*
|
|
20
|
-
* @param {Object} options
|
|
21
|
-
* @param {
|
|
22
|
+
* @param {Object} [options]
|
|
23
|
+
* @param {boolean} options.weak
|
|
22
24
|
*
|
|
23
25
|
* @return {Function}
|
|
24
26
|
*
|
|
25
|
-
* @
|
|
27
|
+
* @access public
|
|
26
28
|
*/
|
|
27
|
-
|
|
29
|
+
function initETagMiddleware(options) {
|
|
28
30
|
const context = {
|
|
29
31
|
options
|
|
30
32
|
}
|
|
31
33
|
return handle.bind(context);
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
/**
|
|
35
37
|
* Add ETag header field.
|
|
36
38
|
*/
|
|
37
39
|
function handle(req, res, next) {
|
|
38
40
|
// console.log('e', req.headers);
|
|
39
|
-
next();
|
|
41
|
+
return next();
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
async function _getResponseEntity(res) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
const debug = require('debug')('nodester:interpreter:ModelsTree');
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @class
|
|
13
|
+
*
|
|
14
|
+
* @access public
|
|
15
|
+
*/
|
|
11
16
|
class ModelsTreeNode {
|
|
12
17
|
constructor(model, parent=null, opts={}) {
|
|
13
18
|
this.model = model;
|
|
@@ -112,6 +117,11 @@ class ModelsTreeNode {
|
|
|
112
117
|
}
|
|
113
118
|
}
|
|
114
119
|
|
|
120
|
+
/**
|
|
121
|
+
* @class
|
|
122
|
+
*
|
|
123
|
+
* @access public
|
|
124
|
+
*/
|
|
115
125
|
class ModelsTree {
|
|
116
126
|
constructor() {
|
|
117
127
|
this.root = new ModelsTreeNode('root', null);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -54,6 +54,14 @@ const FN_TOKENS = new Enum({
|
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* @class
|
|
59
|
+
* @classdef constructs ModelTree based on the querystring
|
|
60
|
+
*
|
|
61
|
+
* @param {string} queryString
|
|
62
|
+
*
|
|
63
|
+
* @access public
|
|
64
|
+
*/
|
|
57
65
|
module.exports = class QueryLexer {
|
|
58
66
|
constructor(queryString='') {
|
|
59
67
|
this.tree = new ModelsTree();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -29,7 +29,7 @@ function handle(req, res, next) {
|
|
|
29
29
|
* - `filename` filename of the view being rendered
|
|
30
30
|
*
|
|
31
31
|
* @alias render
|
|
32
|
-
* @
|
|
32
|
+
* @access public
|
|
33
33
|
*/
|
|
34
34
|
function _render(view, options, callback) {
|
|
35
35
|
const app = this.req.app;
|
package/lib/models/associate.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -11,6 +11,7 @@ module.exports = {
|
|
|
11
11
|
associateModelsSync: _associateModelsSync
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
|
|
14
15
|
async function _associateModels(databaseConnection) {
|
|
15
16
|
try {
|
|
16
17
|
const models = databaseConnection.models;
|
package/lib/models/define.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -13,13 +13,15 @@ const { DataTypes } = require('sequelize');
|
|
|
13
13
|
|
|
14
14
|
module.exports = defineModel;
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
17
|
* @param {SequilizeConnection} databaseConnection
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {string} modelName
|
|
19
19
|
* @param {Function} definition
|
|
20
|
-
* @param {Object}
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* @param {Object} [options]
|
|
21
|
+
* ... Sequilize model options
|
|
22
|
+
* @param {Object} [options.nodester]
|
|
23
|
+
* @param {boolean} [options.nodester.noCRUD]
|
|
24
|
+
* @param {string} [options.nodester.output]
|
|
23
25
|
*/
|
|
24
26
|
function defineModel(
|
|
25
27
|
databaseConnection,
|
|
@@ -46,16 +48,18 @@ function defineModel(
|
|
|
46
48
|
|
|
47
49
|
// Configs related to nodester:
|
|
48
50
|
nodester: {
|
|
51
|
+
noCRUD: false,
|
|
49
52
|
output: 'underscored'
|
|
50
53
|
},
|
|
51
54
|
|
|
52
|
-
// Add user-defined options
|
|
55
|
+
// Add user-defined options
|
|
56
|
+
// (they can fully override upper ones).
|
|
53
57
|
...options
|
|
54
58
|
};
|
|
55
59
|
|
|
56
60
|
const model = databaseConnection.define(modelName, definitionObject, _options);
|
|
57
61
|
|
|
58
|
-
if (
|
|
62
|
+
if (_options.nodester.noCRUD !== true) {
|
|
59
63
|
// Add:
|
|
60
64
|
// - createWithIncludes;
|
|
61
65
|
// - findById;
|
|
@@ -67,7 +71,7 @@ function defineModel(
|
|
|
67
71
|
|
|
68
72
|
// Associations:
|
|
69
73
|
model.associate = (models) => {};
|
|
70
|
-
model.
|
|
74
|
+
model.getIncludesTree = _getIncludesTree.bind(model);
|
|
71
75
|
|
|
72
76
|
// Instance methods:
|
|
73
77
|
model.prototype.toJSON = function() {
|
|
@@ -78,8 +82,22 @@ function defineModel(
|
|
|
78
82
|
return model;
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
// Associations mixins:
|
|
86
|
+
/**
|
|
87
|
+
* @example
|
|
88
|
+
* [
|
|
89
|
+
* <include_name_0>: {
|
|
90
|
+
* <subinclude_name_0>: { ... }
|
|
91
|
+
* },
|
|
92
|
+
* <include_name_1>: { ... }
|
|
93
|
+
* ]
|
|
94
|
+
* @param {Object|null} data
|
|
95
|
+
*
|
|
96
|
+
* @return {Array} associationsTree
|
|
97
|
+
*
|
|
98
|
+
* @alias getIncludesTree
|
|
99
|
+
*/
|
|
100
|
+
function _getIncludesTree(data=null) {
|
|
83
101
|
const result = [];
|
|
84
102
|
|
|
85
103
|
const associations = this.associations;
|
|
@@ -88,15 +106,16 @@ function _getIncludesList(facadeData=null) {
|
|
|
88
106
|
for (const [ associationName, associationDefinition ] of associationEntries) {
|
|
89
107
|
const formatted = { association: associationName };
|
|
90
108
|
|
|
91
|
-
if (
|
|
92
|
-
// If
|
|
93
|
-
|
|
109
|
+
if (typeof data === 'object') {
|
|
110
|
+
// If data (for example during create)
|
|
111
|
+
// is set, go deeper:
|
|
112
|
+
const keys = Object.keys( data );
|
|
94
113
|
if (keys.indexOf(associationName) > 0) {
|
|
95
114
|
const associationModel = associationDefinition.target;
|
|
96
115
|
|
|
97
116
|
if (Object.entries(associationModel.associations).length > 0) {
|
|
98
|
-
const deepData =
|
|
99
|
-
formatted.include = associationModel.
|
|
117
|
+
const deepData = data[ associationName ];
|
|
118
|
+
formatted.include = associationModel.getIncludesTree(
|
|
100
119
|
Array.isArray(deepData) ? deepData[0] : deepData
|
|
101
120
|
);
|
|
102
121
|
}
|
|
@@ -108,4 +127,4 @@ function _getIncludesList(facadeData=null) {
|
|
|
108
127
|
|
|
109
128
|
return result;
|
|
110
129
|
}
|
|
111
|
-
|
|
130
|
+
// Associations mixins\
|
package/lib/models/mixins.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
'use strict';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
9
|
* CRUD mixins for any model:
|
|
10
10
|
*/
|
|
11
|
+
|
|
11
12
|
// Utils:
|
|
12
13
|
const {
|
|
13
14
|
modelHasAssociations,
|
|
@@ -30,8 +31,7 @@ module.exports = {
|
|
|
30
31
|
*
|
|
31
32
|
* @param {Object} modelDefinition
|
|
32
33
|
*
|
|
33
|
-
*
|
|
34
|
-
* @api public
|
|
34
|
+
* @access public
|
|
35
35
|
* @alias implementsCRUD
|
|
36
36
|
*/
|
|
37
37
|
function _implementsCRUD(modelDefinition) {
|
|
@@ -57,7 +57,7 @@ function _implementsCRUD(modelDefinition) {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
/** Main mixinis: */
|
|
61
61
|
async function _createWithIncludes(
|
|
62
62
|
data={}
|
|
63
63
|
) {
|
|
@@ -290,9 +290,9 @@ function _deleteById(
|
|
|
290
290
|
};
|
|
291
291
|
return this.destroy(query);
|
|
292
292
|
}
|
|
293
|
-
|
|
293
|
+
/** Main mixinis\ */
|
|
294
294
|
|
|
295
|
-
|
|
295
|
+
/** Subfunctions: */
|
|
296
296
|
async function _updateOrCreateOrDelete(
|
|
297
297
|
modelDefinition,
|
|
298
298
|
data
|
|
@@ -395,4 +395,4 @@ function _unwrapUpdateOrCreateOrDeleteOperationsResults(
|
|
|
395
395
|
|
|
396
396
|
return result;
|
|
397
397
|
}
|
|
398
|
-
|
|
398
|
+
/** Subfunctions\ */
|
package/lib/query/traverse.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -16,6 +16,14 @@ const { ensure } = require('nodester/validators/arguments');
|
|
|
16
16
|
|
|
17
17
|
module.exports = traverse;
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {ModelsTreeNode} queryNode
|
|
22
|
+
* @param {NodesterFilter} filter
|
|
23
|
+
* @param {Model} model
|
|
24
|
+
*
|
|
25
|
+
* @access public
|
|
26
|
+
*/
|
|
19
27
|
function traverse(queryNode, filter=null, model=null) {
|
|
20
28
|
const _model = model ?? filter.model;
|
|
21
29
|
|
|
@@ -313,7 +321,7 @@ function traverse(queryNode, filter=null, model=null) {
|
|
|
313
321
|
* @param {NodesterFilter} filter
|
|
314
322
|
* @param {Object} resultQuery
|
|
315
323
|
*
|
|
316
|
-
* @
|
|
324
|
+
* @access private
|
|
317
325
|
*/
|
|
318
326
|
function _traverseIncludes(includes, rootModel, filter, resultQuery) {
|
|
319
327
|
const filterIncludesEntries = Object.entries(filter.includes);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
'use strict';
|
|
7
7
|
|
|
8
|
-
const { typeOf } = require('
|
|
8
|
+
const { typeOf } = require('nodester/utils/types');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
module.exports = {
|
|
@@ -13,12 +13,12 @@ module.exports = {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
17
|
* @param {Object} routeHandler
|
|
18
18
|
* @return {Object} parsedRouteHandler
|
|
19
19
|
*
|
|
20
20
|
* @alias parseRouteHandler
|
|
21
|
-
* @public
|
|
21
|
+
* @access public
|
|
22
22
|
*/
|
|
23
23
|
function _parseRouteHandler(routeHandler={}) {
|
|
24
24
|
if (!routeHandler) {
|