not-node 6.3.51 → 6.3.53
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/core/fields/createdAt.js +1 -1
- package/src/exceptions/action.js +100 -0
- package/src/exceptions/db.js +5 -4
- package/src/exceptions/logic.js +14 -0
- package/src/logic/action.runner.js +15 -0
- package/src/logic/actions/increment/getByID.js +23 -0
- package/src/logic/actions/increment/index.js +1 -0
- package/src/logic/actions/index.js +8 -0
- package/src/logic/actions/standart/count.js +24 -0
- package/src/logic/actions/standart/create.js +21 -0
- package/src/logic/actions/standart/delete.js +28 -0
- package/src/logic/actions/standart/get.js +38 -0
- package/src/logic/actions/standart/getRaw.js +31 -0
- package/src/logic/actions/standart/index.js +11 -0
- package/src/logic/actions/standart/list.js +27 -0
- package/src/logic/actions/standart/listAll.js +21 -0
- package/src/logic/actions/standart/listAndCount.js +29 -0
- package/src/logic/actions/standart/update.js +30 -0
- package/src/logic/actions.before/index.js +8 -0
- package/src/logic/actions.before/ownage/index.js +3 -0
- package/src/logic/actions.before/ownage/ownage.js +57 -0
- package/src/logic/actions.before/populate/index.js +3 -0
- package/src/logic/actions.before/populate/populate.js +3 -0
- package/src/logic/actions.before/standart.queries.js +28 -0
- package/src/logic/actions.lib.js +22 -0
- package/src/logic/generic.action.after.js +6 -0
- package/src/logic/generic.action.before.js +6 -0
- package/src/logic/generic.js +53 -0
- package/src/logic/logic.js +204 -0
- package/src/logic/named.actions.pipes.js +106 -0
- package/src/model/default.js +48 -8
- package/src/model/utils.js +21 -12
- package/src/types.js +48 -0
- package/test/model/default.js +5 -0
- package/test/model/proto.js +2 -0
- package/test/notModel.js +2 -2
package/package.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const notRequestError = require("not-error/src/request.error.node.cjs");
|
|
2
|
+
|
|
3
|
+
//delete wasnt successful, or error, or count of deleted documents dont match requested
|
|
4
|
+
class ActionExceptionWrongType extends notRequestError {
|
|
5
|
+
constructor({ params = {}, cause = null } = {}) {
|
|
6
|
+
super("Logic cant run this action", { code: 505, params }, cause);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
module.exports.ActionExceptionWrongType = ActionExceptionWrongType;
|
|
10
|
+
|
|
11
|
+
class ActionExceptionPipeExecutionError extends notRequestError {
|
|
12
|
+
constructor({ params = {}, cause = null } = {}) {
|
|
13
|
+
super(
|
|
14
|
+
"Logic cant run this actions sequence",
|
|
15
|
+
{ code: 505, params },
|
|
16
|
+
cause
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
module.exports.ActionExceptionPipeExecutionError =
|
|
21
|
+
ActionExceptionPipeExecutionError;
|
|
22
|
+
|
|
23
|
+
class OwnageExceptionIdentityUserIdIsNotDefined extends notRequestError {
|
|
24
|
+
constructor(actionName, identity) {
|
|
25
|
+
super(
|
|
26
|
+
"User identity `uid` is not defined, ownage couldnt be determined",
|
|
27
|
+
{ code: 505, params: { actionName, identity } },
|
|
28
|
+
null
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
module.exports.OwnageExceptionIdentityUserIdIsNotDefined =
|
|
33
|
+
OwnageExceptionIdentityUserIdIsNotDefined;
|
|
34
|
+
|
|
35
|
+
class LogicDeleteActionException extends notRequestError {
|
|
36
|
+
constructor(params, cause) {
|
|
37
|
+
super("Logic Delete Action exception", { code: 505, params }, cause);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
module.exports.LogicDeleteActionException = LogicDeleteActionException;
|
|
41
|
+
|
|
42
|
+
class LogicCreateActionException extends notRequestError {
|
|
43
|
+
constructor(params, cause) {
|
|
44
|
+
super("Logic Create Action exception", { code: 505, params }, cause);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
module.exports.LogicCreateActionException = LogicCreateActionException;
|
|
48
|
+
class LogicUpdateActionException extends notRequestError {
|
|
49
|
+
constructor(params, cause) {
|
|
50
|
+
super("Logic Update Action exception", { code: 505, params }, cause);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
module.exports.LogicUpdateActionException = LogicUpdateActionException;
|
|
54
|
+
|
|
55
|
+
class LogicCountActionException extends notRequestError {
|
|
56
|
+
constructor(params, cause) {
|
|
57
|
+
super("Logic Count Action exception", { code: 505, params }, cause);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
module.exports.LogicCountActionException = LogicCountActionException;
|
|
61
|
+
|
|
62
|
+
class LogicListAndCountActionException extends notRequestError {
|
|
63
|
+
constructor(params, cause) {
|
|
64
|
+
super(
|
|
65
|
+
"Logic ListAndCount Action exception",
|
|
66
|
+
{ code: 505, params },
|
|
67
|
+
cause
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
module.exports.LogicListAndCountActionException =
|
|
72
|
+
LogicListAndCountActionException;
|
|
73
|
+
|
|
74
|
+
class LogicGetActionException extends notRequestError {
|
|
75
|
+
constructor(params, cause) {
|
|
76
|
+
super("Logic Get Action exception", { code: 505, params }, cause);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
module.exports.LogicGetActionException = LogicGetActionException;
|
|
80
|
+
|
|
81
|
+
class LogicGetRawActionException extends notRequestError {
|
|
82
|
+
constructor(params, cause) {
|
|
83
|
+
super("Logic GetRaw Action exception", { code: 505, params }, cause);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
module.exports.LogicGetRawActionException = LogicGetRawActionException;
|
|
87
|
+
|
|
88
|
+
class LogicListActionException extends notRequestError {
|
|
89
|
+
constructor(params, cause) {
|
|
90
|
+
super("Logic List Action exception", { code: 505, params }, cause);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
module.exports.LogicListActionException = LogicListActionException;
|
|
94
|
+
|
|
95
|
+
class LogicListAllActionException extends notRequestError {
|
|
96
|
+
constructor(params, cause) {
|
|
97
|
+
super("Logic ListAll Action exception", { code: 505, params }, cause);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
module.exports.LogicListAllActionException = LogicListAllActionException;
|
package/src/exceptions/db.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
const { notRequestError } = require("not-error");
|
|
1
|
+
const { notError, notRequestError } = require("not-error/src/index.cjs");
|
|
2
2
|
|
|
3
3
|
//delete wasnt successful, or error, or count of deleted documents dont match requested
|
|
4
|
-
class DBExceptionDeleteWasNotSuccessful extends
|
|
5
|
-
constructor(
|
|
6
|
-
super("DB Delete Was Not Successful",
|
|
4
|
+
class DBExceptionDeleteWasNotSuccessful extends notError {
|
|
5
|
+
constructor(result) {
|
|
6
|
+
super("DB Delete Was Not Successful", result);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
|
|
9
10
|
module.exports.DBExceptionDeleteWasNotSuccessful =
|
|
10
11
|
DBExceptionDeleteWasNotSuccessful;
|
|
11
12
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const notRequestError = require("not-error/src/request.error.node.cjs");
|
|
2
|
+
|
|
3
|
+
//delete wasnt successful, or error, or count of deleted documents dont match requested
|
|
4
|
+
class LogicExceptionActionExecutionError extends notRequestError {
|
|
5
|
+
constructor(moduleName, modelName, actionName, cause) {
|
|
6
|
+
super(
|
|
7
|
+
"Logic cant run this action",
|
|
8
|
+
{ code: 505, params: { moduleName, modelName, actionName } },
|
|
9
|
+
cause
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
module.exports.LogicExceptionActionExecutionError =
|
|
14
|
+
LogicExceptionActionExecutionError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const { ActionExceptionWrongType } = require("../exceptions/action.js");
|
|
2
|
+
|
|
3
|
+
class ActionRunner {
|
|
4
|
+
static async run(action, params) {
|
|
5
|
+
//any static class with static run method
|
|
6
|
+
if (action && typeof action.run === "function") {
|
|
7
|
+
return await action.run(...params);
|
|
8
|
+
} else if (typeof action === "function") {
|
|
9
|
+
return await action(...params);
|
|
10
|
+
}
|
|
11
|
+
throw new ActionExceptionWrongType();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = ActionRunner;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module.exports = class GetByIDAction {
|
|
2
|
+
static async run(
|
|
3
|
+
logic,
|
|
4
|
+
actionName,
|
|
5
|
+
{ targetID, identity, defaultQueryByID }
|
|
6
|
+
) {
|
|
7
|
+
logic.logDebugAction(actionName, identity);
|
|
8
|
+
|
|
9
|
+
let populate = await logic.getPopulate(actionName, {
|
|
10
|
+
targetID,
|
|
11
|
+
identity,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const result = await logic
|
|
15
|
+
.getModel()
|
|
16
|
+
.getOneByID(targetID, defaultQueryByID, populate);
|
|
17
|
+
logic.logAction(actionName, identity, {
|
|
18
|
+
targetID,
|
|
19
|
+
version: result?.__version,
|
|
20
|
+
});
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = { getByID: require("./getByID.js") };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const ActionsSetsLibrary = require("../actions.lib");
|
|
2
|
+
|
|
3
|
+
let actionsSetsLibrary = new ActionsSetsLibrary();
|
|
4
|
+
|
|
5
|
+
actionsSetsLibrary.add("standart", require("./standart"));
|
|
6
|
+
actionsSetsLibrary.add("increment", require("./increment"));
|
|
7
|
+
|
|
8
|
+
module.exports = actionsSetsLibrary;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const { LogicCountActionException } = require("../../../exceptions/action");
|
|
2
|
+
|
|
3
|
+
module.exports = class CountAction {
|
|
4
|
+
static async run(logic, actionName, { identity, query }) {
|
|
5
|
+
try {
|
|
6
|
+
logic.logDebugAction(actionName, identity);
|
|
7
|
+
const { filter, search } = query;
|
|
8
|
+
const result = await logic
|
|
9
|
+
.getModel()
|
|
10
|
+
.countWithFilter(search || filter);
|
|
11
|
+
logic.logAction(actionName, identity);
|
|
12
|
+
return result;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
throw new LogicCountActionException(
|
|
15
|
+
{
|
|
16
|
+
query,
|
|
17
|
+
activeUserId: identity?.uid,
|
|
18
|
+
role: identity?.role,
|
|
19
|
+
},
|
|
20
|
+
e
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { LogicCreateActionException } = require("../../../exceptions/action");
|
|
2
|
+
module.exports = class CreateAction {
|
|
3
|
+
static async run(logic, actionName, { identity, data }) {
|
|
4
|
+
try {
|
|
5
|
+
logic.logDebugAction(actionName, identity);
|
|
6
|
+
const result = await logic.getModel().add(data);
|
|
7
|
+
logic.logAction(actionName, identity, {
|
|
8
|
+
targetId: result?._id,
|
|
9
|
+
});
|
|
10
|
+
return result;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
throw new LogicCreateActionException(
|
|
13
|
+
{
|
|
14
|
+
activeUserId: identity?.uid,
|
|
15
|
+
role: identity?.role,
|
|
16
|
+
},
|
|
17
|
+
e
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { LogicDeleteActionException } = require("../../../exceptions/action.js");
|
|
2
|
+
module.exports = class DeleteAction {
|
|
3
|
+
static async run(
|
|
4
|
+
logic,
|
|
5
|
+
actionName,
|
|
6
|
+
{ identity, defaultQueryById, targetId }
|
|
7
|
+
) {
|
|
8
|
+
logic.logDebugAction(actionName, identity);
|
|
9
|
+
/** @type {import('../../../types.js').notAppModel } */
|
|
10
|
+
const model = logic.getModel();
|
|
11
|
+
try {
|
|
12
|
+
await model.removeOne(defaultQueryById);
|
|
13
|
+
logic.logAction(actionName, identity, {
|
|
14
|
+
query: defaultQueryById,
|
|
15
|
+
});
|
|
16
|
+
} catch (e) {
|
|
17
|
+
throw new LogicDeleteActionException(
|
|
18
|
+
{
|
|
19
|
+
targetId,
|
|
20
|
+
query: defaultQueryById,
|
|
21
|
+
activeUserId: identity?.uid,
|
|
22
|
+
role: identity?.role,
|
|
23
|
+
},
|
|
24
|
+
e
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { LogicGetActionException } = require("../../../exceptions/action");
|
|
2
|
+
|
|
3
|
+
module.exports = class GetAction {
|
|
4
|
+
static async run(
|
|
5
|
+
logic,
|
|
6
|
+
actionName,
|
|
7
|
+
{ identity, defaultQueryById, targetId }
|
|
8
|
+
) {
|
|
9
|
+
try {
|
|
10
|
+
logic.logDebugAction(actionName, identity);
|
|
11
|
+
|
|
12
|
+
let populate = await logic.getPopulate(actionName, {
|
|
13
|
+
targetId,
|
|
14
|
+
identity,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const result = await logic
|
|
18
|
+
.getModel()
|
|
19
|
+
.getOne(targetId, populate, defaultQueryById);
|
|
20
|
+
|
|
21
|
+
logic.logAction(actionName, identity, {
|
|
22
|
+
targetId,
|
|
23
|
+
version: result?.__version,
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw new LogicGetActionException(
|
|
28
|
+
{
|
|
29
|
+
targetId,
|
|
30
|
+
query: defaultQueryById,
|
|
31
|
+
activeUserId: identity?.uid,
|
|
32
|
+
role: identity?.role,
|
|
33
|
+
},
|
|
34
|
+
e
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { LogicGetRawActionException } = require("../../../exceptions/action");
|
|
2
|
+
|
|
3
|
+
module.exports = class GetRawAction {
|
|
4
|
+
static async run(
|
|
5
|
+
logic,
|
|
6
|
+
actionName,
|
|
7
|
+
{ identity, defaultQueryById, targetId }
|
|
8
|
+
) {
|
|
9
|
+
try {
|
|
10
|
+
logic.logDebugAction(actionName, identity);
|
|
11
|
+
const result = await logic
|
|
12
|
+
.getModel()
|
|
13
|
+
.getOneRaw(targetId, defaultQueryById);
|
|
14
|
+
logic.logAction(actionName, identity, {
|
|
15
|
+
targetId,
|
|
16
|
+
version: result?.__version,
|
|
17
|
+
});
|
|
18
|
+
return result;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
throw new LogicGetRawActionException(
|
|
21
|
+
{
|
|
22
|
+
targetId,
|
|
23
|
+
query: defaultQueryById,
|
|
24
|
+
activeUserId: identity?.uid,
|
|
25
|
+
role: identity?.role,
|
|
26
|
+
},
|
|
27
|
+
e
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
count: require("./count.js"),
|
|
3
|
+
create: require("./create.js"),
|
|
4
|
+
delete: require("./delete.js"),
|
|
5
|
+
get: require("./get.js"),
|
|
6
|
+
getRaw: require("./getRaw.js"),
|
|
7
|
+
list: require("./list.js"),
|
|
8
|
+
listAll: require("./listAll.js"),
|
|
9
|
+
listAndCount: require("./listAndCount.js"),
|
|
10
|
+
update: require("./update.js"),
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const { LogicListActionException } = require("../../../exceptions/action");
|
|
2
|
+
|
|
3
|
+
module.exports = class ListAction {
|
|
4
|
+
static async run(logic, actionName, { identity, query }) {
|
|
5
|
+
try {
|
|
6
|
+
logic.logDebugAction(actionName, identity);
|
|
7
|
+
const { skip, size, sorter, filter } = query;
|
|
8
|
+
let populate = await logic.getPopulate(actionName, {
|
|
9
|
+
identity,
|
|
10
|
+
});
|
|
11
|
+
const result = await logic
|
|
12
|
+
.getModel()
|
|
13
|
+
.listAndPopulate(skip, size, sorter, filter, populate);
|
|
14
|
+
logic.logAction(actionName, identity, {});
|
|
15
|
+
return result;
|
|
16
|
+
} catch (e) {
|
|
17
|
+
throw new LogicListActionException(
|
|
18
|
+
{
|
|
19
|
+
query,
|
|
20
|
+
activeUserId: identity?.uid,
|
|
21
|
+
role: identity?.role,
|
|
22
|
+
},
|
|
23
|
+
e
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { LogicListAllActionException } = require("../../../exceptions/action");
|
|
2
|
+
|
|
3
|
+
module.exports = class ListAllAction {
|
|
4
|
+
static async run(logic, actionName, { identity, defaultQueryMany }) {
|
|
5
|
+
try {
|
|
6
|
+
logic.logDebugAction(actionName, identity);
|
|
7
|
+
const result = await logic.getModel().listAll(defaultQueryMany);
|
|
8
|
+
logic.logAction(actionName, identity, {});
|
|
9
|
+
return result;
|
|
10
|
+
} catch (e) {
|
|
11
|
+
throw new LogicListAllActionException(
|
|
12
|
+
{
|
|
13
|
+
query: defaultQueryMany,
|
|
14
|
+
activeUserId: identity?.uid,
|
|
15
|
+
role: identity?.role,
|
|
16
|
+
},
|
|
17
|
+
e
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const {
|
|
2
|
+
LogicListAndCountActionException,
|
|
3
|
+
} = require("../../../exceptions/action");
|
|
4
|
+
|
|
5
|
+
module.exports = class ListAndCountAction {
|
|
6
|
+
static async run(logic, actionName, { identity, query }) {
|
|
7
|
+
try {
|
|
8
|
+
logic.logDebugAction(actionName, identity);
|
|
9
|
+
const { skip, size, sorter, filter, search } = query;
|
|
10
|
+
let populate = await logic.getPopulate(actionName, {
|
|
11
|
+
identity,
|
|
12
|
+
});
|
|
13
|
+
const result = await logic
|
|
14
|
+
.getModel()
|
|
15
|
+
.listAndCount(skip, size, sorter, filter, search, populate);
|
|
16
|
+
logic.logAction(actionName, identity, {});
|
|
17
|
+
return result;
|
|
18
|
+
} catch (e) {
|
|
19
|
+
throw new LogicListAndCountActionException(
|
|
20
|
+
{
|
|
21
|
+
query,
|
|
22
|
+
activeUserId: identity?.uid,
|
|
23
|
+
role: identity?.role,
|
|
24
|
+
},
|
|
25
|
+
e
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const { LogicUpdateActionException } = require("../../../exceptions/action");
|
|
2
|
+
module.exports = class UpdateAction {
|
|
3
|
+
static async run(
|
|
4
|
+
logic,
|
|
5
|
+
actionName,
|
|
6
|
+
{ identity, data, targetId, defaultQueryById }
|
|
7
|
+
) {
|
|
8
|
+
try {
|
|
9
|
+
logic.logDebugAction(actionName, identity);
|
|
10
|
+
const result = await logic
|
|
11
|
+
.getModel()
|
|
12
|
+
.update(defaultQueryById, data);
|
|
13
|
+
logic.logAction(actionName, identity, {
|
|
14
|
+
targetId,
|
|
15
|
+
version: result?.__version,
|
|
16
|
+
});
|
|
17
|
+
return result;
|
|
18
|
+
} catch (e) {
|
|
19
|
+
throw new LogicUpdateActionException(
|
|
20
|
+
{
|
|
21
|
+
targetId,
|
|
22
|
+
query: defaultQueryById,
|
|
23
|
+
activeUserId: identity?.uid,
|
|
24
|
+
role: identity?.role,
|
|
25
|
+
},
|
|
26
|
+
e
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const ActionsSetsLibrary = require("../actions.lib.js");
|
|
2
|
+
|
|
3
|
+
const ActionsBefore = new ActionsSetsLibrary();
|
|
4
|
+
|
|
5
|
+
ActionsBefore.add("ownage", require("./ownage"));
|
|
6
|
+
ActionsBefore.add("standartQueries", require("./standart.queries.js"));
|
|
7
|
+
|
|
8
|
+
module.exports = ActionsBefore;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const notFilter = require("not-filter");
|
|
2
|
+
const { DOCUMENT_OWNER_FIELD_NAME } = require("../../../auth/const.js");
|
|
3
|
+
const {
|
|
4
|
+
OwnageExceptionIdentityUserIdIsNotDefined,
|
|
5
|
+
} = require("../../../exceptions/action.js");
|
|
6
|
+
const ModelRoutine = require("../../../model/routine.js");
|
|
7
|
+
|
|
8
|
+
//checks that
|
|
9
|
+
module.exports = class OwnageBeforeAction {
|
|
10
|
+
static #ownerFieldName = DOCUMENT_OWNER_FIELD_NAME;
|
|
11
|
+
|
|
12
|
+
static get ownerFieldName() {
|
|
13
|
+
return this.#ownerFieldName;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static async run(logic, actionName, args) {
|
|
17
|
+
const { identity, data, query, targetId, targetID } = args;
|
|
18
|
+
if (identity.uid) {
|
|
19
|
+
//if searching, counting, listing and so on
|
|
20
|
+
//adding condition of ownership by this excat user
|
|
21
|
+
const { filter, search } = query;
|
|
22
|
+
if (filter) {
|
|
23
|
+
notFilter.filter.modifyRules(filter, {
|
|
24
|
+
[OwnageBeforeAction.ownerFieldName]: identity?.uid,
|
|
25
|
+
});
|
|
26
|
+
if (search) {
|
|
27
|
+
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
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
throw new OwnageExceptionIdentityUserIdIsNotDefined(
|
|
52
|
+
actionName,
|
|
53
|
+
identity
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const ModelRoutine = require("../../model/routine.js");
|
|
2
|
+
|
|
3
|
+
//adds to args object few basic queries
|
|
4
|
+
module.exports = class StandartQueriesBeforeAction {
|
|
5
|
+
static async run(logic, actionName, args) {
|
|
6
|
+
try {
|
|
7
|
+
const { targetId, targetID } = args;
|
|
8
|
+
args.defaultQueryById = {
|
|
9
|
+
_id: targetId,
|
|
10
|
+
};
|
|
11
|
+
const Model = logic.getModel();
|
|
12
|
+
const incFieldName = ModelRoutine.incremental(Model);
|
|
13
|
+
if (
|
|
14
|
+
incFieldName &&
|
|
15
|
+
typeof targetID === "number" &&
|
|
16
|
+
!isNaN(targetID) &&
|
|
17
|
+
targetID
|
|
18
|
+
) {
|
|
19
|
+
args.defaultQueryByID = {
|
|
20
|
+
[incFieldName]: targetID,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
args.defaultQueryMany = {};
|
|
24
|
+
} catch (e) {
|
|
25
|
+
logic.log.error(e);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class ActionsSetsLibrary {
|
|
2
|
+
#lib = new Map();
|
|
3
|
+
|
|
4
|
+
add(name, set) {
|
|
5
|
+
if (!this.#lib.has(name)) {
|
|
6
|
+
this.#lib.set(name, set);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get(name) {
|
|
11
|
+
if (this.#lib.has(name)) {
|
|
12
|
+
return this.#lib.get(name);
|
|
13
|
+
}
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
has(name){
|
|
18
|
+
return this.#lib.has(name);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = ActionsSetsLibrary;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const actionsSetsLibrary = require("./actions/index.js");
|
|
2
|
+
const LogicProxied = require("./logic.js");
|
|
3
|
+
|
|
4
|
+
module.exports = ({
|
|
5
|
+
target,
|
|
6
|
+
LogicConstructor = LogicProxied,
|
|
7
|
+
MODULE_NAME,
|
|
8
|
+
MODEL_NAME,
|
|
9
|
+
USER_MODEL_NAME = "not-user//User",
|
|
10
|
+
actionRunner = undefined,
|
|
11
|
+
actionsSets = ["standart"],
|
|
12
|
+
actions = {},
|
|
13
|
+
beforeActions = {},
|
|
14
|
+
beforeActionsAll = [require('./actions.before/standart.queries.js')],
|
|
15
|
+
afterActions = {},
|
|
16
|
+
afterActionsAll = [],
|
|
17
|
+
}) => {
|
|
18
|
+
//start with empty set
|
|
19
|
+
const ACTIONS = {};
|
|
20
|
+
//add all from actions sets library
|
|
21
|
+
actionsSets.forEach((setName) => {
|
|
22
|
+
actionsSetsLibrary.has(setName) &&
|
|
23
|
+
Object.assign(ACTIONS, actionsSetsLibrary.get(setName));
|
|
24
|
+
});
|
|
25
|
+
//add user defined
|
|
26
|
+
Object.assign(ACTIONS, actions);
|
|
27
|
+
|
|
28
|
+
const Logic = new LogicConstructor(ACTIONS, actionRunner, {
|
|
29
|
+
MODULE_NAME,
|
|
30
|
+
MODEL_NAME,
|
|
31
|
+
target,
|
|
32
|
+
USER_MODEL_NAME,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
beforeActionsAll.forEach((action) => {
|
|
36
|
+
Logic.onBefore(undefined, action);
|
|
37
|
+
});
|
|
38
|
+
afterActionsAll.forEach((action) => Logic.onAfter(undefined, action));
|
|
39
|
+
|
|
40
|
+
Object.keys(beforeActions).forEach((actionName) =>
|
|
41
|
+
beforeActions[actionName].forEach((action) =>
|
|
42
|
+
Logic.onBefore(actionName, action)
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
Object.keys(afterActions).forEach((actionName) =>
|
|
47
|
+
afterActions[actionName].forEach((action) =>
|
|
48
|
+
Logic.onAfter(actionName, action)
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return Logic;
|
|
53
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
const getApp = require("../getApp.js"),
|
|
2
|
+
configInit = require("not-config"),
|
|
3
|
+
{ sayForModule, modulePhrase } = require("not-locale"),
|
|
4
|
+
LogInit = require("not-log"),
|
|
5
|
+
{ objHas, isFunc, executeFunctionAsAsync } = require("../common.js");
|
|
6
|
+
|
|
7
|
+
const NamedActionPipes = require("./named.actions.pipes.js");
|
|
8
|
+
const ActionRunner = require("./action.runner.js");
|
|
9
|
+
const {
|
|
10
|
+
LogicExceptionActionExecutionError,
|
|
11
|
+
} = require("../exceptions/logic.js");
|
|
12
|
+
|
|
13
|
+
class LogicProxied {
|
|
14
|
+
afterPipes;
|
|
15
|
+
beforePipes;
|
|
16
|
+
|
|
17
|
+
actions = new Map();
|
|
18
|
+
actionRunner = ActionRunner;
|
|
19
|
+
|
|
20
|
+
populateBuilders = {};
|
|
21
|
+
defaultPopulate = [];
|
|
22
|
+
|
|
23
|
+
MODEL_NAME;
|
|
24
|
+
MODULE_NAME;
|
|
25
|
+
USER_MODEL_NAME = "";
|
|
26
|
+
|
|
27
|
+
#proxy;
|
|
28
|
+
log;
|
|
29
|
+
say;
|
|
30
|
+
phrase;
|
|
31
|
+
config;
|
|
32
|
+
logAction;
|
|
33
|
+
logDebugAction;
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
constructor(
|
|
37
|
+
actions = {},
|
|
38
|
+
actionRunner = ActionRunner,
|
|
39
|
+
{
|
|
40
|
+
MODULE_NAME,
|
|
41
|
+
MODEL_NAME,
|
|
42
|
+
target,
|
|
43
|
+
defaultPopulate,
|
|
44
|
+
populateBuilders,
|
|
45
|
+
USER_MODEL_NAME = "not-user//User",
|
|
46
|
+
}
|
|
47
|
+
) {
|
|
48
|
+
this.MODEL_NAME = MODEL_NAME;
|
|
49
|
+
this.MODULE_NAME = MODULE_NAME;
|
|
50
|
+
this.USER_MODEL_NAME = USER_MODEL_NAME;
|
|
51
|
+
|
|
52
|
+
defaultPopulate && (this.defaultPopulate = defaultPopulate);
|
|
53
|
+
populateBuilders && (this.populateBuilders = populateBuilders);
|
|
54
|
+
|
|
55
|
+
actionRunner && (this.actionRunner = actionRunner);
|
|
56
|
+
this.afterPipes = new NamedActionPipes(
|
|
57
|
+
NamedActionPipes.PIPE_TYPES.CONSECUTIVE,
|
|
58
|
+
this.actionRunner
|
|
59
|
+
);
|
|
60
|
+
this.beforePipes = new NamedActionPipes(
|
|
61
|
+
NamedActionPipes.PIPE_TYPES.CONSECUTIVE,
|
|
62
|
+
this.actionRunner
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
Object.keys(actions).forEach((actionName) => {
|
|
66
|
+
this.actions.set(actionName, actions[actionName]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.#initTools(target);
|
|
70
|
+
|
|
71
|
+
// proxy logic, do something before each call of all methods inside class
|
|
72
|
+
// like if arg passed is 3, print something additionally
|
|
73
|
+
return this.#proxy = new Proxy(this, {
|
|
74
|
+
get(target, prop) {
|
|
75
|
+
if (target.actions.has(prop)) {
|
|
76
|
+
return target.#getActionRunner(prop);
|
|
77
|
+
} else {
|
|
78
|
+
return target[prop];
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#initTools(target) {
|
|
85
|
+
this.log = LogInit(
|
|
86
|
+
target,
|
|
87
|
+
`${this.MODULE_NAME}//Logic//${this.MODEL_NAME}`
|
|
88
|
+
);
|
|
89
|
+
this.say = sayForModule(this.MODULE_NAME);
|
|
90
|
+
this.phrase = modulePhrase(this.MODULE_NAME);
|
|
91
|
+
this.config = configInit.readerForModule(this.MODULE_NAME);
|
|
92
|
+
|
|
93
|
+
this.logAction = (actionName, identity, params = {}) => {
|
|
94
|
+
this.log &&
|
|
95
|
+
this.log.log({
|
|
96
|
+
time: new Date(),
|
|
97
|
+
module: this.MODULE_NAME,
|
|
98
|
+
logic: this.MODEL_NAME,
|
|
99
|
+
action: actionName,
|
|
100
|
+
...identity,
|
|
101
|
+
params,
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
this.logDebugAction = (action, identity) => {
|
|
105
|
+
this.log &&
|
|
106
|
+
this.log.debug(
|
|
107
|
+
new Date(),
|
|
108
|
+
`${this.MODULE_NAME}//Logic//${
|
|
109
|
+
this.MODEL_NAME
|
|
110
|
+
}//${action}`,
|
|
111
|
+
identity?.ip,
|
|
112
|
+
identity?.root
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getModel() {
|
|
118
|
+
return getApp().getModel(`${this.MODULE_NAME}//${this.MODEL_NAME}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
getModelSchema() {
|
|
122
|
+
return getApp().getModelSchema(
|
|
123
|
+
`${this.MODULE_NAME}//${this.MODEL_NAME}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getModelUser() {
|
|
128
|
+
return this.USER_MODEL_NAME
|
|
129
|
+
? getApp().getModel(this.USER_MODEL_NAME)
|
|
130
|
+
: undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async getPopulate(actionName, prepared) {
|
|
134
|
+
if (
|
|
135
|
+
this.populateBuilders &&
|
|
136
|
+
objHas(this.populateBuilders, actionName) &&
|
|
137
|
+
isFunc(this.populateBuilders[actionName])
|
|
138
|
+
) {
|
|
139
|
+
return await executeFunctionAsAsync(
|
|
140
|
+
this.populateBuilders[actionName],
|
|
141
|
+
[prepared]
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return this.defaultPopulate;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#getActionRunner(actionName) {
|
|
148
|
+
return async (...args) => {
|
|
149
|
+
try {
|
|
150
|
+
const action = this.actions.get(actionName);
|
|
151
|
+
await this.beforeAction(actionName, [
|
|
152
|
+
this.#proxy,
|
|
153
|
+
actionName,
|
|
154
|
+
...args,
|
|
155
|
+
]);
|
|
156
|
+
const actionResult = await this.actionRunner.run(action, [
|
|
157
|
+
this.#proxy,
|
|
158
|
+
actionName,
|
|
159
|
+
...args,
|
|
160
|
+
]);
|
|
161
|
+
await this.afterAction(actionName, [
|
|
162
|
+
this.#proxy,
|
|
163
|
+
actionName,
|
|
164
|
+
actionResult,
|
|
165
|
+
...args,
|
|
166
|
+
]);
|
|
167
|
+
return actionResult;
|
|
168
|
+
} catch (e) {
|
|
169
|
+
throw new LogicExceptionActionExecutionError(
|
|
170
|
+
this.MODULE_NAME,
|
|
171
|
+
this.MODEL_NAME,
|
|
172
|
+
actionName,
|
|
173
|
+
e
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async beforeAction(actionName, args) {
|
|
180
|
+
await this.beforePipes.execute(actionName, args);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async afterAction(actionName, args) {
|
|
184
|
+
await this.afterPipes.execute(actionName, args);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
onAfter(name, action) {
|
|
188
|
+
this.afterPipes.add(name, action);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
offAfter(name, action) {
|
|
192
|
+
this.afterPipes.remove(name, action);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
onBefore(name, action) {
|
|
196
|
+
this.beforePipes.add(name, action);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
offBefore(name, action) {
|
|
200
|
+
this.beforePipes.remove(name, action);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = LogicProxied;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
const { objHas } = require("../common.js");
|
|
2
|
+
const {
|
|
3
|
+
ActionExceptionPipeExecutionError,
|
|
4
|
+
} = require("../exceptions/action.js");
|
|
5
|
+
|
|
6
|
+
const ActionRunner = require("./action.runner.js");
|
|
7
|
+
|
|
8
|
+
const CONSECUTIVE = 1;
|
|
9
|
+
const PARALLEL = 2;
|
|
10
|
+
|
|
11
|
+
const PIPE_TYPES = { CONSECUTIVE, PARALLEL };
|
|
12
|
+
Object.freeze(PIPE_TYPES);
|
|
13
|
+
|
|
14
|
+
class NamedActionPipes {
|
|
15
|
+
static PIPE_TYPES = PIPE_TYPES;
|
|
16
|
+
#type = CONSECUTIVE;
|
|
17
|
+
#all = [];
|
|
18
|
+
#named = {};
|
|
19
|
+
actionRunner = ActionRunner;
|
|
20
|
+
|
|
21
|
+
constructor(type = CONSECUTIVE, actionRunner = ActionRunner) {
|
|
22
|
+
this.#type = type;
|
|
23
|
+
actionRunner && (this.actionRunner = actionRunner);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get type(){
|
|
27
|
+
return this.#type;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
add(name, action) {
|
|
31
|
+
if (typeof name === "undefined") {
|
|
32
|
+
this.addToPipe(this.#all, action);
|
|
33
|
+
} else {
|
|
34
|
+
this.initNamedPipe(name);
|
|
35
|
+
this.addToPipe(this.#named[name], action);
|
|
36
|
+
}
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
addToPipe(pipe, action) {
|
|
41
|
+
if (!pipe.includes(action)) {
|
|
42
|
+
pipe.push(action);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
removeFromPipe(pipe, action) {
|
|
47
|
+
if (pipe.includes(action)) {
|
|
48
|
+
pipe.splice(pipe.indexOf(action), 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
initNamedPipe(name) {
|
|
53
|
+
if (!objHas(this.#named, name) || !Array.isArray(this.#named[name])) {
|
|
54
|
+
this.#named[name] = [];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
remove(name, action) {
|
|
59
|
+
if (typeof name === "undefined") {
|
|
60
|
+
this.removeFromPipe(this.#all, action);
|
|
61
|
+
} else {
|
|
62
|
+
this.initNamedPipe(name);
|
|
63
|
+
this.removeFromPipe(this.#named[name], action);
|
|
64
|
+
}
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
buildPipe(name) {
|
|
69
|
+
const final = [...this.#all];
|
|
70
|
+
this.initNamedPipe(name);
|
|
71
|
+
final.push(...this.#named[name]);
|
|
72
|
+
return final;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async execute(name, params) {
|
|
76
|
+
try {
|
|
77
|
+
const pipe = this.buildPipe(name);
|
|
78
|
+
return await this.#execute(pipe, params);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
throw new ActionExceptionPipeExecutionError({ cause: e });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async #execute(pipe, params) {
|
|
85
|
+
const ways = {
|
|
86
|
+
[CONSECUTIVE]: this.executeConsecutive.bind(this),
|
|
87
|
+
[PARALLEL]: this.executeParallel.bind(this),
|
|
88
|
+
};
|
|
89
|
+
return await ways[this.type](pipe, params);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async executeConsecutive(pipe, params) {
|
|
93
|
+
for (let action of pipe) {
|
|
94
|
+
await this.actionRunner.run(action, params);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async executeParallel(pipe, params) {
|
|
99
|
+
const promisesOfPipe = pipe.map((action) =>
|
|
100
|
+
this.actionRunner.run(action, params)
|
|
101
|
+
);
|
|
102
|
+
await Promise.all(promisesOfPipe);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = NamedActionPipes;
|
package/src/model/default.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const { deleteResponseSuccess, updateResponseSuccess } = require("./utils.js");
|
|
2
|
+
const { DBExceptionDeleteWasNotSuccessful } = require("../exceptions/db.js");
|
|
1
3
|
/** @module Model/Default */
|
|
2
4
|
const routine = require("./routine");
|
|
3
5
|
const notQuery = require("not-filter");
|
|
@@ -158,7 +160,7 @@ function getOneRaw(id, condition = {}) {
|
|
|
158
160
|
* @static
|
|
159
161
|
* @param {string} method name of the method
|
|
160
162
|
* @param {object|array} filter filtering rules object
|
|
161
|
-
* @return {Query} mongoose query object
|
|
163
|
+
* @return {import('mongoose').Query} mongoose query object
|
|
162
164
|
**/
|
|
163
165
|
function makeQuery(method, filter) {
|
|
164
166
|
let versioningMod = {
|
|
@@ -349,6 +351,38 @@ function update(filter, data, many = false) {
|
|
|
349
351
|
}
|
|
350
352
|
}
|
|
351
353
|
|
|
354
|
+
async function removeOne(filter) {
|
|
355
|
+
if (this.schema.statics.__versioning) {
|
|
356
|
+
const res = await this.updateOne(filter, { __closed: true });
|
|
357
|
+
if (!updateResponseSuccess(res, 1)) {
|
|
358
|
+
throw new DBExceptionDeleteWasNotSuccessful(res);
|
|
359
|
+
}
|
|
360
|
+
return res;
|
|
361
|
+
} else {
|
|
362
|
+
const res = await this.deleteOne(filter);
|
|
363
|
+
if (!deleteResponseSuccess(res, 1)) {
|
|
364
|
+
throw new DBExceptionDeleteWasNotSuccessful(res);
|
|
365
|
+
}
|
|
366
|
+
return res;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async function removeMany(filter) {
|
|
371
|
+
if (this.schema.statics.__versioning) {
|
|
372
|
+
const res = await this.updateMany(filter, { __closed: true });
|
|
373
|
+
if (!updateResponseSuccess(res, 0)) {
|
|
374
|
+
throw new DBExceptionDeleteWasNotSuccessful(res);
|
|
375
|
+
}
|
|
376
|
+
return res;
|
|
377
|
+
} else {
|
|
378
|
+
const res = await this.deleteMany(filter);
|
|
379
|
+
if (!deleteResponseSuccess(res, 0)) {
|
|
380
|
+
throw new DBExceptionDeleteWasNotSuccessful(res);
|
|
381
|
+
}
|
|
382
|
+
return res;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
352
386
|
module.exports.thisStatics = {
|
|
353
387
|
sanitizeInput,
|
|
354
388
|
getOne,
|
|
@@ -364,6 +398,8 @@ module.exports.thisStatics = {
|
|
|
364
398
|
listAndCount,
|
|
365
399
|
add,
|
|
366
400
|
update,
|
|
401
|
+
removeOne,
|
|
402
|
+
removeMany,
|
|
367
403
|
};
|
|
368
404
|
|
|
369
405
|
/**
|
|
@@ -383,14 +419,18 @@ function getID() {
|
|
|
383
419
|
* @return {number} ID
|
|
384
420
|
*/
|
|
385
421
|
function close(data = undefined) {
|
|
386
|
-
if (
|
|
387
|
-
Object.keys(data)
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
422
|
+
if (this?.schema?.statics?.__versioning) {
|
|
423
|
+
if (data && Object.keys(data)) {
|
|
424
|
+
Object.keys(data).forEach((fieldName) => {
|
|
425
|
+
notPath.setValueByPath(this, fieldName, data[fieldName]);
|
|
426
|
+
this.markModified(fieldName);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
this.__closed = true;
|
|
430
|
+
return this.save();
|
|
431
|
+
} else {
|
|
432
|
+
return this.deleteOne({ _id: this._id });
|
|
391
433
|
}
|
|
392
|
-
this.__closed = true;
|
|
393
|
-
return this.save();
|
|
394
434
|
}
|
|
395
435
|
|
|
396
436
|
function saveNewVersion() {
|
package/src/model/utils.js
CHANGED
|
@@ -7,12 +7,16 @@ function getBaseResult() {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
function createInsertManySuccessSignature(count) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
if (count > 0) {
|
|
11
|
+
return Object.assign(
|
|
12
|
+
{
|
|
13
|
+
insertedCount: count,
|
|
14
|
+
},
|
|
15
|
+
getBaseResult()
|
|
16
|
+
);
|
|
17
|
+
} else {
|
|
18
|
+
return Object.assign({}, getBaseResult());
|
|
19
|
+
}
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
function createInsertOneSuccessSignature() {
|
|
@@ -31,12 +35,16 @@ function createUpdateManySuccessSignature(count) {
|
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
function createDeleteManySuccessSignature(count) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
if (count > 0) {
|
|
39
|
+
return Object.assign(
|
|
40
|
+
{
|
|
41
|
+
deletedCount: count,
|
|
42
|
+
},
|
|
43
|
+
getBaseResult()
|
|
44
|
+
);
|
|
45
|
+
} else {
|
|
46
|
+
return Object.assign({}, getBaseResult());
|
|
47
|
+
}
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
function insertResponseSuccess(res, count = 1) {
|
|
@@ -68,6 +76,7 @@ function deleteManyResponseSuccess(res, count) {
|
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
module.exports.deleteManyResponseSuccess = deleteManyResponseSuccess;
|
|
79
|
+
|
|
71
80
|
/**
|
|
72
81
|
* checking result of modification queries to ensure that changes were made
|
|
73
82
|
*/
|
package/src/types.js
CHANGED
|
@@ -169,4 +169,52 @@
|
|
|
169
169
|
* @property {notFieldModel} [model]
|
|
170
170
|
*/
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* @typedef {string} uuid
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @typedef {object} ListAndCountResult
|
|
178
|
+
* @property {Array<notAppDocument>} list list of extended mongoose documents
|
|
179
|
+
* @property {number} skip count of skiped from start of set
|
|
180
|
+
* @property {number} count total count of documents in this set
|
|
181
|
+
* @property {number} page number of returned page
|
|
182
|
+
* @property {number} pages total count of pages in this set
|
|
183
|
+
*/
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @typedef {object} notAppModelMethods
|
|
187
|
+
* @property {function(object):object} sanitizeInput
|
|
188
|
+
* @property {function(uuid, [Array<string>], [object]):Promise<notAppDocument>} getOne
|
|
189
|
+
* @property {function(number, [Array<string>], [object]):Promise<notAppDocument>} getOneByID
|
|
190
|
+
* @property {function(uuid, [object]):Promise<notAppDocument>} getOneRaw
|
|
191
|
+
* @property {function(string, object):Promise<notAppDocument|Array<notAppDocument>|undefined>} makeQuery
|
|
192
|
+
* @property {function(number, number, object, object|Array):Promise<Array<notAppDocument>>} list
|
|
193
|
+
* @property {function(string, Array<String>, object, Array<String>):Promise<Array<notAppDocument>>} listByField
|
|
194
|
+
* @property {function([object]):Promise<Array<notAppDocument>>} listAll
|
|
195
|
+
* @property {function(object|array):Promise<Array<notAppDocument>>} listAllAndPopulate
|
|
196
|
+
* @property {function(object):Promise<number>} countWithFilter
|
|
197
|
+
* @property {function(number, number, object, object | Array, Array<String>):Promise<Array<notAppDocument>>} listAndPopulate
|
|
198
|
+
* @property {function(number, number, object, object|array, object|array, object):Promise<ListAndCountResult>} listAndCount
|
|
199
|
+
* @property {function(object):Promise<notAppDocument>} add
|
|
200
|
+
* @property {function(object, object, boolean):Promise<notAppDocument|undefined>} update
|
|
201
|
+
* @property {function(object):Promise<undefined>} removeOne
|
|
202
|
+
* @property {function(object):Promise<undefined>} removeMany
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @typedef {import('mongoose').Model & notAppModelMethods} notAppModel
|
|
207
|
+
**/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @typedef {object} notAppDocumentMethods
|
|
211
|
+
* @property {function():number} getID
|
|
212
|
+
* @property {function([object]):Promise<notAppDocument|undefined>} close
|
|
213
|
+
* @property {function():Promise<notAppDocument>} saveNewVersion
|
|
214
|
+
**/
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @typedef {import('mongoose').Document & notAppDocumentMethods} notAppDocument
|
|
218
|
+
**/
|
|
219
|
+
|
|
172
220
|
module.exports = {};
|
package/test/model/default.js
CHANGED
package/test/model/proto.js
CHANGED
package/test/notModel.js
CHANGED