not-node 5.1.28 → 5.1.31
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/package.json +1 -1
- package/src/fields/index.js +9 -1
- package/src/generic/form.getByID.js +31 -0
- package/src/generic/index.js +27 -0
- package/src/generic/logic.js +94 -0
- package/src/generic/route.js +9 -0
- package/src/model/default.js +3 -1
package/package.json
CHANGED
package/src/fields/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const { objHas } = require("../common");
|
|
|
7
7
|
const DEFAULT_TYPE = "ui";
|
|
8
8
|
const DEFAULT_FROM = ":FIELDS";
|
|
9
9
|
const DEFAULT_TO = ":thisSchema";
|
|
10
|
+
const DEFAULT_SPLITER = "//";
|
|
10
11
|
|
|
11
12
|
module.exports.initFileSchemaFromFields = ({
|
|
12
13
|
app,
|
|
@@ -79,6 +80,7 @@ module.exports.initSchemaField = (
|
|
|
79
80
|
* ['destFieldName', {full: true, field: 'content'}] - form 2
|
|
80
81
|
* ['destFieldName', 'srcFieldName'] //field alias, form 3
|
|
81
82
|
* ['destFieldName', {mutation: 'content'}, 'srcFieldName']// - form 4
|
|
83
|
+
* 'module-name//field-name' - form 5, equal to ['field-name', 'module-name//field-name']
|
|
82
84
|
**/
|
|
83
85
|
const parseFieldDescription = (field) => {
|
|
84
86
|
let srcName,
|
|
@@ -98,7 +100,13 @@ const parseFieldDescription = (field) => {
|
|
|
98
100
|
srcName = field[2];
|
|
99
101
|
}
|
|
100
102
|
} else {
|
|
101
|
-
|
|
103
|
+
if (field.includes(DEFAULT_SPLITER)) {
|
|
104
|
+
//form 5
|
|
105
|
+
destName = field.split(DEFAULT_SPLITER)[0];
|
|
106
|
+
srcName = field;
|
|
107
|
+
} else {
|
|
108
|
+
destName = srcName = field; //form 1
|
|
109
|
+
}
|
|
102
110
|
}
|
|
103
111
|
return {
|
|
104
112
|
srcName,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//DB related validation tools
|
|
2
|
+
const Form = require("../form/form");
|
|
3
|
+
const { firstLetterToUpper, firstLetterToLower } = require("../common");
|
|
4
|
+
//not-node
|
|
5
|
+
const { getIP } = require("../auth");
|
|
6
|
+
//form
|
|
7
|
+
const FIELDS = [
|
|
8
|
+
["targetID", { required: true }, "not-node//ID"],
|
|
9
|
+
["activeUserId", { required: true }, "not-node//objectId"],
|
|
10
|
+
["activeUser", "not-node//requiredObject"],
|
|
11
|
+
["ip", "not-node//ip"],
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
module.exports = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
|
|
15
|
+
const FORM_NAME = `${MODULE_NAME}:${firstLetterToUpper(actionName)}Form`;
|
|
16
|
+
return class extends Form {
|
|
17
|
+
constructor({ app }) {
|
|
18
|
+
super({ FIELDS, FORM_NAME, app });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
extract(req) {
|
|
22
|
+
const incField = `${firstLetterToLower(MODEL_NAME)}ID`;
|
|
23
|
+
return {
|
|
24
|
+
targetId: req.params[incField],
|
|
25
|
+
activeUser: req.user,
|
|
26
|
+
activeUserId: req.user._id,
|
|
27
|
+
ip: getIP(req),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
package/src/generic/index.js
CHANGED
|
@@ -2,3 +2,30 @@ module.exports.GenericLogic = require("./logic.js");
|
|
|
2
2
|
module.exports.GenericRoute = require("./route.js");
|
|
3
3
|
module.exports.GenericGetByIdForm = require("./form.getById.js");
|
|
4
4
|
module.exports.GenericListAndCountForm = require("./form.listAndCount.js");
|
|
5
|
+
|
|
6
|
+
const FORMS = {};
|
|
7
|
+
module.exports.setCustomGenericForm = (name, factory) => {
|
|
8
|
+
FORMS[name] = factory;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
module.exports.getCustomGenericForm = (name) => {
|
|
12
|
+
return FORMS[name];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const LOGICS = {};
|
|
16
|
+
module.exports.setCustomGenericLogic = (name, factory) => {
|
|
17
|
+
LOGICS[name] = factory;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports.getCustomGenericLogic = (name) => {
|
|
21
|
+
return LOGICS[name];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ROUTES = {};
|
|
25
|
+
module.exports.setCustomGenericRoute = (name, factory) => {
|
|
26
|
+
ROUTES[name] = factory;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
module.exports.getCustomGenericRoute = (name) => {
|
|
30
|
+
return ROUTES[name];
|
|
31
|
+
};
|
package/src/generic/logic.js
CHANGED
|
@@ -307,6 +307,100 @@ module.exports = ({
|
|
|
307
307
|
});
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
+
/**
|
|
311
|
+
* get item with populated sub-documents
|
|
312
|
+
* @param {Object} prepared
|
|
313
|
+
* @param {Object} prepared.targetID target item ID
|
|
314
|
+
* @param {Object} prepared.activeUser current user info
|
|
315
|
+
* @param {string} prepared.ip current user ip
|
|
316
|
+
* @param {boolean} prepared.root current user is root
|
|
317
|
+
* @param {boolean} prepared.shouldOwn if user should be owner of target
|
|
318
|
+
* @returns {Promise<Object>} requested document
|
|
319
|
+
**/
|
|
320
|
+
static async _getOneByID({
|
|
321
|
+
targetID,
|
|
322
|
+
action,
|
|
323
|
+
ip,
|
|
324
|
+
root,
|
|
325
|
+
activeUser,
|
|
326
|
+
shouldOwn = true,
|
|
327
|
+
}) {
|
|
328
|
+
Log.debug(
|
|
329
|
+
`${MODULE_NAME}//Logic//${MODEL_NAME}//${action}`,
|
|
330
|
+
targetID,
|
|
331
|
+
ip,
|
|
332
|
+
root
|
|
333
|
+
);
|
|
334
|
+
let query = {};
|
|
335
|
+
if (shouldOwn) {
|
|
336
|
+
query[ownerFieldName] = activeUser._id;
|
|
337
|
+
}
|
|
338
|
+
let populate = getPopulate(action, {
|
|
339
|
+
targetID,
|
|
340
|
+
activeUser,
|
|
341
|
+
ip,
|
|
342
|
+
root,
|
|
343
|
+
});
|
|
344
|
+
const result = await getModel().getOneByID(
|
|
345
|
+
targetID,
|
|
346
|
+
query,
|
|
347
|
+
populate
|
|
348
|
+
);
|
|
349
|
+
LogAction(
|
|
350
|
+
{
|
|
351
|
+
action,
|
|
352
|
+
by: activeUser._id,
|
|
353
|
+
role: activeUser.role,
|
|
354
|
+
ip,
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
targetID,
|
|
358
|
+
version: result.__version,
|
|
359
|
+
}
|
|
360
|
+
);
|
|
361
|
+
return result;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* get item with populated sub-documents
|
|
366
|
+
* @param {Object} prepared
|
|
367
|
+
* @param {Object} prepared.targetID target item ID
|
|
368
|
+
* @param {Object} prepared.activeUser current user info
|
|
369
|
+
* @param {string} prepared.ip current user ip
|
|
370
|
+
* @param {boolean} prepared.root current user is root
|
|
371
|
+
* @returns {Promise<Object>} requested document
|
|
372
|
+
**/
|
|
373
|
+
static async getByID({ targetID, activeUser, ip, root = false }) {
|
|
374
|
+
return await this._getOneByID({
|
|
375
|
+
targetID,
|
|
376
|
+
action: "getByID",
|
|
377
|
+
activeUser,
|
|
378
|
+
ip,
|
|
379
|
+
root,
|
|
380
|
+
shouldOwn: false,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* get activeUser own item with populated sub-documents
|
|
386
|
+
* @param {Object} prepared
|
|
387
|
+
* @param {Object} prepared.targetID target item ID
|
|
388
|
+
* @param {Object} prepared.activeUser current user info
|
|
389
|
+
* @param {string} prepared.ip current user ip
|
|
390
|
+
* @param {boolean} prepared.root current user is root
|
|
391
|
+
* @returns {Promise<Object>} requested document
|
|
392
|
+
**/
|
|
393
|
+
static async getByIDOwn({ targetID, activeUser, ip, root = false }) {
|
|
394
|
+
return await this._getOneByID({
|
|
395
|
+
targetID,
|
|
396
|
+
action: "getByIDOwn",
|
|
397
|
+
activeUser,
|
|
398
|
+
ip,
|
|
399
|
+
root,
|
|
400
|
+
shouldOwn: true,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
310
404
|
/**
|
|
311
405
|
* get item without populated sub-documents
|
|
312
406
|
* @param {Object} prepared
|
package/src/generic/route.js
CHANGED
|
@@ -26,6 +26,15 @@ module.exports = ({ getLogic, before, after }) => {
|
|
|
26
26
|
return await getLogic().getOwn(prepared);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
static async _getByID(req, res, next, prepared) {
|
|
30
|
+
prepared.root = true;
|
|
31
|
+
return await getLogic().getByID(prepared);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static async getByID(req, res, next, prepared) {
|
|
35
|
+
return await getLogic().getByIDOwn(prepared);
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
static async _getRaw(req, res, next, prepared) {
|
|
30
39
|
prepared.root = true;
|
|
31
40
|
return await getLogic().getRaw(prepared);
|
package/src/model/default.js
CHANGED
|
@@ -111,9 +111,10 @@ function getOne(id, population = [], condition = {}) {
|
|
|
111
111
|
* @static
|
|
112
112
|
* @param {number} ID some unique numeric identificator
|
|
113
113
|
* @param {Object} condition optional if needed additional condition
|
|
114
|
+
* @param {Array} population optional if needed population of some fields
|
|
114
115
|
* @return {Promise} Promise of document, if increment is OFF - then Promise.resolve(null)
|
|
115
116
|
**/
|
|
116
|
-
function getOneByID(ID, condition = {}) {
|
|
117
|
+
function getOneByID(ID, condition = {}, population = []) {
|
|
117
118
|
if (this.schema.statics.__incField) {
|
|
118
119
|
let by, query;
|
|
119
120
|
if (this.schema.statics.__versioning) {
|
|
@@ -129,6 +130,7 @@ function getOneByID(ID, condition = {}) {
|
|
|
129
130
|
}
|
|
130
131
|
by[this.schema.statics.__incField] = ID;
|
|
131
132
|
query = this.findOne(by);
|
|
133
|
+
populateQuery(query, population, this.schema.statics.__versioning);
|
|
132
134
|
return query.exec();
|
|
133
135
|
} else {
|
|
134
136
|
return Promise.resolve(null);
|