not-node 5.1.30 → 5.1.33
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/scaffold/base.js +0 -0
- package/scaffold/field.js +0 -0
- package/scaffold/form.js +0 -0
- package/scaffold/logic.js +0 -0
- package/scaffold/model.js +0 -0
- package/scaffold/module.js +0 -0
- package/scaffold/route.js +0 -0
- package/src/form/form.js +35 -0
- package/src/generic/form.getByID.js +31 -0
- package/src/generic/index.js +1 -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/scaffold/base.js
ADDED
|
File without changes
|
|
File without changes
|
package/scaffold/form.js
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/src/form/form.js
CHANGED
|
@@ -243,6 +243,41 @@ class Form {
|
|
|
243
243
|
}
|
|
244
244
|
return results;
|
|
245
245
|
}
|
|
246
|
+
|
|
247
|
+
createInstructionFromRouteActionFields(
|
|
248
|
+
req,
|
|
249
|
+
mainInstruction = "fromBody",
|
|
250
|
+
exceptions = {}
|
|
251
|
+
) {
|
|
252
|
+
const result = {};
|
|
253
|
+
if (
|
|
254
|
+
req?.notRouteData?.actionData?.fields &&
|
|
255
|
+
Array.isArray(req.notRouteData.actionData.fields)
|
|
256
|
+
) {
|
|
257
|
+
const fields = req.notRouteData.actionData.fields.flat(2);
|
|
258
|
+
fields.forEach((fieldName) => {
|
|
259
|
+
if (objHas(exceptions, fieldName)) {
|
|
260
|
+
result[fieldName] = exceptions[fieldName];
|
|
261
|
+
} else {
|
|
262
|
+
result[fieldName] = mainInstruction;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
extractByInstructionsFromRouteActionFields(
|
|
270
|
+
req,
|
|
271
|
+
mainInstruction = "fromBody",
|
|
272
|
+
exceptions = {}
|
|
273
|
+
) {
|
|
274
|
+
const instructions = this.createInstructionFromRouteActionFields(
|
|
275
|
+
req,
|
|
276
|
+
mainInstruction,
|
|
277
|
+
exceptions
|
|
278
|
+
);
|
|
279
|
+
return this.extractByInstructions(req, instructions);
|
|
280
|
+
}
|
|
246
281
|
}
|
|
247
282
|
|
|
248
283
|
module.exports = Form;
|
|
@@ -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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
module.exports.GenericLogic = require("./logic.js");
|
|
2
2
|
module.exports.GenericRoute = require("./route.js");
|
|
3
3
|
module.exports.GenericGetByIdForm = require("./form.getById.js");
|
|
4
|
+
module.exports.GenericGetByIDForm = require("./form.getByID.js");
|
|
4
5
|
module.exports.GenericListAndCountForm = require("./form.listAndCount.js");
|
|
5
6
|
|
|
6
7
|
const FORMS = {};
|
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);
|