not-node 5.0.22 → 5.1.2
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 -1
- package/bin/not-deploy.js +52 -0
- package/index.js +21 -19
- package/package.json +1 -1
- package/src/app.js +61 -58
- package/src/auth/abstract.js +17 -19
- package/src/auth/const.js +8 -12
- package/src/auth/fields.js +143 -124
- package/src/auth/index.js +14 -14
- package/src/auth/roles.js +64 -63
- package/src/auth/routes.js +89 -54
- package/src/auth/rules.js +63 -63
- package/src/auth/session.js +60 -62
- package/src/bootstrap/form.js +13 -13
- package/src/bootstrap/logic.js +45 -41
- package/src/bootstrap/model.js +14 -17
- package/src/bootstrap/route.js +132 -53
- package/src/common.js +86 -79
- package/src/core/fields/ID.js +6 -6
- package/src/core/fields/__closed.js +3 -3
- package/src/core/fields/__latest.js +3 -3
- package/src/core/fields/__version.js +3 -3
- package/src/core/fields/__versions.js +3 -3
- package/src/core/fields/_id.js +6 -6
- package/src/core/fields/active.js +9 -9
- package/src/core/fields/codeName.js +9 -9
- package/src/core/fields/createdAt.js +17 -17
- package/src/core/fields/default.js +9 -9
- package/src/core/fields/description.js +11 -11
- package/src/core/fields/email.js +9 -9
- package/src/core/fields/enabled.js +9 -9
- package/src/core/fields/expiredAt.js +16 -16
- package/src/core/fields/height.js +11 -11
- package/src/core/fields/ip.js +10 -10
- package/src/core/fields/objectId.js +10 -10
- package/src/core/fields/owner.js +13 -13
- package/src/core/fields/ownerModel.js +11 -11
- package/src/core/fields/price.js +11 -11
- package/src/core/fields/requiredObject.js +10 -10
- package/src/core/fields/session.js +10 -10
- package/src/core/fields/size.js +11 -11
- package/src/core/fields/telephone.js +9 -9
- package/src/core/fields/title.js +15 -15
- package/src/core/fields/updatedAt.js +17 -17
- package/src/core/fields/userId.js +11 -12
- package/src/core/fields/uuid.js +11 -11
- package/src/core/fields/validators/email.js +6 -4
- package/src/core/fields/validators/owner.js +6 -4
- package/src/core/fields/width.js +11 -11
- package/src/domain.js +435 -431
- package/src/env.js +23 -23
- package/src/error.js +20 -23
- package/src/exceptions/db.js +23 -0
- package/src/exceptions/http.js +43 -0
- package/src/fields/index.js +106 -111
- package/src/form/fabric.js +19 -24
- package/src/form/form.js +195 -186
- package/src/form/index.js +2 -2
- package/src/generic/index.js +2 -0
- package/src/generic/logic.js +595 -0
- package/src/generic/route.js +76 -0
- package/src/init/additional.js +7 -7
- package/src/init/app.js +75 -68
- package/src/init/bodyparser.js +14 -14
- package/src/init/compression.js +6 -7
- package/src/init/core.js +12 -12
- package/src/init/cors.js +22 -24
- package/src/init/db/index.js +41 -43
- package/src/init/db/ioredis.js +28 -20
- package/src/init/db/mongoose.js +42 -32
- package/src/init/db/redis.js +59 -48
- package/src/init/env.js +44 -36
- package/src/init/express.js +18 -21
- package/src/init/fileupload.js +8 -10
- package/src/init/http.js +65 -47
- package/src/init/index.js +141 -148
- package/src/init/informer.js +7 -9
- package/src/init/methodoverride.js +4 -6
- package/src/init/middleware.js +23 -26
- package/src/init/modules.js +4 -4
- package/src/init/monitoring.js +8 -8
- package/src/init/rateLimiter.js +53 -45
- package/src/init/routes.js +67 -69
- package/src/init/security.js +37 -33
- package/src/init/sequence.js +111 -107
- package/src/init/sequence.standart.js +54 -56
- package/src/init/sessions/index.js +21 -21
- package/src/init/sessions/mongoose.js +49 -36
- package/src/init/sessions/redis.js +28 -27
- package/src/init/static.js +53 -40
- package/src/init/template.js +17 -17
- package/src/lib.js +230 -200
- package/src/manifest/batchRunner.js +26 -25
- package/src/manifest/initializator/forms.js +24 -24
- package/src/manifest/initializator/index.js +8 -10
- package/src/manifest/initializator/manifests.js +45 -42
- package/src/manifest/initializator/models.js +37 -34
- package/src/manifest/manifest.filter.js +130 -97
- package/src/manifest/manifest.js +103 -77
- package/src/manifest/module.js +350 -360
- package/src/manifest/registrator/fields.js +90 -85
- package/src/manifest/registrator/forms.js +47 -47
- package/src/manifest/registrator/index.js +14 -16
- package/src/manifest/registrator/locales.js +17 -16
- package/src/manifest/registrator/logics.js +66 -64
- package/src/manifest/registrator/models.js +64 -62
- package/src/manifest/registrator/routes.js +171 -132
- package/src/manifest/registrator/routes.ws.js +109 -103
- package/src/manifest/route.js +216 -161
- package/src/model/buildValidator.js +53 -42
- package/src/model/default.js +304 -286
- package/src/model/enrich.js +69 -61
- package/src/model/increment.js +124 -137
- package/src/model/proto.js +179 -148
- package/src/model/routine.js +85 -76
- package/src/model/utils.js +33 -0
- package/src/model/versioning.js +148 -130
- package/src/obsolete.js +12 -8
- package/src/parser.js +29 -22
- package/src/repos.js +29 -31
- package/src/rollup.js +75 -65
- package/src/shell.helpers.js +28 -28
- package/static.js +31 -0
- package/src/generic/router.js +0 -16
- package/src/manifest/module.models.js +0 -0
- package/src/manifest/module.status.js +0 -0
package/src/model/default.js
CHANGED
|
@@ -1,371 +1,389 @@
|
|
|
1
1
|
/** @module Model/Default */
|
|
2
|
-
const routine = require(
|
|
3
|
-
const notQuery = require(
|
|
4
|
-
const {objHas} = require(
|
|
2
|
+
const routine = require("./routine");
|
|
3
|
+
const notQuery = require("not-filter");
|
|
4
|
+
const { objHas } = require("../common");
|
|
5
5
|
|
|
6
|
-
const defaultFilter = (obj)=>{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const defaultFilter = (obj) => {
|
|
7
|
+
if (obj.schema.statics.__versioning) {
|
|
8
|
+
return {
|
|
9
|
+
__latest: true,
|
|
10
|
+
__closed: false,
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return {};
|
|
14
|
+
}
|
|
11
15
|
};
|
|
12
16
|
|
|
13
|
-
const defaultSorter = ()=>{
|
|
14
|
-
|
|
15
|
-
['_id', 'descending']
|
|
16
|
-
];
|
|
17
|
+
const defaultSorter = () => {
|
|
18
|
+
return [["_id", "descending"]];
|
|
17
19
|
};
|
|
18
20
|
|
|
19
21
|
module.exports.extractVariants = function (items) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (Array.isArray(items)) {
|
|
23
|
+
return items.map((item) => item.getVariant());
|
|
24
|
+
} else {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
function populateQuery_markVersioning(inst){
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
function populateQuery_markVersioning(inst) {
|
|
30
|
+
if (objHas(inst, "match")) {
|
|
31
|
+
inst.match.__latest = true;
|
|
32
|
+
} else {
|
|
33
|
+
inst.match = { __latest: true };
|
|
34
|
+
}
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
module.exports.populateQuery_markVersioning = populateQuery_markVersioning;
|
|
36
38
|
|
|
37
39
|
const populateQuery = (query, populate, versioning = false) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
if (Array.isArray(populate)) {
|
|
41
|
+
for (let key of populate) {
|
|
42
|
+
let inst = {};
|
|
43
|
+
if (typeof key === "string") {
|
|
44
|
+
inst.path = key;
|
|
45
|
+
} else if (objHas(key, "path")) {
|
|
46
|
+
Object.assign(inst, key);
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`No path to populate: \n` + JSON.stringify(key, null, 4)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (versioning) {
|
|
53
|
+
populateQuery_markVersioning(inst);
|
|
54
|
+
}
|
|
55
|
+
query.populate(inst);
|
|
56
|
+
}
|
|
52
57
|
}
|
|
53
|
-
}
|
|
54
58
|
};
|
|
55
59
|
|
|
56
60
|
module.exports.populateQuery = populateQuery;
|
|
57
61
|
|
|
58
62
|
/**
|
|
59
|
-
* Sanitize input
|
|
60
|
-
* @static
|
|
61
|
-
* @param {object} input data
|
|
62
|
-
* @return {object} data;
|
|
63
|
-
**/
|
|
63
|
+
* Sanitize input
|
|
64
|
+
* @static
|
|
65
|
+
* @param {object} input data
|
|
66
|
+
* @return {object} data;
|
|
67
|
+
**/
|
|
64
68
|
function sanitizeInput(input) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
if (!objHas(input, "default")) {
|
|
70
|
+
input.default = false;
|
|
71
|
+
}
|
|
72
|
+
return input;
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
/**
|
|
72
|
-
* Retrieves one record by primary key
|
|
73
|
-
* If versioning ON, it retrieves __latest and not __closed
|
|
74
|
-
* @static
|
|
75
|
-
* @param {string} id primary key
|
|
76
|
-
* @param {Array} population optional if needed population of some fields
|
|
77
|
-
* @param {Object} condition optional if needed additional condition
|
|
78
|
-
* @return {Promise} Promise
|
|
79
|
-
**/
|
|
76
|
+
* Retrieves one record by primary key
|
|
77
|
+
* If versioning ON, it retrieves __latest and not __closed
|
|
78
|
+
* @static
|
|
79
|
+
* @param {string} id primary key
|
|
80
|
+
* @param {Array} population optional if needed population of some fields
|
|
81
|
+
* @param {Object} condition optional if needed additional condition
|
|
82
|
+
* @return {Promise} Promise
|
|
83
|
+
**/
|
|
80
84
|
function getOne(id, population = [], condition = {}) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
let query;
|
|
86
|
+
if (this.schema.statics.__versioning) {
|
|
87
|
+
query = this.findOne({
|
|
88
|
+
_id: id,
|
|
89
|
+
...condition,
|
|
90
|
+
__latest: true,
|
|
91
|
+
__closed: false,
|
|
92
|
+
});
|
|
93
|
+
if (Array.isArray(population)) {
|
|
94
|
+
population.push("__versions");
|
|
95
|
+
} else {
|
|
96
|
+
population = ["__versions"];
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
query = this.findOne({
|
|
100
|
+
_id: id,
|
|
101
|
+
...condition,
|
|
102
|
+
});
|
|
93
103
|
}
|
|
94
|
-
|
|
95
|
-
query
|
|
96
|
-
_id: id,
|
|
97
|
-
...condition
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
populateQuery(query, population, this.schema.statics.__versioning);
|
|
101
|
-
return query.exec();
|
|
104
|
+
populateQuery(query, population, this.schema.statics.__versioning);
|
|
105
|
+
return query.exec();
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
/**
|
|
105
|
-
* Retrieves one record by unique numeric ID
|
|
106
|
-
* If versioning ON, it retrieves __latest and not __closed
|
|
107
|
-
* @static
|
|
108
|
-
* @param {number} ID some unique numeric identificator
|
|
109
|
-
* @param {Object} condition optional if needed additional condition
|
|
110
|
-
* @return {Promise} Promise of document, if increment is OFF - then Promise.resolve(null)
|
|
111
|
-
**/
|
|
109
|
+
* Retrieves one record by unique numeric ID
|
|
110
|
+
* If versioning ON, it retrieves __latest and not __closed
|
|
111
|
+
* @static
|
|
112
|
+
* @param {number} ID some unique numeric identificator
|
|
113
|
+
* @param {Object} condition optional if needed additional condition
|
|
114
|
+
* @return {Promise} Promise of document, if increment is OFF - then Promise.resolve(null)
|
|
115
|
+
**/
|
|
112
116
|
function getOneByID(ID, condition = {}) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
117
|
+
if (this.schema.statics.__incField) {
|
|
118
|
+
let by, query;
|
|
119
|
+
if (this.schema.statics.__versioning) {
|
|
120
|
+
by = {
|
|
121
|
+
...condition,
|
|
122
|
+
__latest: true,
|
|
123
|
+
__closed: false,
|
|
124
|
+
};
|
|
125
|
+
} else {
|
|
126
|
+
by = {
|
|
127
|
+
...condition,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
by[this.schema.statics.__incField] = ID;
|
|
131
|
+
query = this.findOne(by);
|
|
132
|
+
return query.exec();
|
|
133
|
+
} else {
|
|
134
|
+
return Promise.resolve(null);
|
|
135
|
+
}
|
|
128
136
|
}
|
|
129
137
|
|
|
130
|
-
|
|
131
138
|
/**
|
|
132
|
-
* Retrieves one record by primary key, without any restriction
|
|
133
|
-
* @static
|
|
134
|
-
* @param {string} id primary key
|
|
135
|
-
* @param {Object} condition optional if needed additional condition
|
|
136
|
-
* @return {Promise} Promise
|
|
137
|
-
**/
|
|
139
|
+
* Retrieves one record by primary key, without any restriction
|
|
140
|
+
* @static
|
|
141
|
+
* @param {string} id primary key
|
|
142
|
+
* @param {Object} condition optional if needed additional condition
|
|
143
|
+
* @return {Promise} Promise
|
|
144
|
+
**/
|
|
138
145
|
function getOneRaw(id, condition = {}) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
let query = this.findOne({
|
|
147
|
+
_id: id,
|
|
148
|
+
...condition,
|
|
149
|
+
});
|
|
150
|
+
return query.exec();
|
|
144
151
|
}
|
|
145
152
|
|
|
146
153
|
/**
|
|
147
|
-
* Common query with filtering constructor for generic method
|
|
148
|
-
* @static
|
|
149
|
-
* @param {string} method name of the method
|
|
150
|
-
* @param {object|array} filter filtering rules object
|
|
151
|
-
* @return {Query} mongoose query object
|
|
152
|
-
**/
|
|
153
|
-
function makeQuery(method, filter){
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
154
|
+
* Common query with filtering constructor for generic method
|
|
155
|
+
* @static
|
|
156
|
+
* @param {string} method name of the method
|
|
157
|
+
* @param {object|array} filter filtering rules object
|
|
158
|
+
* @return {Query} mongoose query object
|
|
159
|
+
**/
|
|
160
|
+
function makeQuery(method, filter) {
|
|
161
|
+
let versioningMod = {
|
|
162
|
+
__latest: true,
|
|
163
|
+
__closed: false,
|
|
164
|
+
},
|
|
165
|
+
finalFilter = {};
|
|
166
|
+
switch (notQuery.filter.getFilterType(filter)) {
|
|
167
|
+
case notQuery.filter.OPT_OR:
|
|
168
|
+
if (this.schema.statics.__versioning) {
|
|
169
|
+
finalFilter = {
|
|
170
|
+
$and: [versioningMod, { $or: filter }],
|
|
171
|
+
};
|
|
172
|
+
} else {
|
|
173
|
+
finalFilter = {
|
|
174
|
+
$or: filter,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
case notQuery.filter.OPT_AND:
|
|
179
|
+
if (this.schema.statics.__versioning) {
|
|
180
|
+
finalFilter = {
|
|
181
|
+
$and: [versioningMod, filter],
|
|
182
|
+
};
|
|
183
|
+
} else {
|
|
184
|
+
finalFilter = {
|
|
185
|
+
$and: [filter],
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
default:
|
|
190
|
+
finalFilter = {
|
|
191
|
+
...(this.schema.statics.__versioning ? versioningMod : {}),
|
|
192
|
+
};
|
|
169
193
|
}
|
|
170
|
-
|
|
171
|
-
case notQuery.filter.OPT_AND:
|
|
172
|
-
if (this.schema.statics.__versioning){
|
|
173
|
-
finalFilter = {
|
|
174
|
-
$and: [versioningMod, filter]
|
|
175
|
-
};
|
|
176
|
-
}else{
|
|
177
|
-
finalFilter = {
|
|
178
|
-
$and:[filter]
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
break;
|
|
182
|
-
default:
|
|
183
|
-
finalFilter = {...(this.schema.statics.__versioning?versioningMod:{})};
|
|
184
|
-
}
|
|
185
|
-
return this[method](finalFilter);
|
|
194
|
+
return this[method](finalFilter);
|
|
186
195
|
}
|
|
187
196
|
|
|
188
197
|
/**
|
|
189
|
-
* List record method
|
|
190
|
-
* @static
|
|
191
|
-
* @param {number} skip number of skiped records
|
|
192
|
-
* @param {number} size length of return list
|
|
193
|
-
* @param {Object} sorter sorter rules
|
|
194
|
-
* @param {(Object|Array)} filter filter rules
|
|
195
|
-
* @return {Promise} Promise
|
|
196
|
-
**/
|
|
197
|
-
function list(skip, size, sorter, filter){
|
|
198
|
-
|
|
199
|
-
|
|
198
|
+
* List record method
|
|
199
|
+
* @static
|
|
200
|
+
* @param {number} skip number of skiped records
|
|
201
|
+
* @param {number} size length of return list
|
|
202
|
+
* @param {Object} sorter sorter rules
|
|
203
|
+
* @param {(Object|Array)} filter filter rules
|
|
204
|
+
* @return {Promise} Promise
|
|
205
|
+
**/
|
|
206
|
+
function list(skip, size, sorter, filter) {
|
|
207
|
+
let query = this.makeQuery("find", filter);
|
|
208
|
+
return query.sort(sorter).skip(skip).limit(size).exec();
|
|
200
209
|
}
|
|
201
210
|
|
|
202
|
-
|
|
203
211
|
/**
|
|
204
|
-
* Returns list of items with specific values in specific field, optionaly filtered
|
|
205
|
-
* @static
|
|
206
|
-
* @param {string} field name of the field to search in
|
|
207
|
-
* @param {array} list list of 'id' field values
|
|
208
|
-
* @param {object|array} filter filter rules
|
|
209
|
-
* @param {object} populate populate rules
|
|
210
|
-
* @return {Promise} Promise
|
|
211
|
-
*/
|
|
212
|
+
* Returns list of items with specific values in specific field, optionaly filtered
|
|
213
|
+
* @static
|
|
214
|
+
* @param {string} field name of the field to search in
|
|
215
|
+
* @param {array} list list of 'id' field values
|
|
216
|
+
* @param {object|array} filter filter rules
|
|
217
|
+
* @param {object} populate populate rules
|
|
218
|
+
* @return {Promise} Promise
|
|
219
|
+
*/
|
|
212
220
|
|
|
213
|
-
function listByField(field, list = [], filter = {}, populate = []){
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
221
|
+
function listByField(field, list = [], filter = {}, populate = []) {
|
|
222
|
+
let query = this.makeQuery("find", {
|
|
223
|
+
[field]: {
|
|
224
|
+
$in: list,
|
|
225
|
+
...filter,
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
populateQuery(query, populate, this.schema.statics.__versioning);
|
|
229
|
+
return query.exec();
|
|
222
230
|
}
|
|
223
231
|
|
|
224
232
|
/**
|
|
225
|
-
* List record and populate method
|
|
226
|
-
* @static
|
|
227
|
-
* @param {number} skip number of skiped records
|
|
228
|
-
* @param {number} size length of return list
|
|
229
|
-
* @param {object} sorter sorter rules
|
|
230
|
-
* @param {object|array} filter filter rules
|
|
231
|
-
* @param {object} populate populate rules
|
|
232
|
-
* @return {Promise} Promise
|
|
233
|
-
*/
|
|
233
|
+
* List record and populate method
|
|
234
|
+
* @static
|
|
235
|
+
* @param {number} skip number of skiped records
|
|
236
|
+
* @param {number} size length of return list
|
|
237
|
+
* @param {object} sorter sorter rules
|
|
238
|
+
* @param {object|array} filter filter rules
|
|
239
|
+
* @param {object} populate populate rules
|
|
240
|
+
* @return {Promise} Promise
|
|
241
|
+
*/
|
|
234
242
|
function listAndPopulate(skip, size, sorter, filter, populate) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
243
|
+
let query = this.makeQuery("find", filter);
|
|
244
|
+
query.sort(sorter).skip(skip).limit(size);
|
|
245
|
+
populateQuery(query, populate, this.schema.statics.__versioning);
|
|
246
|
+
return query.exec();
|
|
239
247
|
}
|
|
240
248
|
|
|
241
249
|
/**
|
|
242
|
-
* List all records from collection
|
|
243
|
-
* If Versioning is ON restricts to __latest and not __closed
|
|
244
|
-
* By default sorts by _id in DESC
|
|
245
|
-
* @
|
|
246
|
-
* @
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
250
|
+
* List all records from collection, optional filtering evailable
|
|
251
|
+
* If Versioning is ON restricts to __latest and not __closed
|
|
252
|
+
* By default sorts by _id in DESC
|
|
253
|
+
* @param {object|array} filter filter rules
|
|
254
|
+
* @static
|
|
255
|
+
* @return {Promise} Promise
|
|
256
|
+
*/
|
|
257
|
+
function listAll(filter = null) {
|
|
258
|
+
let by = defaultFilter(this);
|
|
259
|
+
if (filter) {
|
|
260
|
+
by = notQuery.filter.modifyRules(by, filter);
|
|
261
|
+
}
|
|
262
|
+
return this.find(by).sort(defaultSorter()).exec();
|
|
252
263
|
}
|
|
253
264
|
|
|
254
265
|
/**
|
|
255
|
-
* List all record in collection and populates
|
|
256
|
-
* If Versioning is ON restricts to __latest and not __closed
|
|
257
|
-
* By default sorts by _id in DESC
|
|
258
|
-
* @static
|
|
259
|
-
* @param {object|array} populate populate rules
|
|
260
|
-
* @return {Promise} Promise
|
|
261
|
-
*/
|
|
266
|
+
* List all record in collection and populates
|
|
267
|
+
* If Versioning is ON restricts to __latest and not __closed
|
|
268
|
+
* By default sorts by _id in DESC
|
|
269
|
+
* @static
|
|
270
|
+
* @param {object|array} populate populate rules
|
|
271
|
+
* @return {Promise} Promise
|
|
272
|
+
*/
|
|
262
273
|
function listAllAndPopulate(populate) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
274
|
+
let by = defaultFilter(this),
|
|
275
|
+
query = this.find(by);
|
|
276
|
+
query.sort(defaultSorter());
|
|
277
|
+
populateQuery(query, populate, this.schema.statics.__versioning);
|
|
278
|
+
return query.exec();
|
|
268
279
|
}
|
|
269
280
|
|
|
270
281
|
/**
|
|
271
|
-
* List record in collection and populates, with count of total founded records
|
|
272
|
-
* By default sorts by _id in DESC
|
|
273
|
-
* @static
|
|
274
|
-
* @param {number} skip number of skiped records
|
|
275
|
-
* @param {number} size length of return list
|
|
276
|
-
* @param {object} sorter sorter rules
|
|
277
|
-
* @param {(object|array)} filter filter rules
|
|
278
|
-
* @param {(object|array)} search search rules
|
|
279
|
-
* @param {object} populate populate rules
|
|
280
|
-
* @return {Promise} {list, count, pages}
|
|
281
|
-
*/
|
|
282
|
-
async function listAndCount(skip, size, sorter, filter, search, populate = []){
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
282
|
+
* List record in collection and populates, with count of total founded records
|
|
283
|
+
* By default sorts by _id in DESC
|
|
284
|
+
* @static
|
|
285
|
+
* @param {number} skip number of skiped records
|
|
286
|
+
* @param {number} size length of return list
|
|
287
|
+
* @param {object} sorter sorter rules
|
|
288
|
+
* @param {(object|array)} filter filter rules
|
|
289
|
+
* @param {(object|array)} search search rules
|
|
290
|
+
* @param {object} populate populate rules
|
|
291
|
+
* @return {Promise} {list, count, pages}
|
|
292
|
+
*/
|
|
293
|
+
async function listAndCount(skip, size, sorter, filter, search, populate = []) {
|
|
294
|
+
let listQuery = this.listAndPopulate(
|
|
295
|
+
skip,
|
|
296
|
+
size,
|
|
297
|
+
sorter,
|
|
298
|
+
search || filter,
|
|
299
|
+
populate
|
|
300
|
+
),
|
|
301
|
+
countQuery = this.countWithFilter(search || filter);
|
|
302
|
+
const [list, count] = await Promise.all([listQuery, countQuery]);
|
|
303
|
+
return {
|
|
304
|
+
list,
|
|
305
|
+
skip,
|
|
306
|
+
count,
|
|
307
|
+
page: Math.floor(skip / size) + (skip % size === 0 ? 1 : 0),
|
|
308
|
+
pages: Math.ceil(count / size),
|
|
309
|
+
};
|
|
293
310
|
}
|
|
294
311
|
|
|
295
312
|
/**
|
|
296
|
-
* Counts record method
|
|
297
|
-
* @static
|
|
298
|
-
* @param {object|array} filter filter rules
|
|
299
|
-
* @return {Promise} Promise
|
|
300
|
-
*/
|
|
301
|
-
function countWithFilter(filter){
|
|
302
|
-
|
|
303
|
-
|
|
313
|
+
* Counts record method
|
|
314
|
+
* @static
|
|
315
|
+
* @param {object|array} filter filter rules
|
|
316
|
+
* @return {Promise} Promise
|
|
317
|
+
*/
|
|
318
|
+
function countWithFilter(filter) {
|
|
319
|
+
let query = this.makeQuery("countDocuments", filter);
|
|
320
|
+
return query.exec();
|
|
304
321
|
}
|
|
305
322
|
|
|
306
|
-
|
|
307
323
|
/**
|
|
308
|
-
* Starts add routine
|
|
309
|
-
* @static
|
|
310
|
-
* @param {object} data data
|
|
311
|
-
* @return {Promise} Promise
|
|
312
|
-
*/
|
|
324
|
+
* Starts add routine
|
|
325
|
+
* @static
|
|
326
|
+
* @param {object} data data
|
|
327
|
+
* @return {Promise<MongooseDocument>} Promise of saved document
|
|
328
|
+
*/
|
|
313
329
|
function add(data) {
|
|
314
|
-
|
|
330
|
+
return routine.add(this, data);
|
|
315
331
|
}
|
|
316
332
|
|
|
317
333
|
/**
|
|
318
|
-
* Starts update routine
|
|
319
|
-
* @static
|
|
320
|
-
* @param {object} filter search criteria
|
|
321
|
-
* @param {object} data data
|
|
322
|
-
* @param {boolean} many if true will affect all records according to filter
|
|
323
|
-
* @return {Promise} Promise
|
|
324
|
-
*/
|
|
334
|
+
* Starts update routine
|
|
335
|
+
* @static
|
|
336
|
+
* @param {object} filter search criteria
|
|
337
|
+
* @param {object} data data
|
|
338
|
+
* @param {boolean} many if true will affect all records according to filter
|
|
339
|
+
* @return {Promise} Promise of saved document
|
|
340
|
+
*/
|
|
325
341
|
function update(filter, data, many = false) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
342
|
+
if (many) {
|
|
343
|
+
return routine.updateMany(this, filter, data);
|
|
344
|
+
} else {
|
|
345
|
+
return routine.update(this, filter, data);
|
|
346
|
+
}
|
|
331
347
|
}
|
|
332
348
|
|
|
333
349
|
module.exports.thisStatics = {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
350
|
+
sanitizeInput,
|
|
351
|
+
getOne,
|
|
352
|
+
getOneByID,
|
|
353
|
+
getOneRaw,
|
|
354
|
+
makeQuery,
|
|
355
|
+
list,
|
|
356
|
+
listByField,
|
|
357
|
+
listAll,
|
|
358
|
+
listAllAndPopulate,
|
|
359
|
+
countWithFilter,
|
|
360
|
+
listAndPopulate,
|
|
361
|
+
listAndCount,
|
|
362
|
+
add,
|
|
363
|
+
update,
|
|
348
364
|
};
|
|
349
365
|
|
|
350
366
|
/**
|
|
351
|
-
* Returns incremental ID for this doc
|
|
352
|
-
* @return {numeric} ID
|
|
353
|
-
*/
|
|
367
|
+
* Returns incremental ID for this doc
|
|
368
|
+
* @return {numeric} ID
|
|
369
|
+
*/
|
|
354
370
|
function getID() {
|
|
355
|
-
|
|
371
|
+
return this.schema.statics.__incField
|
|
372
|
+
? this[this.schema.statics.__incField]
|
|
373
|
+
: 0;
|
|
356
374
|
}
|
|
357
375
|
|
|
358
376
|
/**
|
|
359
|
-
* Closes document and saves it
|
|
360
|
-
* This is replaces remove when Versioning is ON
|
|
361
|
-
* @return {numeric} ID
|
|
362
|
-
*/
|
|
377
|
+
* Closes document and saves it
|
|
378
|
+
* This is replaces remove when Versioning is ON
|
|
379
|
+
* @return {numeric} ID
|
|
380
|
+
*/
|
|
363
381
|
function close() {
|
|
364
|
-
|
|
365
|
-
|
|
382
|
+
this.__closed = true;
|
|
383
|
+
return this.save();
|
|
366
384
|
}
|
|
367
385
|
|
|
368
386
|
module.exports.thisMethods = {
|
|
369
|
-
|
|
370
|
-
|
|
387
|
+
getID,
|
|
388
|
+
close,
|
|
371
389
|
};
|