not-node 4.0.1 → 4.0.5
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.json +1 -0
- package/index.js +2 -0
- package/package.json +2 -1
- package/src/domain.js +42 -32
- package/src/form/index.js +78 -0
- package/src/init/db/index.js +2 -1
- package/src/init/db/ioredis.js +24 -0
- package/src/init/db/mongoose.js +1 -0
- package/src/init/index.js +8 -2
- package/src/init/middleware.js +3 -1
- package/src/init/rateLimiter.js +1 -1
- package/src/init/routes.js +42 -18
- package/src/init/sessions/mongoose.js +1 -1
- package/src/manifest/module.js +18 -2
- package/src/manifest/registrator/forms.js +48 -0
- package/src/manifest/registrator/index.js +2 -0
- package/src/manifest/registrator/routes.js +2 -2
- package/src/manifest/route.js +30 -0
- package/src/model/proto.js +3 -4
- package/test/init/routes.js +6 -4
- package/test/module/index.js +1 -1
- package/test/notDomain.js +22 -10
- package/test/notInit.js +3 -2
- package/test/notModule.js +7 -0
package/.eslintrc.json
CHANGED
package/index.js
CHANGED
|
@@ -29,6 +29,8 @@ module.exports.Routine = require('./src/model/routine');
|
|
|
29
29
|
module.exports.Common = require('./src/common');
|
|
30
30
|
/** Fields library manager */
|
|
31
31
|
module.exports.Fields = require('./src/fields');
|
|
32
|
+
/** Form validation template **/
|
|
33
|
+
module.exports.Form = require('./src/form');
|
|
32
34
|
/** Application initialization procedures */
|
|
33
35
|
module.exports.Init = require('./src/init').Init;
|
|
34
36
|
/** Application object */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-node",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "node complimentary part for client side notFramework.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"eslint-plugin-promise": "^5.1.1",
|
|
79
79
|
"eslint-plugin-sonarjs": "^0.10.0",
|
|
80
80
|
"ink-docstrap": "^1.3.2",
|
|
81
|
+
"ioredis": "^4.28.1",
|
|
81
82
|
"jsdoc": "^3.6.7",
|
|
82
83
|
"mocha": "*",
|
|
83
84
|
"mocha-suppress-logs": "^0.3.1",
|
package/src/domain.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
*/
|
|
6
6
|
const EventEmitter = require('events');
|
|
7
|
-
const {
|
|
7
|
+
const {isFunc,isAsync, objHas, firstLetterToUpper} = require('./common');
|
|
8
8
|
|
|
9
9
|
const Env = require('./env');
|
|
10
10
|
const
|
|
@@ -133,6 +133,17 @@ class notDomain extends EventEmitter {
|
|
|
133
133
|
return null;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Returns form
|
|
138
|
+
* @param {string} name 'formName' or 'moduleName//formName'
|
|
139
|
+
* ('login', 'not-user//login')
|
|
140
|
+
* @return {object} form
|
|
141
|
+
**/
|
|
142
|
+
getForm(name) {
|
|
143
|
+
const type = 'form';
|
|
144
|
+
return this.getByPath(name, type);
|
|
145
|
+
}
|
|
146
|
+
|
|
136
147
|
/**
|
|
137
148
|
* Returns model
|
|
138
149
|
* @param {string} name 'modelName' or 'moduleName//modelName'
|
|
@@ -141,11 +152,7 @@ class notDomain extends EventEmitter {
|
|
|
141
152
|
**/
|
|
142
153
|
getModel(name) {
|
|
143
154
|
const type = 'model';
|
|
144
|
-
|
|
145
|
-
return this.getByFullPath(name, type);
|
|
146
|
-
} else {
|
|
147
|
-
return this.getByShortPath(name, type);
|
|
148
|
-
}
|
|
155
|
+
return this.getByPath(name, type);
|
|
149
156
|
}
|
|
150
157
|
|
|
151
158
|
getByFullPath(name, type){
|
|
@@ -169,31 +176,23 @@ class notDomain extends EventEmitter {
|
|
|
169
176
|
|
|
170
177
|
/**
|
|
171
178
|
* Returns file with model declarations
|
|
172
|
-
* @param {string}
|
|
179
|
+
* @param {string} name 'modelName' or 'moduleName//modelName'
|
|
173
180
|
* @return {object} CommonJS module object
|
|
174
181
|
**/
|
|
175
|
-
getModelFile(
|
|
182
|
+
getModelFile(name) {
|
|
176
183
|
const type = 'modelFile';
|
|
177
|
-
|
|
178
|
-
return this.getByFullPath(modelName, type);
|
|
179
|
-
} else {
|
|
180
|
-
return this.getByShortPath(modelName, type);
|
|
181
|
-
}
|
|
184
|
+
return this.getByPath(name, type);
|
|
182
185
|
}
|
|
183
186
|
|
|
184
187
|
/**
|
|
185
188
|
* Returns specified by name or 'moduleName//modelName' model Schema
|
|
186
|
-
* @param {string}
|
|
189
|
+
* @param {string} name 'modelName' or 'moduleName//modelName'
|
|
187
190
|
* @return {object} model schema
|
|
188
191
|
**/
|
|
189
192
|
|
|
190
|
-
getModelSchema(
|
|
193
|
+
getModelSchema(name) {
|
|
191
194
|
const type = 'modelSchema';
|
|
192
|
-
|
|
193
|
-
return this.getByFullPath(modelName, type);
|
|
194
|
-
} else {
|
|
195
|
-
return this.getByShortPath(modelName, type);
|
|
196
|
-
}
|
|
195
|
+
return this.getByPath(name, type);
|
|
197
196
|
}
|
|
198
197
|
|
|
199
198
|
/**
|
|
@@ -204,25 +203,25 @@ class notDomain extends EventEmitter {
|
|
|
204
203
|
**/
|
|
205
204
|
getLogic(name) {
|
|
206
205
|
const type = 'logic';
|
|
207
|
-
|
|
208
|
-
return this.getByFullPath(name, type);
|
|
209
|
-
} else {
|
|
210
|
-
return this.getByShortPath(name, type);
|
|
211
|
-
}
|
|
206
|
+
return this.getByPath(name, type);
|
|
212
207
|
}
|
|
213
208
|
|
|
214
209
|
|
|
215
210
|
/**
|
|
216
211
|
* Returns file with logic declarations
|
|
217
|
-
* @param {string}
|
|
212
|
+
* @param {string} name 'logicName' or 'moduleName//logicName'
|
|
218
213
|
* @return {object} CommonJS module object
|
|
219
214
|
**/
|
|
220
|
-
getLogicFile(
|
|
215
|
+
getLogicFile(name) {
|
|
221
216
|
const type = 'logicFile';
|
|
222
|
-
|
|
223
|
-
|
|
217
|
+
return this.getByPath(name, type);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
getByPath(name, type){
|
|
221
|
+
if (name.indexOf('//') > 0) {
|
|
222
|
+
return this.getByFullPath(name, type);
|
|
224
223
|
} else {
|
|
225
|
-
return this.getByShortPath(
|
|
224
|
+
return this.getByShortPath(name, type);
|
|
226
225
|
}
|
|
227
226
|
}
|
|
228
227
|
|
|
@@ -251,7 +250,13 @@ class notDomain extends EventEmitter {
|
|
|
251
250
|
async execInModules(methodName) {
|
|
252
251
|
for (let mod of Object.values(this.modules)) {
|
|
253
252
|
try{
|
|
254
|
-
|
|
253
|
+
if(isFunc(mod.exec)){
|
|
254
|
+
if(isAsync(mod.exec)){
|
|
255
|
+
await mod.exec(methodName);
|
|
256
|
+
}else{
|
|
257
|
+
mod.exec(methodName);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
255
260
|
}catch(e){
|
|
256
261
|
this.report(e);
|
|
257
262
|
}
|
|
@@ -397,6 +402,10 @@ class notDomain extends EventEmitter {
|
|
|
397
402
|
count: 0,
|
|
398
403
|
list: []
|
|
399
404
|
},
|
|
405
|
+
forms: {
|
|
406
|
+
count: 0,
|
|
407
|
+
list: []
|
|
408
|
+
},
|
|
400
409
|
actions: {
|
|
401
410
|
count: 0,
|
|
402
411
|
list: []
|
|
@@ -412,8 +421,9 @@ class notDomain extends EventEmitter {
|
|
|
412
421
|
stats.modules.content[modName] = modStatus;
|
|
413
422
|
stats.routes.count += modStatus.routes.count;
|
|
414
423
|
stats.models.count += modStatus.models.count;
|
|
424
|
+
stats.forms.count += modStatus.forms.count;
|
|
415
425
|
stats.actions.count += modStatus.actions.count;
|
|
416
|
-
for (let t of ['routes', 'models', 'actions']) {
|
|
426
|
+
for (let t of ['routes', 'models', 'actions', 'forms']) {
|
|
417
427
|
stats[t].list.push(...(modStatus[t].list.map(itmName => `${modName}//${itmName}`)));
|
|
418
428
|
}
|
|
419
429
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//DB related validation tools
|
|
2
|
+
const mongoose = require('mongoose');
|
|
3
|
+
const Schema = mongoose.Schema;
|
|
4
|
+
//not-node
|
|
5
|
+
const initFields = require('../fields').initFields;
|
|
6
|
+
const {
|
|
7
|
+
byFieldsValidators
|
|
8
|
+
} = require('../model/enrich');
|
|
9
|
+
const {
|
|
10
|
+
notValidationError,
|
|
11
|
+
notError
|
|
12
|
+
} = require('not-error');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generic form validation class
|
|
16
|
+
**/
|
|
17
|
+
module.exports = class Form {
|
|
18
|
+
constructor({
|
|
19
|
+
FIELDS,
|
|
20
|
+
FORM_NAME
|
|
21
|
+
}) {
|
|
22
|
+
this.FORM_NAME = FORM_NAME;
|
|
23
|
+
this.FIELDS = FIELDS;
|
|
24
|
+
this.SCHEMA = byFieldsValidators(initFields(FIELDS, 'model'));
|
|
25
|
+
this.MODEL = mongoose.model(FORM_NAME, Schema(this.SCHEMA));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extract data from ExpressRequest object and validates it
|
|
30
|
+
* returns it or throws
|
|
31
|
+
* @param {ExpressRequest} req expressjs request object
|
|
32
|
+
* @return {Promise<Object>} form data
|
|
33
|
+
* @throws {notValidationError}
|
|
34
|
+
**/
|
|
35
|
+
async run(req) {
|
|
36
|
+
let data = await this.extract(req);
|
|
37
|
+
await this.validate(data);
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Extracts data, should be overriden
|
|
43
|
+
* @param {ExpressRequest} req expressjs request object
|
|
44
|
+
* @return {Object} forma data
|
|
45
|
+
**/
|
|
46
|
+
async extract( /*req*/ ) {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validates form data or throws
|
|
52
|
+
* @param {Object} data form data
|
|
53
|
+
* @return {Object}
|
|
54
|
+
* @throws {notValidationError}
|
|
55
|
+
**/
|
|
56
|
+
async validate(data) {
|
|
57
|
+
try {
|
|
58
|
+
await this.MODEL.validate(data, this.FIELDS);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
let fields = {};
|
|
61
|
+
if (e instanceof mongoose.Error.ValidationError) {
|
|
62
|
+
Object.keys(e.errors).forEach(name => {
|
|
63
|
+
fields[name] = [e.errors[name].message];
|
|
64
|
+
});
|
|
65
|
+
throw new notValidationError(e.message, fields, e, data);
|
|
66
|
+
} else {
|
|
67
|
+
throw new notError(
|
|
68
|
+
'core:form_validation_error', {
|
|
69
|
+
FORM_NAME: this.FORM_NAME,
|
|
70
|
+
FIELDS: this.FIELDS,
|
|
71
|
+
data
|
|
72
|
+
},
|
|
73
|
+
e
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
package/src/init/db/index.js
CHANGED
|
@@ -6,6 +6,7 @@ module.exports = class InitDB{
|
|
|
6
6
|
static default = path.resolve(__dirname, './mongoose.js');
|
|
7
7
|
|
|
8
8
|
static drivers = {
|
|
9
|
+
'ioredis': path.resolve(__dirname, './ioredis.js'),
|
|
9
10
|
'redis': path.resolve(__dirname, './redis.js'),
|
|
10
11
|
'mongoose': path.resolve(__dirname, './mongoose.js')
|
|
11
12
|
};
|
|
@@ -40,7 +41,7 @@ module.exports = class InitDB{
|
|
|
40
41
|
master,
|
|
41
42
|
config,
|
|
42
43
|
options,
|
|
43
|
-
conf,
|
|
44
|
+
conf: conf[driver],
|
|
44
45
|
alias: driver
|
|
45
46
|
});
|
|
46
47
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const log = require('not-log')(module, 'not-node//init');
|
|
2
|
+
const ADDS = require('../additional');
|
|
3
|
+
|
|
4
|
+
module.exports = class InitDBRedisIO{
|
|
5
|
+
|
|
6
|
+
static async initRedis({ conf, master, alias}) {
|
|
7
|
+
log.info('Setting up ioredis connection...');
|
|
8
|
+
const Redis = require('ioredis');
|
|
9
|
+
const redisClient = new Redis(conf);
|
|
10
|
+
redisClient.on('error', log.error);
|
|
11
|
+
master.setEnv(`db.${alias}`, redisClient);
|
|
12
|
+
log.log('redis client');
|
|
13
|
+
log.log(Object.keys(redisClient));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async run({config, options, master, conf, alias}){
|
|
17
|
+
await ADDS.run(`db.${alias}.pre`, {config, options, master, conf, alias});
|
|
18
|
+
await InitDBRedisIO.initRedis({conf, master, alias});
|
|
19
|
+
await ADDS.run(`db.${alias}.post`, {config, options, master, conf, alias});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
};
|
package/src/init/db/mongoose.js
CHANGED
|
@@ -28,6 +28,7 @@ module.exports = class InitDBMongoose{
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
async run({config, options, master, conf, alias}){
|
|
31
|
+
log.info(`db.${alias}.pre`);
|
|
31
32
|
await ADDS.run(`db.${alias}.pre`, {config, options, master, conf, alias});
|
|
32
33
|
await InitDBMongoose.initMongoose({conf, master, alias});
|
|
33
34
|
await ADDS.run(`db.${alias}.post`, {config, options, master, conf, alias});
|
package/src/init/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//
|
|
2
|
+
const os = require('os');
|
|
2
3
|
const path = require('path');
|
|
3
4
|
const logger = require('not-log');
|
|
4
5
|
const log = logger(module, 'not-node:Init');
|
|
@@ -80,7 +81,7 @@ class Init {
|
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
static getEnv(key) {
|
|
83
|
-
return Env.
|
|
84
|
+
return Env.getEnv(key);
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
static getApp() {
|
|
@@ -126,7 +127,7 @@ class Init {
|
|
|
126
127
|
additional
|
|
127
128
|
}) {
|
|
128
129
|
try {
|
|
129
|
-
log.info('Kick start app...');
|
|
130
|
+
log.info('Kick start app...'+ os.platform()+os.arch());
|
|
130
131
|
ADDS.init(additional);
|
|
131
132
|
const initSequence = new InitSequence(STANDART_INIT_SEQUENCE);
|
|
132
133
|
await ADDS.run('pre', {
|
|
@@ -149,6 +150,11 @@ class Init {
|
|
|
149
150
|
};
|
|
150
151
|
//running all prepared initalizers with current context
|
|
151
152
|
await initSequence.run(context);
|
|
153
|
+
await ADDS.run('post', {
|
|
154
|
+
config,
|
|
155
|
+
options,
|
|
156
|
+
manifest
|
|
157
|
+
});
|
|
152
158
|
log.info('Application initalization finished');
|
|
153
159
|
} catch (e) {
|
|
154
160
|
Init.throwError(e.message, 1);
|
package/src/init/middleware.js
CHANGED
|
@@ -18,7 +18,9 @@ module.exports = class InitMiddleware{
|
|
|
18
18
|
} else {
|
|
19
19
|
proc = require(warePath);
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
if(typeof proc === 'function'){
|
|
22
|
+
master.getServer().use(proc);
|
|
23
|
+
}
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
await ADDS.run('middleware.post', {config, options, master});
|
package/src/init/rateLimiter.js
CHANGED
package/src/init/routes.js
CHANGED
|
@@ -2,49 +2,73 @@
|
|
|
2
2
|
|
|
3
3
|
const serveStatic = require('serve-static');
|
|
4
4
|
const log = require('not-log')(module, 'not-node//init');
|
|
5
|
-
const {
|
|
5
|
+
const {
|
|
6
|
+
notError,
|
|
7
|
+
notValidationError,
|
|
8
|
+
notRequestError
|
|
9
|
+
} = require('not-error');
|
|
6
10
|
|
|
7
11
|
module.exports = class InitRoutes {
|
|
8
12
|
|
|
9
|
-
static finalError({
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
static finalError({
|
|
14
|
+
master
|
|
15
|
+
}) {
|
|
16
|
+
return (err, req, res, next) => {
|
|
17
|
+
//reportable errors from known cases
|
|
18
|
+
if (err instanceof notError) {
|
|
12
19
|
master.getApp().report(err);
|
|
13
|
-
if
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
res.status(err.getCode()).json({
|
|
20
|
+
//if request params - ok, but result is not
|
|
21
|
+
if (err instanceof notRequestError) {
|
|
22
|
+
if (err.getRedirect()) {
|
|
23
|
+
return res.redirect(err.getRedirect());
|
|
24
|
+
} else {
|
|
25
|
+
return res.status(err.getCode()).json({
|
|
19
26
|
status: 'error',
|
|
20
|
-
|
|
27
|
+
message: err.getResult().message,
|
|
21
28
|
errors: err.getResult().errors
|
|
22
29
|
});
|
|
23
30
|
}
|
|
31
|
+
//bad request params
|
|
32
|
+
}else if (err instanceof notValidationError){
|
|
33
|
+
return res.status(400).json({
|
|
34
|
+
status: 'error',
|
|
35
|
+
message: err.message,
|
|
36
|
+
errors: err.getFieldsErrors()
|
|
37
|
+
});
|
|
24
38
|
}
|
|
25
|
-
}
|
|
39
|
+
}
|
|
40
|
+
//other cases
|
|
41
|
+
if (err instanceof Error && (res && res.status && res.json)) {
|
|
26
42
|
res.status(err.statusCode || 500);
|
|
43
|
+
//reporting as unknown
|
|
27
44
|
master.getApp().report(new notError(`Internal error(${res.statusCode}): %${req.url} - %${err.message}`, {}, err));
|
|
28
45
|
res.json({
|
|
29
46
|
status: 'error',
|
|
30
|
-
|
|
47
|
+
message: err.message
|
|
31
48
|
});
|
|
32
|
-
}else{
|
|
49
|
+
} else {
|
|
33
50
|
log.error('Unknown error:', err);
|
|
34
51
|
res.status(500).json({
|
|
35
52
|
status: 'error'
|
|
36
53
|
});
|
|
37
54
|
}
|
|
55
|
+
next();
|
|
38
56
|
};
|
|
39
57
|
}
|
|
40
58
|
|
|
41
|
-
async run({
|
|
59
|
+
async run({
|
|
60
|
+
master,
|
|
61
|
+
config,
|
|
62
|
+
options
|
|
63
|
+
}) {
|
|
42
64
|
log.info('Setting up routes...');
|
|
43
65
|
master.getApp().expose(master.getServer());
|
|
44
66
|
require(options.routesPath)(master.getServer(), master.getApp());
|
|
45
|
-
master.
|
|
46
|
-
master.
|
|
47
|
-
master.
|
|
67
|
+
master.getServer().use(serveStatic(config.get('staticPath')));
|
|
68
|
+
master.getServer().use(options.indexRoute);
|
|
69
|
+
master.getServer().use(InitRoutes.finalError({
|
|
70
|
+
master
|
|
71
|
+
}));
|
|
48
72
|
}
|
|
49
73
|
|
|
50
74
|
};
|
|
@@ -5,7 +5,7 @@ const ADDS = require('../additional');
|
|
|
5
5
|
module.exports = class InitSessionsMongo{
|
|
6
6
|
static createStore({config, master, expressSession}){
|
|
7
7
|
const MongoDBStore = require('connect-mongodb-session')(expressSession);
|
|
8
|
-
const mongooseOptions = config.get('mongoose.options');
|
|
8
|
+
const mongooseOptions = config.get('db.mongoose.options');
|
|
9
9
|
let store = new MongoDBStore({
|
|
10
10
|
uri: `mongodb://${mongooseOptions.user}:${mongooseOptions.pass}@${mongooseOptions.host}/${mongooseOptions.db}`,
|
|
11
11
|
databaseName: mongooseOptions.db,
|
package/src/manifest/module.js
CHANGED
|
@@ -47,6 +47,7 @@ class notModule {
|
|
|
47
47
|
};
|
|
48
48
|
this.models = {};
|
|
49
49
|
this.logics = {};
|
|
50
|
+
this.forms = {};
|
|
50
51
|
this.manifests = {};
|
|
51
52
|
this.faulty = false;
|
|
52
53
|
this.paths = {
|
|
@@ -164,7 +165,6 @@ class notModule {
|
|
|
164
165
|
}
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
|
|
168
168
|
getModelSchema(modelName) {
|
|
169
169
|
let modelFile = this.getModelFile(modelName);
|
|
170
170
|
if (modelFile && objHas(modelFile, modelName) && modelFile.thisSchema) {
|
|
@@ -221,10 +221,15 @@ class notModule {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
getStatus() {
|
|
224
|
+
const formsList = Object.keys(this.forms);
|
|
224
225
|
const modelsList = Object.keys(this.models);
|
|
225
226
|
const routesList = Object.keys(this.routes);
|
|
226
227
|
const actionsList = this.getActionsList();
|
|
227
228
|
return {
|
|
229
|
+
forms: {
|
|
230
|
+
count: formsList.length,
|
|
231
|
+
list: formsList
|
|
232
|
+
},
|
|
228
233
|
models: {
|
|
229
234
|
count: modelsList.length,
|
|
230
235
|
list: modelsList,
|
|
@@ -272,11 +277,18 @@ class notModule {
|
|
|
272
277
|
return content;
|
|
273
278
|
}
|
|
274
279
|
|
|
275
|
-
|
|
276
280
|
setManifest(key, val){
|
|
277
281
|
this.manifests[key] = val;
|
|
278
282
|
}
|
|
279
283
|
|
|
284
|
+
getForm(key) {
|
|
285
|
+
return this.forms[key];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
setForm(key, val) {
|
|
289
|
+
this.forms[key] = val;
|
|
290
|
+
}
|
|
291
|
+
|
|
280
292
|
setModel(key, val){
|
|
281
293
|
this.models[key] = val;
|
|
282
294
|
}
|
|
@@ -285,6 +297,10 @@ class notModule {
|
|
|
285
297
|
this.routes[key] = val;
|
|
286
298
|
}
|
|
287
299
|
|
|
300
|
+
setLogic(key, val){
|
|
301
|
+
this.logics[key] = val;
|
|
302
|
+
}
|
|
303
|
+
|
|
288
304
|
getRoute(routeName) {
|
|
289
305
|
if (this.routes && objHas(this.routes, routeName)) {
|
|
290
306
|
return this.routes[routeName];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const {tryFile} = require('../../common');
|
|
4
|
+
|
|
5
|
+
module.exports = class notModuleRegistratorForms{
|
|
6
|
+
|
|
7
|
+
static openFile = require;
|
|
8
|
+
|
|
9
|
+
constructor({nModule}){
|
|
10
|
+
this.run({nModule});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
run({nModule}){
|
|
14
|
+
const srcDir = notModuleRegistratorForms.getPath(nModule);
|
|
15
|
+
if (!srcDir) { return false; }
|
|
16
|
+
this.findAll(
|
|
17
|
+
{
|
|
18
|
+
nModule,
|
|
19
|
+
srcDir
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static getPath(nModule){
|
|
26
|
+
return nModule.module.paths.forms;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Searching forms in directory
|
|
31
|
+
* @param {Object} input
|
|
32
|
+
* @param {string} input.srcDir
|
|
33
|
+
**/
|
|
34
|
+
findAll({nModule, srcDir}){
|
|
35
|
+
fs.readdirSync(srcDir).forEach((file) => {
|
|
36
|
+
let fromPath = path.join(srcDir, file);
|
|
37
|
+
if (!tryFile(fromPath)) { return; }
|
|
38
|
+
this.register({nModule, fromPath});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
register({nModule, fromPath}){
|
|
43
|
+
const Form = notModuleRegistratorForms.openFile(fromPath);
|
|
44
|
+
const parts = path.parse(fromPath);
|
|
45
|
+
nModule.setForm(parts.name, new Form());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
};
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
const
|
|
7
7
|
notModuleRegistratorFields = require('./fields'),
|
|
8
|
+
notModuleRegistratorForms = require('./forms'),
|
|
8
9
|
notModuleRegistratorModels = require('./models'),
|
|
9
10
|
notModuleRegistratorLogics = require('./logics'),
|
|
10
11
|
notModuleRegistratorRoutes = require('./routes'),
|
|
@@ -13,6 +14,7 @@ const
|
|
|
13
14
|
|
|
14
15
|
const DEFAULT_REGISTRATORS = [
|
|
15
16
|
notModuleRegistratorFields,
|
|
17
|
+
notModuleRegistratorForms,
|
|
16
18
|
notModuleRegistratorModels,
|
|
17
19
|
notModuleRegistratorLogics,
|
|
18
20
|
notModuleRegistratorRoutes,
|
|
@@ -167,7 +167,7 @@ module.exports = class notModuleRegistratorRoutes{
|
|
|
167
167
|
});
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
-
static registerManifest({nModule,
|
|
171
|
-
nModule.setManifest(routeName,
|
|
170
|
+
static registerManifest({nModule, routeManifest, routeName}) {
|
|
171
|
+
nModule.setManifest(routeName, routeManifest);
|
|
172
172
|
}
|
|
173
173
|
};
|
package/src/manifest/route.js
CHANGED
|
@@ -133,11 +133,41 @@ class notRoute{
|
|
|
133
133
|
);
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
extractReturn(notRouteData){
|
|
137
|
+
if(objHas(notRouteData.rule, 'return')){
|
|
138
|
+
return [...notRouteData.rule.return];
|
|
139
|
+
}else if(objHas(notRouteData.actionData, 'return')){
|
|
140
|
+
return [...notRouteData.actionData.return];
|
|
141
|
+
}else{
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Removes fields from result object acctoding to actionData.return array
|
|
148
|
+
* if presented
|
|
149
|
+
* @param {ExpressRequest} req request object
|
|
150
|
+
* @param {object} result result returned by main action processor
|
|
151
|
+
*/
|
|
152
|
+
filterResultByReturnRule(req, result){
|
|
153
|
+
const returnList = this.extractReturn(req.notRouteData);
|
|
154
|
+
if(result && (typeof result === 'object') && returnList && Array.isArray(returnList)){
|
|
155
|
+
let presented = Object.keys(result);
|
|
156
|
+
presented.forEach(fieldName => {
|
|
157
|
+
if(!returnList.includes(fieldName)){
|
|
158
|
+
delete result[fieldName];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
136
164
|
async executeRoute(modRoute, actionName, {req, res, next}){
|
|
137
165
|
//waiting preparation
|
|
138
166
|
let prepared = await this.executeFunction(modRoute, CONST_BEFORE_ACTION, [req, res, next]);
|
|
139
167
|
//waiting results
|
|
140
168
|
let result = await this.executeFunction(modRoute, actionName, [req, res, next, prepared]);
|
|
169
|
+
//filter result IF actionData.return specified
|
|
170
|
+
this.filterResultByReturnRule(req, result);
|
|
141
171
|
//run after with results, continue without waiting when it finished
|
|
142
172
|
return this.executeFunction(modRoute, CONST_AFTER_ACTION, [req, res, next, result]);
|
|
143
173
|
}
|
package/src/model/proto.js
CHANGED
|
@@ -54,10 +54,9 @@ module.exports = class ModelFabricate{
|
|
|
54
54
|
if (targetModule.thisStatics) {
|
|
55
55
|
Object.assign(schema.statics, targetModule.thisStatics);
|
|
56
56
|
}
|
|
57
|
-
this.extendSchemaFrom(targetModule.thisVirtuals, schema.virtual);
|
|
58
|
-
this.extendSchemaFrom(targetModule.thisPre, schema.pre);
|
|
59
|
-
this.extendSchemaFrom(targetModule.thisPost, schema.post);
|
|
60
|
-
|
|
57
|
+
this.extendSchemaFrom(targetModule.thisVirtuals, schema.virtual.bind(schema));
|
|
58
|
+
this.extendSchemaFrom(targetModule.thisPre, schema.pre.bind(schema));
|
|
59
|
+
this.extendSchemaFrom(targetModule.thisPost, schema.post.bind(schema));
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
static enrichByFields(targetModule){
|
package/test/init/routes.js
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = ({expect})=>{
|
|
|
17
17
|
json(dat){
|
|
18
18
|
expect(dat).to.be.deep.equal({
|
|
19
19
|
status: 'error',
|
|
20
|
-
|
|
20
|
+
message: 'Serious generic error'
|
|
21
21
|
});
|
|
22
22
|
done();
|
|
23
23
|
}
|
|
@@ -120,7 +120,7 @@ module.exports = ({expect})=>{
|
|
|
120
120
|
json(dat){
|
|
121
121
|
expect(dat).to.be.deep.equal({
|
|
122
122
|
status: 'error',
|
|
123
|
-
|
|
123
|
+
message: 'Serious not error request',
|
|
124
124
|
errors: {field: ['name']},
|
|
125
125
|
});
|
|
126
126
|
done();
|
|
@@ -142,8 +142,10 @@ module.exports = ({expect})=>{
|
|
|
142
142
|
|
|
143
143
|
|
|
144
144
|
describe('run', ()=>{
|
|
145
|
-
it('', async ()=>{
|
|
146
|
-
const fakeServer = { sarver: true
|
|
145
|
+
it('run', async ()=>{
|
|
146
|
+
const fakeServer = { sarver: true, use(fn){
|
|
147
|
+
expect(typeof fn).to.be.equal('function');
|
|
148
|
+
}};
|
|
147
149
|
const fakeApp = {
|
|
148
150
|
report(){},
|
|
149
151
|
use(fn){
|
package/test/module/index.js
CHANGED
|
@@ -22,7 +22,7 @@ module.exports = (input)=>{
|
|
|
22
22
|
notModuleRegistrator.setRegistrators(regs);
|
|
23
23
|
expect(notModuleRegistrator.registrators.length).to.be.equal(0);
|
|
24
24
|
notModuleRegistrator.resetRegistrators();
|
|
25
|
-
expect(notModuleRegistrator.registrators.length).to.be.equal(
|
|
25
|
+
expect(notModuleRegistrator.registrators.length).to.be.equal(6);
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
it('with paths', ()=>{
|
package/test/notDomain.js
CHANGED
|
@@ -184,7 +184,7 @@ describe('notDomain', function() {
|
|
|
184
184
|
it('model name short', function() {
|
|
185
185
|
const route = 'Jungle';
|
|
186
186
|
const ctx = {
|
|
187
|
-
|
|
187
|
+
getByPath(){return null;}
|
|
188
188
|
};
|
|
189
189
|
const res = notDomain.prototype.getModel.call(ctx, route);
|
|
190
190
|
expect(res).to.be.null;
|
|
@@ -194,7 +194,7 @@ describe('notDomain', function() {
|
|
|
194
194
|
const id = Math.random();
|
|
195
195
|
const route = 'module//Jungle';
|
|
196
196
|
const ctx = {
|
|
197
|
-
|
|
197
|
+
getByPath(){return id;}
|
|
198
198
|
};
|
|
199
199
|
const res = notDomain.prototype.getModel.call(ctx, route);
|
|
200
200
|
expect(res).to.be.equal(id);
|
|
@@ -206,7 +206,7 @@ describe('notDomain', function() {
|
|
|
206
206
|
const id = Math.random();
|
|
207
207
|
const route = 'Jungle';
|
|
208
208
|
const ctx = {
|
|
209
|
-
|
|
209
|
+
getByPath(){return id;}
|
|
210
210
|
};
|
|
211
211
|
const res = notDomain.prototype.getModelFile.call(ctx, route);
|
|
212
212
|
expect(res).to.be.equal(id);
|
|
@@ -216,7 +216,7 @@ describe('notDomain', function() {
|
|
|
216
216
|
const id = Math.random();
|
|
217
217
|
const route = 'module//Jungle';
|
|
218
218
|
const ctx = {
|
|
219
|
-
|
|
219
|
+
getByPath(){return id;}
|
|
220
220
|
};
|
|
221
221
|
const res = notDomain.prototype.getModelFile.call(ctx, route);
|
|
222
222
|
expect(res).to.be.equal(id);
|
|
@@ -229,7 +229,7 @@ describe('notDomain', function() {
|
|
|
229
229
|
const id = Math.random();
|
|
230
230
|
const route = 'Jungle';
|
|
231
231
|
const ctx = {
|
|
232
|
-
|
|
232
|
+
getByPath(){return id;}
|
|
233
233
|
};
|
|
234
234
|
const res = notDomain.prototype.getModelSchema.call(ctx, route);
|
|
235
235
|
expect(res).to.be.equal(id);
|
|
@@ -239,7 +239,7 @@ describe('notDomain', function() {
|
|
|
239
239
|
const id = Math.random();
|
|
240
240
|
const route = 'module//Jungle';
|
|
241
241
|
const ctx = {
|
|
242
|
-
|
|
242
|
+
getByPath(){return id;}
|
|
243
243
|
};
|
|
244
244
|
const res = notDomain.prototype.getModelSchema.call(ctx, route);
|
|
245
245
|
expect(res).to.be.equal(id);
|
|
@@ -252,7 +252,7 @@ describe('notDomain', function() {
|
|
|
252
252
|
const id = Math.random();
|
|
253
253
|
const route = 'Jungle';
|
|
254
254
|
const ctx = {
|
|
255
|
-
|
|
255
|
+
getByPath(){return id;}
|
|
256
256
|
};
|
|
257
257
|
const res = notDomain.prototype.getLogic.call(ctx, route);
|
|
258
258
|
expect(res).to.be.equal(id);
|
|
@@ -262,7 +262,7 @@ describe('notDomain', function() {
|
|
|
262
262
|
const id = Math.random();
|
|
263
263
|
const route = 'module//Jungle';
|
|
264
264
|
const ctx = {
|
|
265
|
-
|
|
265
|
+
getByPath(){return id;}
|
|
266
266
|
};
|
|
267
267
|
const res = notDomain.prototype.getLogic.call(ctx, route);
|
|
268
268
|
expect(res).to.be.equal(id);
|
|
@@ -274,7 +274,7 @@ describe('notDomain', function() {
|
|
|
274
274
|
const id = Math.random();
|
|
275
275
|
const route = 'Jungle';
|
|
276
276
|
const ctx = {
|
|
277
|
-
|
|
277
|
+
getByPath(){return id;}
|
|
278
278
|
};
|
|
279
279
|
const res = notDomain.prototype.getLogicFile.call(ctx, route);
|
|
280
280
|
expect(res).to.be.equal(id);
|
|
@@ -284,7 +284,7 @@ describe('notDomain', function() {
|
|
|
284
284
|
const id = Math.random();
|
|
285
285
|
const route = 'module//Jungle';
|
|
286
286
|
const ctx = {
|
|
287
|
-
|
|
287
|
+
getByPath(){return id;}
|
|
288
288
|
};
|
|
289
289
|
const res = notDomain.prototype.getLogicFile.call(ctx, route);
|
|
290
290
|
expect(res).to.be.equal(id);
|
|
@@ -711,6 +711,10 @@ describe('notDomain', function() {
|
|
|
711
711
|
actions: {
|
|
712
712
|
count: 2,
|
|
713
713
|
list: ['user//list', 'role//list']
|
|
714
|
+
},
|
|
715
|
+
forms: {
|
|
716
|
+
count: 3,
|
|
717
|
+
list: ['user//listAll', 'user//list', 'role//list']
|
|
714
718
|
}
|
|
715
719
|
};
|
|
716
720
|
}
|
|
@@ -737,10 +741,18 @@ describe('notDomain', function() {
|
|
|
737
741
|
actions: {
|
|
738
742
|
count: 2,
|
|
739
743
|
list: ['user//list', 'role//list']
|
|
744
|
+
},
|
|
745
|
+
forms: {
|
|
746
|
+
count: 3,
|
|
747
|
+
list: ['user//listAll', 'user//list', 'role//list']
|
|
740
748
|
}
|
|
741
749
|
}
|
|
742
750
|
}
|
|
743
751
|
},
|
|
752
|
+
forms: {
|
|
753
|
+
count: 3,
|
|
754
|
+
list: ['not-user//user//listAll', 'not-user//user//list', 'not-user//role//list']
|
|
755
|
+
},
|
|
744
756
|
routes: {
|
|
745
757
|
count: 2,
|
|
746
758
|
list: ["not-user//user","not-user//role"]
|
package/test/notInit.js
CHANGED
|
@@ -7,9 +7,10 @@ describe('initialization', function() {
|
|
|
7
7
|
describe('notInit', ()=>{
|
|
8
8
|
describe('getAbsolutePath', ()=>{
|
|
9
9
|
it('all set', ()=>{
|
|
10
|
-
|
|
10
|
+
Init.options = {
|
|
11
11
|
pathToApp: '/home/admin/server/test'
|
|
12
|
-
}
|
|
12
|
+
};
|
|
13
|
+
const res = Init.getAbsolutePath('../src/index', );
|
|
13
14
|
expect(res).to.be.equal('/home/admin/server/src/index');
|
|
14
15
|
});
|
|
15
16
|
});
|
package/test/notModule.js
CHANGED
|
@@ -468,6 +468,9 @@ describe('notModule', function() {
|
|
|
468
468
|
routes: {
|
|
469
469
|
userRoute: {}
|
|
470
470
|
},
|
|
471
|
+
forms: {
|
|
472
|
+
'route//action':{}
|
|
473
|
+
},
|
|
471
474
|
getModelsStatuses() {
|
|
472
475
|
return {
|
|
473
476
|
user: {
|
|
@@ -505,6 +508,10 @@ describe('notModule', function() {
|
|
|
505
508
|
actions: {
|
|
506
509
|
count: 1,
|
|
507
510
|
list: ['route//action']
|
|
511
|
+
},
|
|
512
|
+
forms: {
|
|
513
|
+
count: 1,
|
|
514
|
+
list: ['route//action']
|
|
508
515
|
}
|
|
509
516
|
});
|
|
510
517
|
});
|