not-node 6.5.21 → 6.5.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.5.21",
3
+ "version": "6.5.24",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -135,4 +135,4 @@
135
135
  ]
136
136
  }
137
137
  }
138
- }
138
+ }
package/src/common.js CHANGED
@@ -486,3 +486,13 @@ const findSignature = (obj, signatures, strict = true, typeStrict = true) => {
486
486
  });
487
487
  };
488
488
  module.exports.findSignature = findSignature;
489
+
490
+ const isAction = (action) =>
491
+ action &&
492
+ (typeof action.run === "function" || typeof action === "function");
493
+
494
+ module.exports.isAction = isAction;
495
+
496
+ const isArrayOfActions = (list) => list.every(isAction);
497
+
498
+ module.exports.isArrayOfActions = isArrayOfActions;
@@ -321,12 +321,12 @@ module.exports = ({
321
321
  * @param {import('../types').PreparedData} prepared
322
322
  * @returns {Promise<Object>} requested document
323
323
  **/
324
- static async getOwnRaw({ targetId, identity }) {
324
+ static async getRawOwn({ targetId, identity }) {
325
325
  return await this._getOneRaw({
326
326
  targetId,
327
327
  identity,
328
328
  shouldOwn: true,
329
- action: "getOwnRaw",
329
+ action: "getRawOwn",
330
330
  });
331
331
  }
332
332
 
@@ -76,14 +76,14 @@ module.exports = ({ getLogic, before, after }) => {
76
76
  );
77
77
  }
78
78
  if (this.rootAsUser) {
79
- return await getLogic(prepared).getOwnRaw(prepared);
79
+ return await getLogic(prepared).getRawOwn(prepared);
80
80
  } else {
81
81
  return await getLogic(prepared).getRaw(prepared);
82
82
  }
83
83
  }
84
84
 
85
85
  static async getRaw(req, res, next, prepared) {
86
- return await getLogic(prepared).getOwnRaw(prepared);
86
+ return await getLogic(prepared).getRawOwn(prepared);
87
87
  }
88
88
 
89
89
  static async _update(req, res, next, prepared) {
@@ -3,6 +3,9 @@ const ActionsSetsLibrary = require("../actions.lib");
3
3
  let actionsSetsLibrary = new ActionsSetsLibrary();
4
4
 
5
5
  actionsSetsLibrary.add("standart", require("./standart"));
6
+
7
+ actionsSetsLibrary.add("standartOwn", require("./standart.own"));
8
+
6
9
  actionsSetsLibrary.add("increment", require("./increment"));
7
10
 
8
11
  module.exports = actionsSetsLibrary;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Duplicates standart set with names postpended with 'Own'
3
+ * Work with:
4
+ * beforeActionsOnCondition = [
5
+ * require("./actions.before/ownage/ownage.js").ifActionNameEndsWith_Own(),
6
+ * ],
7
+ * It modifies filtering queries before actions with names ended with 'Own'
8
+ */
9
+
10
+ const standartSet = require("./standart");
11
+
12
+ const standartOwnSet = {};
13
+ Object.keys(standartSet).forEach((actionName) => {
14
+ standartOwnSet[`${actionName}Own`] = standartSet[actionName];
15
+ });
16
+
17
+ Object.freeze(standartOwnSet);
18
+ module.exports = standartOwnSet;
@@ -4,5 +4,6 @@ const ActionsBefore = new ActionsSetsLibrary();
4
4
 
5
5
  ActionsBefore.add("ownage", require("./ownage"));
6
6
  ActionsBefore.add("standartQueries", require("./standart.queries.js"));
7
+ ActionsBefore.add("populate", require("./populate"));
7
8
 
8
9
  module.exports = ActionsBefore;
@@ -1,57 +1,86 @@
1
- const notFilter = require("not-filter");
2
- const { DOCUMENT_OWNER_FIELD_NAME } = require("../../../auth/const.js");
1
+ const {
2
+ DOCUMENT_OWNER_FIELD_NAME,
3
+ DOCUMENT_SESSION_FIELD_NAME,
4
+ } = require("../../../auth/const.js");
3
5
  const {
4
6
  OwnageExceptionIdentityUserIdIsNotDefined,
5
7
  } = require("../../../exceptions/action.js");
6
- const ModelRoutine = require("../../../model/routine.js");
8
+
9
+ const StandartQueriesBeforeAction = require("../standart.queries.js");
7
10
 
8
11
  //checks that
9
12
  module.exports = class OwnageBeforeAction {
10
13
  static #ownerFieldName = DOCUMENT_OWNER_FIELD_NAME;
14
+ static #sessionFieldName = DOCUMENT_SESSION_FIELD_NAME;
11
15
 
12
16
  static get ownerFieldName() {
13
17
  return this.#ownerFieldName;
14
18
  }
15
19
 
16
- static async run(logic, actionName, args) {
17
- const { identity, data, query, targetId, targetID } = args;
20
+ static get sessionFieldName() {
21
+ return this.#sessionFieldName;
22
+ }
23
+
24
+ static createOwnageFilterForUser(identity) {
25
+ return Object.freeze({
26
+ [OwnageBeforeAction.ownerFieldName]: identity.uid,
27
+ });
28
+ }
29
+
30
+ static createOwnageFilterForSession(identity) {
31
+ return Object.freeze({
32
+ [OwnageBeforeAction.sessionFieldName]: identity.sid,
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Returns object with filtering conditions to restrict access by owner or session
38
+ *
39
+ * @static
40
+ * @param {import('../../../types.js').notAppIdentityData} identity
41
+ * @return {object}
42
+ */
43
+ static getOwnageFilterForIdentity(identity) {
18
44
  if (identity.uid) {
19
- //if searching, counting, listing and so on
20
- //adding condition of ownership by this excat user
21
- let { filter, search } = query;
22
- if (filter) {
23
- filter = notFilter.filter.modifyRules(filter, {
24
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
25
- });
26
- if (search) {
27
- search = notFilter.filter.modifyRules(search, filter);
28
- }
29
- }
30
- args.defaultQueryById = {
31
- _id: targetId,
32
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
33
- };
34
- const Model = logic.getModel();
35
- const incFieldName = ModelRoutine.incremental(Model);
36
- if (incFieldName) {
37
- args.defaultQueryByID = {
38
- [incFieldName]: targetID,
39
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
40
- };
41
- }
42
-
43
- args.defaultQueryMany = {
44
- [OwnageBeforeAction.ownerFieldName]: identity?.uid,
45
- };
46
- //mark data as owned by
47
- if (data) {
48
- data[OwnageBeforeAction.ownerFieldName] = identity.uid;
49
- }
45
+ return OwnageBeforeAction.createOwnageFilterForUser(identity);
46
+ } else if (identity.sid) {
47
+ return OwnageBeforeAction.createOwnageFilterForSession(identity);
50
48
  } else {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ /**
54
+ *
55
+ *
56
+ * @static
57
+ * @param {import('../../logic.js')} logic
58
+ * @param {String} actionName
59
+ * @param {import('../../../types.js').PreparedData} args
60
+ */
61
+ static async run(logic, actionName, args) {
62
+ if (!args.identity) {
51
63
  throw new OwnageExceptionIdentityUserIdIsNotDefined(
52
64
  actionName,
53
- identity
65
+ undefined
54
66
  );
55
67
  }
68
+ const ownageFilter = OwnageBeforeAction.getOwnageFilterForIdentity(
69
+ args.identity
70
+ );
71
+ if (ownageFilter === null) {
72
+ throw new OwnageExceptionIdentityUserIdIsNotDefined(
73
+ actionName,
74
+ args.identity
75
+ );
76
+ }
77
+ StandartQueriesBeforeAction.modifyQueries(args, ownageFilter);
78
+ }
79
+
80
+ static ifActionNameEndsWith_Own() {
81
+ return Object.freeze({
82
+ condition: (actionName) => actionName.endsWith("Own"),
83
+ action: OwnageBeforeAction,
84
+ });
56
85
  }
57
86
  };
@@ -1,3 +1,12 @@
1
1
  module.exports = class PopulateBeforeAction {
2
- static async run(/*logic, actionName, args*/) {}
2
+ static async run(logic, actionName, args) {
3
+ try {
4
+ const { identity } = args;
5
+ args.populate = await logic.getPopulate(actionName, {
6
+ identity,
7
+ });
8
+ } catch (e) {
9
+ logic.log.error(e);
10
+ }
11
+ }
3
12
  };
@@ -1,13 +1,49 @@
1
+ const notFilter = require("not-filter");
1
2
  const ModelRoutine = require("../../model/routine.js");
2
3
 
3
4
  //adds to args object few basic queries
4
5
  module.exports = class StandartQueriesBeforeAction {
6
+ static modifyQueries(args, modificationFilter) {
7
+ const { query } = args;
8
+ let { filter, search } = query;
9
+ if (filter) {
10
+ filter = notFilter.filter.modifyRules(filter, modificationFilter);
11
+ if (search) {
12
+ search = notFilter.filter.modifyRules(search, filter);
13
+ }
14
+ }
15
+ if (args.defaultQueryById) {
16
+ args.defaultQueryById = notFilter.filter.modifyRules(
17
+ args.defaultQueryById,
18
+ modificationFilter
19
+ );
20
+ }
21
+ if (args.defaultQueryByID) {
22
+ args.defaultQueryByID = notFilter.filter.modifyRules(
23
+ args.defaultQueryByID,
24
+ modificationFilter
25
+ );
26
+ }
27
+
28
+ args.defaultQueryMany = notFilter.filter.modifyRules(
29
+ args.defaultQueryMany,
30
+ modificationFilter
31
+ );
32
+
33
+ //mark data as owned by
34
+ if (typeof args.data == "object" && args.data) {
35
+ Object.assign(args.data, modificationFilter);
36
+ }
37
+ }
38
+
5
39
  static async run(logic, actionName, args) {
6
40
  try {
7
41
  const { targetId, targetID } = args;
8
- args.defaultQueryById = {
9
- _id: targetId,
10
- };
42
+ if (targetId) {
43
+ args.defaultQueryById = {
44
+ _id: targetId,
45
+ };
46
+ }
11
47
  const Model = logic.getModel();
12
48
  const incFieldName = ModelRoutine.incremental(Model);
13
49
  if (
@@ -8,11 +8,18 @@ module.exports = ({
8
8
  MODEL_NAME,
9
9
  USER_MODEL_NAME = "not-user//User",
10
10
  actionRunner = undefined,
11
- actionsSets = ["standart"],
11
+ actionsSets = ["standart", "standartOwn"],
12
12
  actions = {},
13
13
  beforeActions = {},
14
- beforeActionsAll = [require("./actions.before/standart.queries.js")],
14
+ beforeActionsOnCondition = [
15
+ require("./actions.before/ownage/ownage.js").ifActionNameEndsWith_Own(),
16
+ ], //each item = {condition: (actionName)=>boolean, action}
17
+ beforeActionsAll = [
18
+ require("./actions.before/standart.queries.js"),
19
+ require("./actions.before/populate/populate.js"),
20
+ ],
15
21
  afterActions = {},
22
+ afterActionsOnCondition = [], //each item = {condition: (actionName)=>boolean, action}
16
23
  populateBuilders = {},
17
24
  afterActionsAll = [],
18
25
  defaultPopulate = [],
@@ -36,11 +43,28 @@ module.exports = ({
36
43
  defaultPopulate,
37
44
  });
38
45
 
46
+ //adds before/after to all
39
47
  beforeActionsAll.forEach((action) => {
40
48
  Logic.onBefore(undefined, action);
41
49
  });
50
+
42
51
  afterActionsAll.forEach((action) => Logic.onAfter(undefined, action));
43
52
 
53
+ //adds before/after to as many as satisfies condition
54
+ Object.keys(ACTIONS).forEach((actionName) => {
55
+ beforeActionsOnCondition.forEach(({ condition, action }) => {
56
+ if (condition(actionName)) {
57
+ Logic.onBefore(actionName, action);
58
+ }
59
+ });
60
+ afterActionsOnCondition.forEach(({ condition, action }) => {
61
+ if (condition(actionName)) {
62
+ Logic.onAfter(actionName, action);
63
+ }
64
+ });
65
+ });
66
+
67
+ //adds before/after per actionName
44
68
  Object.keys(beforeActions).forEach((actionName) =>
45
69
  beforeActions[actionName].forEach((action) =>
46
70
  Logic.onBefore(actionName, action)
@@ -17,8 +17,8 @@ class LogicProxied {
17
17
  actions = new Map();
18
18
  actionRunner = ActionRunner;
19
19
 
20
- populateBuilders = {};
21
- defaultPopulate = [];
20
+ #populateBuilders = {};
21
+ #defaultPopulate = [];
22
22
 
23
23
  MODEL_NAME;
24
24
  MODULE_NAME;
@@ -32,7 +32,6 @@ class LogicProxied {
32
32
  logAction;
33
33
  logDebugAction;
34
34
 
35
-
36
35
  constructor(
37
36
  actions = {},
38
37
  actionRunner = ActionRunner,
@@ -48,9 +47,15 @@ class LogicProxied {
48
47
  this.MODEL_NAME = MODEL_NAME;
49
48
  this.MODULE_NAME = MODULE_NAME;
50
49
  this.USER_MODEL_NAME = USER_MODEL_NAME;
50
+ if (defaultPopulate) {
51
+ this.#defaultPopulate = defaultPopulate;
52
+ }
53
+ Object.freeze(this.#defaultPopulate);
51
54
 
52
- defaultPopulate && (this.defaultPopulate = defaultPopulate);
53
- populateBuilders && (this.populateBuilders = populateBuilders);
55
+ if (populateBuilders) {
56
+ this.#populateBuilders = populateBuilders;
57
+ }
58
+ Object.freeze(this.#populateBuilders);
54
59
 
55
60
  actionRunner && (this.actionRunner = actionRunner);
56
61
  this.afterPipes = new NamedActionPipes(
@@ -70,7 +75,7 @@ class LogicProxied {
70
75
 
71
76
  // proxy logic, do something before each call of all methods inside class
72
77
  // like if arg passed is 3, print something additionally
73
- return this.#proxy = new Proxy(this, {
78
+ return (this.#proxy = new Proxy(this, {
74
79
  get(target, prop) {
75
80
  if (target.actions.has(prop)) {
76
81
  return target.#getActionRunner(prop);
@@ -78,7 +83,7 @@ class LogicProxied {
78
83
  return target[prop];
79
84
  }
80
85
  },
81
- });
86
+ }));
82
87
  }
83
88
 
84
89
  #initTools(target) {
@@ -105,9 +110,7 @@ class LogicProxied {
105
110
  this.log &&
106
111
  this.log.debug(
107
112
  new Date(),
108
- `${this.MODULE_NAME}//Logic//${
109
- this.MODEL_NAME
110
- }//${action}`,
113
+ `${this.MODULE_NAME}//Logic//${this.MODEL_NAME}//${action}`,
111
114
  identity?.ip,
112
115
  identity?.root
113
116
  );
@@ -132,16 +135,16 @@ class LogicProxied {
132
135
 
133
136
  async getPopulate(actionName, prepared) {
134
137
  if (
135
- this.populateBuilders &&
136
- objHas(this.populateBuilders, actionName) &&
137
- isFunc(this.populateBuilders[actionName])
138
+ this.#populateBuilders &&
139
+ objHas(this.#populateBuilders, actionName) &&
140
+ isFunc(this.#populateBuilders[actionName])
138
141
  ) {
139
142
  return await executeFunctionAsAsync(
140
- this.populateBuilders[actionName],
143
+ this.#populateBuilders[actionName],
141
144
  [prepared]
142
145
  );
143
146
  }
144
- return this.defaultPopulate;
147
+ return [...this.#defaultPopulate];
145
148
  }
146
149
 
147
150
  #getActionRunner(actionName) {