@cyberskill/shared 2.11.0 → 2.13.0
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.
|
@@ -1,45 +1,44 @@
|
|
|
1
|
-
import { cloneDeep as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { v4 as
|
|
6
|
-
import { MONGO_SLUG_MAX_ATTEMPTS as
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
var ce = Object.defineProperty, ue = Object.defineProperties, oe = Object.getOwnPropertyDescriptors, z = Object.getOwnPropertySymbols, ie = Object.prototype.hasOwnProperty, le = Object.prototype.propertyIsEnumerable, T = (n, t, e) => t in n ? ce(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, S = (n, t) => {
|
|
1
|
+
import { cloneDeep as B, isObject as T } from "lodash-es";
|
|
2
|
+
import Q from "migrate-mongo";
|
|
3
|
+
import W from "mongoose-aggregate-paginate-v2";
|
|
4
|
+
import k from "mongoose-paginate-v2";
|
|
5
|
+
import { v4 as G } from "uuid";
|
|
6
|
+
import { MONGO_SLUG_MAX_ATTEMPTS as J } from "./mongo.constant.js";
|
|
7
|
+
import { getNestedValue as X, setNestedValue as H, normalizeMongoFilter as V } from "../../util/object/object.util.js";
|
|
8
|
+
import { regexSearchMapper as Y } from "../../util/common/common.util.js";
|
|
9
|
+
import { writeFileSync as q, pathExistsSync as Z, readFileSync as ee, appendFileSync as te } from "../fs/fs.util.js";
|
|
10
|
+
import { PATH as w, MIGRATE_MONGO_CONFIG as L } from "../path/path.constant.js";
|
|
11
|
+
import { validate as re } from "../../util/validate/validate.util.js";
|
|
12
|
+
import { generateShortId as ne, generateSlug as I } from "../../util/string/string.util.js";
|
|
13
|
+
import { RESPONSE_STATUS as D } from "../../constant/response-status.js";
|
|
14
|
+
import { catchError as f } from "../log/log.util.js";
|
|
15
|
+
var se = Object.defineProperty, ue = Object.defineProperties, le = Object.getOwnPropertyDescriptors, U = Object.getOwnPropertySymbols, ce = Object.prototype.hasOwnProperty, oe = Object.prototype.propertyIsEnumerable, $ = (s, t, e) => t in s ? se(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e, v = (s, t) => {
|
|
17
16
|
for (var e in t || (t = {}))
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
for (var e of
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
},
|
|
24
|
-
var
|
|
17
|
+
ce.call(t, e) && $(s, e, t[e]);
|
|
18
|
+
if (U)
|
|
19
|
+
for (var e of U(t))
|
|
20
|
+
oe.call(t, e) && $(s, e, t[e]);
|
|
21
|
+
return s;
|
|
22
|
+
}, F = (s, t) => ue(s, le(t)), ie = (s, t, e) => $(s, typeof t != "symbol" ? t + "" : t, e), a = (s, t, e) => new Promise((r, n) => {
|
|
23
|
+
var u = (o) => {
|
|
25
24
|
try {
|
|
26
|
-
|
|
27
|
-
} catch (
|
|
28
|
-
|
|
25
|
+
c(e.next(o));
|
|
26
|
+
} catch (i) {
|
|
27
|
+
n(i);
|
|
29
28
|
}
|
|
30
|
-
},
|
|
29
|
+
}, l = (o) => {
|
|
31
30
|
try {
|
|
32
|
-
|
|
33
|
-
} catch (
|
|
34
|
-
|
|
31
|
+
c(e.throw(o));
|
|
32
|
+
} catch (i) {
|
|
33
|
+
n(i);
|
|
35
34
|
}
|
|
36
|
-
},
|
|
37
|
-
|
|
35
|
+
}, c = (o) => o.done ? r(o.value) : Promise.resolve(o.value).then(u, l);
|
|
36
|
+
c((e = e.apply(s, t)).next());
|
|
38
37
|
});
|
|
39
|
-
function
|
|
40
|
-
return
|
|
38
|
+
function ae(s) {
|
|
39
|
+
return s === s.toUpperCase() ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : s;
|
|
41
40
|
}
|
|
42
|
-
const
|
|
41
|
+
const x = {
|
|
43
42
|
/**
|
|
44
43
|
* Creates generic fields that are commonly used across MongoDB documents.
|
|
45
44
|
* This function generates standard fields including a UUID, deletion flag, and timestamps
|
|
@@ -49,7 +48,7 @@ const w = {
|
|
|
49
48
|
*/
|
|
50
49
|
createGenericFields() {
|
|
51
50
|
return {
|
|
52
|
-
id:
|
|
51
|
+
id: G(),
|
|
53
52
|
isDel: !1,
|
|
54
53
|
createdAt: /* @__PURE__ */ new Date(),
|
|
55
54
|
updatedAt: /* @__PURE__ */ new Date()
|
|
@@ -63,8 +62,8 @@ const w = {
|
|
|
63
62
|
* @param schema - The Mongoose schema to apply plugins to.
|
|
64
63
|
* @param plugins - An array of plugin functions or false values to filter and apply.
|
|
65
64
|
*/
|
|
66
|
-
applyPlugins(
|
|
67
|
-
t.filter((e) => typeof e == "function").forEach((e) =>
|
|
65
|
+
applyPlugins(s, t) {
|
|
66
|
+
t.filter((e) => typeof e == "function").forEach((e) => s.plugin(e));
|
|
68
67
|
},
|
|
69
68
|
/**
|
|
70
69
|
* Applies middleware functions to a Mongoose schema.
|
|
@@ -73,9 +72,9 @@ const w = {
|
|
|
73
72
|
* @param schema - The Mongoose schema to apply middleware to.
|
|
74
73
|
* @param middlewares - An array of middleware configurations with method, pre, and post functions.
|
|
75
74
|
*/
|
|
76
|
-
applyMiddlewares(
|
|
77
|
-
t.forEach(({ method: e, pre: r, post:
|
|
78
|
-
e && r &&
|
|
75
|
+
applyMiddlewares(s, t) {
|
|
76
|
+
t.forEach(({ method: e, pre: r, post: n }) => {
|
|
77
|
+
e && r && s.pre(e, r), e && n && s.post(e, n);
|
|
79
78
|
});
|
|
80
79
|
},
|
|
81
80
|
/**
|
|
@@ -86,10 +85,10 @@ const w = {
|
|
|
86
85
|
* @param mongoose - The Mongoose instance to create the schema with.
|
|
87
86
|
* @returns A Mongoose schema with generic document fields.
|
|
88
87
|
*/
|
|
89
|
-
createGenericSchema(
|
|
90
|
-
return new
|
|
88
|
+
createGenericSchema(s) {
|
|
89
|
+
return new s.Schema(
|
|
91
90
|
{
|
|
92
|
-
id: { type: String, default:
|
|
91
|
+
id: { type: String, default: G, unique: !0 },
|
|
93
92
|
isDel: { type: Boolean, default: !1 }
|
|
94
93
|
},
|
|
95
94
|
{ timestamps: !0 }
|
|
@@ -108,34 +107,34 @@ const w = {
|
|
|
108
107
|
* @returns A configured Mongoose schema.
|
|
109
108
|
*/
|
|
110
109
|
createSchema({
|
|
111
|
-
mongoose:
|
|
110
|
+
mongoose: s,
|
|
112
111
|
schema: t,
|
|
113
112
|
virtuals: e = [],
|
|
114
113
|
standalone: r = !1
|
|
115
114
|
}) {
|
|
116
|
-
const
|
|
115
|
+
const n = new s.Schema(t, {
|
|
117
116
|
toJSON: { virtuals: !0 },
|
|
118
117
|
// So `res.json()` and other `JSON.stringify()` functions include virtuals
|
|
119
118
|
toObject: { virtuals: !0 }
|
|
120
119
|
// So `console.log()` and other functions that use `toObject()` include virtuals
|
|
121
120
|
});
|
|
122
|
-
return e.forEach(({ name:
|
|
123
|
-
if (
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
name:
|
|
127
|
-
options:
|
|
121
|
+
return e.forEach(({ name: u, options: l, get: c }) => {
|
|
122
|
+
if (x.isDynamicVirtual(l)) {
|
|
123
|
+
const o = n.statics;
|
|
124
|
+
o._dynamicVirtuals || (o._dynamicVirtuals = []), o._dynamicVirtuals.push({
|
|
125
|
+
name: u,
|
|
126
|
+
options: l
|
|
128
127
|
});
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
var
|
|
132
|
-
return ((
|
|
128
|
+
const i = n.virtual(u);
|
|
129
|
+
c ? i.get(c) : i.get(function() {
|
|
130
|
+
var d;
|
|
131
|
+
return ((d = this._populated) == null ? void 0 : d[u]) || (l != null && l.count ? 0 : l != null && l.justOne ? null : []);
|
|
133
132
|
});
|
|
134
133
|
} else {
|
|
135
|
-
const
|
|
136
|
-
|
|
134
|
+
const o = n.virtual(u, l);
|
|
135
|
+
c && o.get(c);
|
|
137
136
|
}
|
|
138
|
-
}), r ||
|
|
137
|
+
}), r || n.add(x.createGenericSchema(s)), n;
|
|
139
138
|
},
|
|
140
139
|
/**
|
|
141
140
|
* Creates a Mongoose model with plugins, middleware, and pagination support.
|
|
@@ -154,23 +153,23 @@ const w = {
|
|
|
154
153
|
* @throws {Error} When the model name is not provided.
|
|
155
154
|
*/
|
|
156
155
|
createModel({
|
|
157
|
-
mongoose:
|
|
156
|
+
mongoose: s,
|
|
158
157
|
name: t,
|
|
159
158
|
schema: e,
|
|
160
159
|
virtuals: r = [],
|
|
161
|
-
pagination:
|
|
162
|
-
aggregate:
|
|
163
|
-
middlewares:
|
|
160
|
+
pagination: n = !0,
|
|
161
|
+
aggregate: u = !0,
|
|
162
|
+
middlewares: l = []
|
|
164
163
|
}) {
|
|
165
164
|
if (!t)
|
|
166
165
|
throw new Error("Model name is required.");
|
|
167
|
-
if (
|
|
168
|
-
return
|
|
169
|
-
const
|
|
170
|
-
return (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
]),
|
|
166
|
+
if (s.models[t])
|
|
167
|
+
return s.models[t];
|
|
168
|
+
const c = x.createSchema({ mongoose: s, schema: e, virtuals: r });
|
|
169
|
+
return (n || u) && x.applyPlugins(c, [
|
|
170
|
+
n && k,
|
|
171
|
+
u && W
|
|
172
|
+
]), x.applyMiddlewares(c, l), s.model(t, c);
|
|
174
173
|
},
|
|
175
174
|
/**
|
|
176
175
|
* Validation utilities for Mongoose schemas.
|
|
@@ -185,9 +184,9 @@ const w = {
|
|
|
185
184
|
* @returns A validation function that returns true if the field is not empty.
|
|
186
185
|
*/
|
|
187
186
|
isRequired() {
|
|
188
|
-
return function(
|
|
189
|
-
return
|
|
190
|
-
return !
|
|
187
|
+
return function(s) {
|
|
188
|
+
return a(this, null, function* () {
|
|
189
|
+
return !re.isEmpty(s);
|
|
191
190
|
});
|
|
192
191
|
};
|
|
193
192
|
},
|
|
@@ -200,12 +199,12 @@ const w = {
|
|
|
200
199
|
* @returns A validation function that returns true if the value is unique across the specified fields.
|
|
201
200
|
* @throws {Error} When fields is not a non-empty array of strings.
|
|
202
201
|
*/
|
|
203
|
-
isUnique(
|
|
202
|
+
isUnique(s) {
|
|
204
203
|
return function(t) {
|
|
205
|
-
return
|
|
206
|
-
if (!Array.isArray(
|
|
204
|
+
return a(this, null, function* () {
|
|
205
|
+
if (!Array.isArray(s) || s.length === 0)
|
|
207
206
|
throw new Error("Fields must be a non-empty array of strings.");
|
|
208
|
-
const e = { $or:
|
|
207
|
+
const e = { $or: s.map((n) => ({ [n]: t })) };
|
|
209
208
|
return !(yield this.constructor.exists(e));
|
|
210
209
|
});
|
|
211
210
|
};
|
|
@@ -219,12 +218,12 @@ const w = {
|
|
|
219
218
|
* @returns A validation function that returns true if the value matches all regex patterns.
|
|
220
219
|
* @throws {Error} When regexArray is not an array of valid RegExp objects.
|
|
221
220
|
*/
|
|
222
|
-
matchesRegex(
|
|
221
|
+
matchesRegex(s) {
|
|
223
222
|
return function(t) {
|
|
224
|
-
return
|
|
225
|
-
if (!Array.isArray(
|
|
223
|
+
return a(this, null, function* () {
|
|
224
|
+
if (!Array.isArray(s) || s.some((e) => !(e instanceof RegExp)))
|
|
226
225
|
throw new Error("regexArray must be an array of valid RegExp objects.");
|
|
227
|
-
return
|
|
226
|
+
return s.every((e) => e.test(t));
|
|
228
227
|
});
|
|
229
228
|
};
|
|
230
229
|
}
|
|
@@ -233,7 +232,7 @@ const w = {
|
|
|
233
232
|
* Migration utilities for MongoDB.
|
|
234
233
|
* This object extends the migrate-mongo library with additional configuration utilities.
|
|
235
234
|
*/
|
|
236
|
-
migrate:
|
|
235
|
+
migrate: F(v({}, Q), {
|
|
237
236
|
/**
|
|
238
237
|
* Sets the migration configuration and updates .gitignore.
|
|
239
238
|
* This function creates a migration configuration file and ensures it's properly
|
|
@@ -241,15 +240,15 @@ const w = {
|
|
|
241
240
|
*
|
|
242
241
|
* @param options - Migration configuration options to write to the config file.
|
|
243
242
|
*/
|
|
244
|
-
setConfig: (
|
|
243
|
+
setConfig: (s) => {
|
|
245
244
|
const t = `// This file is automatically generated by the Cyberskill CLI.
|
|
246
|
-
module.exports = ${JSON.stringify(
|
|
247
|
-
|
|
245
|
+
module.exports = ${JSON.stringify(s, null, 4)}`;
|
|
246
|
+
q(w.MIGRATE_MONGO_CONFIG, t);
|
|
248
247
|
const e = `
|
|
249
|
-
${
|
|
248
|
+
${L}
|
|
250
249
|
`;
|
|
251
|
-
|
|
252
|
-
`).includes(
|
|
250
|
+
Z(w.GIT_IGNORE) ? ee(w.GIT_IGNORE, "utf-8").split(`
|
|
251
|
+
`).includes(L) || te(w.GIT_IGNORE, e) : q(w.GIT_IGNORE, e);
|
|
253
252
|
}
|
|
254
253
|
}),
|
|
255
254
|
/**
|
|
@@ -261,20 +260,20 @@ ${U}
|
|
|
261
260
|
* @param fields - An array of field names to convert to regex patterns.
|
|
262
261
|
* @returns A new filter object with string values converted to regex patterns.
|
|
263
262
|
*/
|
|
264
|
-
regexify(
|
|
265
|
-
if (!
|
|
263
|
+
regexify(s, t) {
|
|
264
|
+
if (!s)
|
|
266
265
|
return {};
|
|
267
|
-
let e =
|
|
266
|
+
let e = B(s);
|
|
268
267
|
if (!t || t.length === 0)
|
|
269
268
|
return e;
|
|
270
269
|
for (const r of t) {
|
|
271
|
-
const
|
|
272
|
-
if (typeof
|
|
273
|
-
const
|
|
274
|
-
$regex: `.*${
|
|
270
|
+
const n = r.toString().split("."), u = X(e, n);
|
|
271
|
+
if (typeof u == "string" && u.length > 0) {
|
|
272
|
+
const l = {
|
|
273
|
+
$regex: `.*${Y(u)}.*`,
|
|
275
274
|
$options: "i"
|
|
276
275
|
};
|
|
277
|
-
e =
|
|
276
|
+
e = H(e, n, l);
|
|
278
277
|
}
|
|
279
278
|
}
|
|
280
279
|
return e;
|
|
@@ -285,8 +284,8 @@ ${U}
|
|
|
285
284
|
* @param options - The virtual options to check.
|
|
286
285
|
* @returns True if the options contain a dynamic ref function.
|
|
287
286
|
*/
|
|
288
|
-
isDynamicVirtual(
|
|
289
|
-
return !!(
|
|
287
|
+
isDynamicVirtual(s) {
|
|
288
|
+
return !!(s && typeof s.ref == "function");
|
|
290
289
|
},
|
|
291
290
|
/**
|
|
292
291
|
* Generic utility function to get new records from the database
|
|
@@ -295,12 +294,12 @@ ${U}
|
|
|
295
294
|
* @param filterFn - Function to determine if a record already exists
|
|
296
295
|
* @returns Array of records that don't exist in the database
|
|
297
296
|
*/
|
|
298
|
-
getNewRecords(
|
|
299
|
-
return
|
|
300
|
-
const r = yield
|
|
297
|
+
getNewRecords(s, t, e) {
|
|
298
|
+
return a(this, null, function* () {
|
|
299
|
+
const r = yield s.findAll({});
|
|
301
300
|
return r.success ? t.filter(
|
|
302
|
-
(
|
|
303
|
-
(
|
|
301
|
+
(u) => !r.result.some(
|
|
302
|
+
(l) => e(l, u)
|
|
304
303
|
)
|
|
305
304
|
) : t;
|
|
306
305
|
});
|
|
@@ -312,160 +311,163 @@ ${U}
|
|
|
312
311
|
* @param filterFn - Function to determine if a record exists
|
|
313
312
|
* @returns Array of existing records that match the filter criteria
|
|
314
313
|
*/
|
|
315
|
-
getExistingRecords(
|
|
316
|
-
return
|
|
317
|
-
const r = yield
|
|
314
|
+
getExistingRecords(s, t, e) {
|
|
315
|
+
return a(this, null, function* () {
|
|
316
|
+
const r = yield s.findAll({});
|
|
318
317
|
return r.success ? r.result.filter(
|
|
319
|
-
(
|
|
320
|
-
(
|
|
318
|
+
(u) => t.some(
|
|
319
|
+
(l) => e(u, l)
|
|
321
320
|
)
|
|
322
321
|
) : [];
|
|
323
322
|
});
|
|
324
323
|
}
|
|
325
324
|
};
|
|
326
|
-
function
|
|
327
|
-
if (!
|
|
328
|
-
return
|
|
325
|
+
function C(s, t) {
|
|
326
|
+
if (!s || !t || t.length === 0)
|
|
327
|
+
return s;
|
|
329
328
|
const e = new Set(t.map((r) => r.name));
|
|
330
|
-
if (Array.isArray(
|
|
331
|
-
const r =
|
|
332
|
-
if (typeof
|
|
333
|
-
return !e.has(
|
|
334
|
-
if (typeof
|
|
335
|
-
const
|
|
336
|
-
return !e.has(
|
|
329
|
+
if (Array.isArray(s)) {
|
|
330
|
+
const r = s.filter((n) => {
|
|
331
|
+
if (typeof n == "string")
|
|
332
|
+
return !e.has(n);
|
|
333
|
+
if (typeof n == "object" && n !== null) {
|
|
334
|
+
const u = n;
|
|
335
|
+
return !e.has(u.path || u.populate || "");
|
|
337
336
|
}
|
|
338
337
|
return !0;
|
|
339
338
|
});
|
|
340
339
|
return r.length > 0 ? r : void 0;
|
|
341
340
|
}
|
|
342
|
-
if (typeof
|
|
343
|
-
return e.has(
|
|
344
|
-
if (typeof
|
|
345
|
-
const r =
|
|
346
|
-
return e.has(
|
|
341
|
+
if (typeof s == "string")
|
|
342
|
+
return e.has(s) ? void 0 : s;
|
|
343
|
+
if (typeof s == "object" && s !== null) {
|
|
344
|
+
const r = s, n = r.path || r.populate || "";
|
|
345
|
+
return e.has(n) ? void 0 : s;
|
|
347
346
|
}
|
|
348
|
-
return
|
|
347
|
+
return s;
|
|
349
348
|
}
|
|
350
|
-
function
|
|
351
|
-
if (!
|
|
349
|
+
function de(s, t, e) {
|
|
350
|
+
if (!s.length || !t || !(e != null && e.ref))
|
|
352
351
|
return [];
|
|
353
352
|
const r = /* @__PURE__ */ new Map();
|
|
354
|
-
return
|
|
353
|
+
return s.forEach((n) => {
|
|
355
354
|
try {
|
|
356
|
-
const
|
|
357
|
-
if (
|
|
355
|
+
const u = e.ref(n);
|
|
356
|
+
if (u == null)
|
|
358
357
|
return;
|
|
359
|
-
const
|
|
360
|
-
if (
|
|
361
|
-
const
|
|
362
|
-
r.has(
|
|
358
|
+
const l = typeof u == "string" ? u : String(u);
|
|
359
|
+
if (l && l.trim() !== "") {
|
|
360
|
+
const c = ae(l);
|
|
361
|
+
r.has(c) || r.set(c, []), r.get(c).push(n);
|
|
363
362
|
}
|
|
364
|
-
} catch (
|
|
365
|
-
|
|
363
|
+
} catch (u) {
|
|
364
|
+
f(new Error(`Dynamic ref function failed for virtual "${t}": ${u instanceof Error ? u.message : String(u)}`));
|
|
366
365
|
}
|
|
367
|
-
}), Array.from(r.entries()).map(([
|
|
366
|
+
}), Array.from(r.entries()).map(([n, u]) => ({ model: n, docs: u }));
|
|
368
367
|
}
|
|
369
|
-
function
|
|
370
|
-
return
|
|
368
|
+
function fe(s) {
|
|
369
|
+
return s !== null && typeof s == "object" && "toObject" in s && typeof s.toObject == "function";
|
|
370
|
+
}
|
|
371
|
+
function z(s, t, e, r, n) {
|
|
372
|
+
return a(this, null, function* () {
|
|
371
373
|
if (!t.length || !e.length || !r)
|
|
372
374
|
return t;
|
|
373
|
-
const
|
|
375
|
+
const u = e.filter((o) => {
|
|
374
376
|
if (Array.isArray(r))
|
|
375
|
-
return r.length > 0 && r.some((
|
|
377
|
+
return r.length > 0 && r.some((i) => typeof i == "string" ? i === o.name : i.path === o.name);
|
|
376
378
|
if (typeof r == "string")
|
|
377
|
-
return r ===
|
|
379
|
+
return r === o.name;
|
|
378
380
|
if (typeof r == "object" && r !== null) {
|
|
379
|
-
const
|
|
380
|
-
return
|
|
381
|
+
const i = r;
|
|
382
|
+
return i.path && i.path === o.name || i.populate && i.populate === o.name;
|
|
381
383
|
}
|
|
382
384
|
return !1;
|
|
383
385
|
});
|
|
384
|
-
if (
|
|
386
|
+
if (u.length === 0)
|
|
385
387
|
return t;
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
const l = B(t.map((o) => fe(o) ? o.toObject() : o));
|
|
389
|
+
l.forEach((o) => {
|
|
390
|
+
u.forEach(({ name: i, options: d }) => {
|
|
391
|
+
i in o || (o[i] = d.count ? 0 : d.justOne ? null : []);
|
|
390
392
|
});
|
|
391
393
|
});
|
|
392
|
-
const
|
|
393
|
-
for (const
|
|
394
|
-
const { name:
|
|
394
|
+
const c = /* @__PURE__ */ new Map();
|
|
395
|
+
for (const o of u) {
|
|
396
|
+
const { name: i, options: d } = o, h = de(l, i, d);
|
|
395
397
|
for (const m of h) {
|
|
396
|
-
|
|
398
|
+
c.has(m.model) || c.set(m.model, {
|
|
397
399
|
virtuals: [],
|
|
398
400
|
localValueSets: /* @__PURE__ */ new Map(),
|
|
399
401
|
docsByLocalValue: /* @__PURE__ */ new Map()
|
|
400
402
|
});
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
const
|
|
404
|
-
m.docs.forEach((
|
|
405
|
-
const
|
|
406
|
-
if (
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
let
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
var O,
|
|
413
|
-
return ((
|
|
414
|
-
})),
|
|
403
|
+
const y = c.get(m.model);
|
|
404
|
+
y.virtuals.find((N) => N.name === i) || (y.virtuals.push(o), y.localValueSets.set(i, /* @__PURE__ */ new Set()));
|
|
405
|
+
const R = y.localValueSets.get(i);
|
|
406
|
+
m.docs.forEach((N) => {
|
|
407
|
+
const j = N[d.localField];
|
|
408
|
+
if (j != null) {
|
|
409
|
+
const E = String(j);
|
|
410
|
+
R.add(E);
|
|
411
|
+
let S = -1;
|
|
412
|
+
const b = N;
|
|
413
|
+
b.id !== void 0 ? S = l.findIndex((M) => M.id === b.id) : b._id !== void 0 && (S = l.findIndex((M) => {
|
|
414
|
+
var O, g, _, p;
|
|
415
|
+
return ((g = (O = M._id) == null ? void 0 : O.toString) == null ? void 0 : g.call(O)) === ((p = (_ = b._id) == null ? void 0 : _.toString) == null ? void 0 : p.call(_));
|
|
416
|
+
})), S !== -1 && (y.docsByLocalValue.has(E) || y.docsByLocalValue.set(E, []), y.docsByLocalValue.get(E).push(S));
|
|
415
417
|
}
|
|
416
418
|
});
|
|
417
419
|
}
|
|
418
420
|
}
|
|
419
|
-
return yield Promise.all(Array.from(
|
|
420
|
-
const h =
|
|
421
|
+
return yield Promise.all(Array.from(c.entries()).map((o) => a(null, [o], function* ([i, d]) {
|
|
422
|
+
const h = s.models[i];
|
|
421
423
|
if (!h)
|
|
422
424
|
return;
|
|
423
425
|
const m = /* @__PURE__ */ new Set();
|
|
424
|
-
if (
|
|
425
|
-
|
|
426
|
+
if (d.localValueSets.forEach((E) => {
|
|
427
|
+
E.forEach((S) => m.add(S));
|
|
426
428
|
}), m.size === 0)
|
|
427
429
|
return;
|
|
428
|
-
const
|
|
429
|
-
let
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
for (const
|
|
433
|
-
const { name:
|
|
434
|
-
const
|
|
435
|
-
return
|
|
430
|
+
const y = [...new Set(d.virtuals.map((E) => E.options.foreignField))], R = Array.from(m);
|
|
431
|
+
let N;
|
|
432
|
+
y.length === 1 ? N = { [String(y[0])]: { $in: R } } : N = { $or: y.map((E) => ({ [E]: { $in: R } })) };
|
|
433
|
+
const j = yield h.find(N, n).lean();
|
|
434
|
+
for (const E of d.virtuals) {
|
|
435
|
+
const { name: S, options: b } = E, M = j.filter((O) => {
|
|
436
|
+
const g = O[b.foreignField];
|
|
437
|
+
return g != null && m.has(String(g));
|
|
436
438
|
});
|
|
437
|
-
if (
|
|
439
|
+
if (b.count) {
|
|
438
440
|
const O = /* @__PURE__ */ new Map();
|
|
439
|
-
|
|
440
|
-
var
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
}),
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
const P =
|
|
447
|
-
P[
|
|
441
|
+
M.forEach((g) => {
|
|
442
|
+
var _;
|
|
443
|
+
const p = (_ = g[b.foreignField]) == null ? void 0 : _.toString();
|
|
444
|
+
p && O.set(p, (O.get(p) || 0) + 1);
|
|
445
|
+
}), d.localValueSets.get(S).forEach((g) => {
|
|
446
|
+
const _ = d.docsByLocalValue.get(g) || [], p = O.get(g) || 0;
|
|
447
|
+
_.forEach((A) => {
|
|
448
|
+
const P = l[A];
|
|
449
|
+
P[S] === void 0 && (P[S] = p);
|
|
448
450
|
});
|
|
449
451
|
});
|
|
450
452
|
} else {
|
|
451
453
|
const O = /* @__PURE__ */ new Map();
|
|
452
|
-
|
|
453
|
-
var
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
}),
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
const
|
|
460
|
-
|
|
454
|
+
M.forEach((g) => {
|
|
455
|
+
var _;
|
|
456
|
+
const p = (_ = g[b.foreignField]) == null ? void 0 : _.toString();
|
|
457
|
+
p && (O.has(p) || O.set(p, []), O.get(p).push(g));
|
|
458
|
+
}), d.localValueSets.get(S).forEach((g) => {
|
|
459
|
+
const _ = d.docsByLocalValue.get(g) || [], p = O.get(g) || [], A = b.justOne ? p[0] || null : p;
|
|
460
|
+
_.forEach((P) => {
|
|
461
|
+
const K = l[P];
|
|
462
|
+
K[S] = A;
|
|
461
463
|
});
|
|
462
464
|
});
|
|
463
465
|
}
|
|
464
466
|
}
|
|
465
|
-
}))),
|
|
467
|
+
}))), l;
|
|
466
468
|
});
|
|
467
469
|
}
|
|
468
|
-
class
|
|
470
|
+
class Ve {
|
|
469
471
|
/**
|
|
470
472
|
* Creates a new MongoDB controller instance.
|
|
471
473
|
*
|
|
@@ -473,7 +475,7 @@ class xe {
|
|
|
473
475
|
* @param collectionName - The name of the collection to operate on.
|
|
474
476
|
*/
|
|
475
477
|
constructor(t, e) {
|
|
476
|
-
|
|
478
|
+
ie(this, "collection"), this.collection = t.collection(e);
|
|
477
479
|
}
|
|
478
480
|
/**
|
|
479
481
|
* Creates a single document in the collection.
|
|
@@ -483,9 +485,9 @@ class xe {
|
|
|
483
485
|
* @returns A promise that resolves to a standardized response with the created document.
|
|
484
486
|
*/
|
|
485
487
|
createOne(t) {
|
|
486
|
-
return
|
|
488
|
+
return a(this, null, function* () {
|
|
487
489
|
try {
|
|
488
|
-
const e =
|
|
490
|
+
const e = v(v({}, x.createGenericFields()), t);
|
|
489
491
|
return (yield this.collection.insertOne(e)).acknowledged ? {
|
|
490
492
|
success: !0,
|
|
491
493
|
message: "Document created successfully",
|
|
@@ -493,10 +495,10 @@ class xe {
|
|
|
493
495
|
} : {
|
|
494
496
|
success: !1,
|
|
495
497
|
message: "Document creation failed",
|
|
496
|
-
code:
|
|
498
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
497
499
|
};
|
|
498
500
|
} catch (e) {
|
|
499
|
-
return
|
|
501
|
+
return f(e);
|
|
500
502
|
}
|
|
501
503
|
});
|
|
502
504
|
}
|
|
@@ -508,20 +510,20 @@ class xe {
|
|
|
508
510
|
* @returns A promise that resolves to a standardized response with the created documents.
|
|
509
511
|
*/
|
|
510
512
|
createMany(t) {
|
|
511
|
-
return
|
|
513
|
+
return a(this, null, function* () {
|
|
512
514
|
try {
|
|
513
|
-
const e = t.map((
|
|
515
|
+
const e = t.map((n) => v(v({}, x.createGenericFields()), n)), r = yield this.collection.insertMany(e);
|
|
514
516
|
return r.insertedCount === 0 ? {
|
|
515
517
|
success: !1,
|
|
516
518
|
message: "No documents were inserted",
|
|
517
|
-
code:
|
|
519
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
518
520
|
} : {
|
|
519
521
|
success: !0,
|
|
520
522
|
message: `${r.insertedCount} documents created successfully`,
|
|
521
523
|
result: e
|
|
522
524
|
};
|
|
523
525
|
} catch (e) {
|
|
524
|
-
return
|
|
526
|
+
return f(e);
|
|
525
527
|
}
|
|
526
528
|
});
|
|
527
529
|
}
|
|
@@ -532,12 +534,12 @@ class xe {
|
|
|
532
534
|
* @returns A promise that resolves to a standardized response with the found document.
|
|
533
535
|
*/
|
|
534
536
|
findOne(t) {
|
|
535
|
-
return
|
|
537
|
+
return a(this, null, function* () {
|
|
536
538
|
try {
|
|
537
539
|
const e = yield this.collection.findOne(t);
|
|
538
|
-
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code:
|
|
540
|
+
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code: D.NOT_FOUND.CODE };
|
|
539
541
|
} catch (e) {
|
|
540
|
-
return
|
|
542
|
+
return f(e);
|
|
541
543
|
}
|
|
542
544
|
});
|
|
543
545
|
}
|
|
@@ -548,7 +550,7 @@ class xe {
|
|
|
548
550
|
* @returns A promise that resolves to a standardized response with the found documents.
|
|
549
551
|
*/
|
|
550
552
|
findAll() {
|
|
551
|
-
return
|
|
553
|
+
return a(this, arguments, function* (t = {}) {
|
|
552
554
|
try {
|
|
553
555
|
return {
|
|
554
556
|
success: !0,
|
|
@@ -556,7 +558,7 @@ class xe {
|
|
|
556
558
|
result: yield this.collection.find(t).toArray()
|
|
557
559
|
};
|
|
558
560
|
} catch (e) {
|
|
559
|
-
return
|
|
561
|
+
return f(e);
|
|
560
562
|
}
|
|
561
563
|
});
|
|
562
564
|
}
|
|
@@ -567,7 +569,7 @@ class xe {
|
|
|
567
569
|
* @returns A promise that resolves to a standardized response with the document count.
|
|
568
570
|
*/
|
|
569
571
|
count() {
|
|
570
|
-
return
|
|
572
|
+
return a(this, arguments, function* (t = {}) {
|
|
571
573
|
try {
|
|
572
574
|
const e = yield this.collection.countDocuments(t);
|
|
573
575
|
return {
|
|
@@ -576,7 +578,7 @@ class xe {
|
|
|
576
578
|
result: e
|
|
577
579
|
};
|
|
578
580
|
} catch (e) {
|
|
579
|
-
return
|
|
581
|
+
return f(e);
|
|
580
582
|
}
|
|
581
583
|
});
|
|
582
584
|
}
|
|
@@ -588,7 +590,7 @@ class xe {
|
|
|
588
590
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
589
591
|
*/
|
|
590
592
|
updateOne(t, e) {
|
|
591
|
-
return
|
|
593
|
+
return a(this, null, function* () {
|
|
592
594
|
try {
|
|
593
595
|
const r = yield this.collection.updateOne(t, {
|
|
594
596
|
$set: e
|
|
@@ -596,14 +598,14 @@ class xe {
|
|
|
596
598
|
return r.matchedCount === 0 ? {
|
|
597
599
|
success: !1,
|
|
598
600
|
message: "No documents matched the filter",
|
|
599
|
-
code:
|
|
601
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
600
602
|
} : {
|
|
601
603
|
success: !0,
|
|
602
604
|
message: "Document updated successfully",
|
|
603
605
|
result: r
|
|
604
606
|
};
|
|
605
607
|
} catch (r) {
|
|
606
|
-
return
|
|
608
|
+
return f(r);
|
|
607
609
|
}
|
|
608
610
|
});
|
|
609
611
|
}
|
|
@@ -615,7 +617,7 @@ class xe {
|
|
|
615
617
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
616
618
|
*/
|
|
617
619
|
updateMany(t, e) {
|
|
618
|
-
return
|
|
620
|
+
return a(this, null, function* () {
|
|
619
621
|
try {
|
|
620
622
|
const r = yield this.collection.updateMany(t, {
|
|
621
623
|
$set: e
|
|
@@ -623,14 +625,14 @@ class xe {
|
|
|
623
625
|
return r.matchedCount === 0 ? {
|
|
624
626
|
success: !1,
|
|
625
627
|
message: "No documents matched the filter",
|
|
626
|
-
code:
|
|
628
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
627
629
|
} : {
|
|
628
630
|
success: !0,
|
|
629
631
|
message: "Documents updated successfully",
|
|
630
632
|
result: r
|
|
631
633
|
};
|
|
632
634
|
} catch (r) {
|
|
633
|
-
return
|
|
635
|
+
return f(r);
|
|
634
636
|
}
|
|
635
637
|
});
|
|
636
638
|
}
|
|
@@ -641,20 +643,20 @@ class xe {
|
|
|
641
643
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
642
644
|
*/
|
|
643
645
|
deleteOne(t) {
|
|
644
|
-
return
|
|
646
|
+
return a(this, null, function* () {
|
|
645
647
|
try {
|
|
646
648
|
const e = yield this.collection.deleteOne(t);
|
|
647
649
|
return e.deletedCount === 0 ? {
|
|
648
650
|
success: !1,
|
|
649
651
|
message: "No documents matched the filter",
|
|
650
|
-
code:
|
|
652
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
651
653
|
} : {
|
|
652
654
|
success: !0,
|
|
653
655
|
message: "Document deleted successfully",
|
|
654
656
|
result: e
|
|
655
657
|
};
|
|
656
658
|
} catch (e) {
|
|
657
|
-
return
|
|
659
|
+
return f(e);
|
|
658
660
|
}
|
|
659
661
|
});
|
|
660
662
|
}
|
|
@@ -665,25 +667,25 @@ class xe {
|
|
|
665
667
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
666
668
|
*/
|
|
667
669
|
deleteMany(t) {
|
|
668
|
-
return
|
|
670
|
+
return a(this, null, function* () {
|
|
669
671
|
try {
|
|
670
672
|
const e = yield this.collection.deleteMany(t);
|
|
671
673
|
return e.deletedCount === 0 ? {
|
|
672
674
|
success: !1,
|
|
673
675
|
message: "No documents matched the filter",
|
|
674
|
-
code:
|
|
676
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
675
677
|
} : {
|
|
676
678
|
success: !0,
|
|
677
679
|
message: "Documents deleted successfully",
|
|
678
680
|
result: e
|
|
679
681
|
};
|
|
680
682
|
} catch (e) {
|
|
681
|
-
return
|
|
683
|
+
return f(e);
|
|
682
684
|
}
|
|
683
685
|
});
|
|
684
686
|
}
|
|
685
687
|
}
|
|
686
|
-
class
|
|
688
|
+
class xe {
|
|
687
689
|
/**
|
|
688
690
|
* Creates a new Mongoose controller instance.
|
|
689
691
|
*
|
|
@@ -716,11 +718,11 @@ class we {
|
|
|
716
718
|
* @returns The document with dynamic virtuals populated.
|
|
717
719
|
*/
|
|
718
720
|
populateDynamicVirtualsForDocument(t, e) {
|
|
719
|
-
return
|
|
721
|
+
return a(this, null, function* () {
|
|
720
722
|
const r = this.getDynamicVirtuals();
|
|
721
723
|
if (r && r.length > 0) {
|
|
722
|
-
const
|
|
723
|
-
return
|
|
724
|
+
const n = yield z(this.model.base, [t], r, e);
|
|
725
|
+
return n && n[0] ? n[0] : t;
|
|
724
726
|
}
|
|
725
727
|
return t;
|
|
726
728
|
});
|
|
@@ -733,9 +735,9 @@ class we {
|
|
|
733
735
|
* @returns The documents with dynamic virtuals populated.
|
|
734
736
|
*/
|
|
735
737
|
populateDynamicVirtualsForDocuments(t, e) {
|
|
736
|
-
return
|
|
738
|
+
return a(this, null, function* () {
|
|
737
739
|
const r = this.getDynamicVirtuals();
|
|
738
|
-
return r && r.length > 0 && t.length > 0 ? yield
|
|
740
|
+
return r && r.length > 0 && t.length > 0 ? yield z(this.model.base, t, r, e) : t;
|
|
739
741
|
});
|
|
740
742
|
}
|
|
741
743
|
/**
|
|
@@ -749,18 +751,22 @@ class we {
|
|
|
749
751
|
* @returns A promise that resolves to a standardized response with the found document.
|
|
750
752
|
*/
|
|
751
753
|
findOne() {
|
|
752
|
-
return
|
|
754
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
755
|
+
var u, l;
|
|
753
756
|
try {
|
|
754
|
-
const c =
|
|
755
|
-
|
|
756
|
-
const
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
757
|
+
const c = V(t), o = this.model.findOne(c, e, r), i = this.getDynamicVirtuals(), d = C(n, i);
|
|
758
|
+
d && o.populate(d);
|
|
759
|
+
const h = yield o.exec();
|
|
760
|
+
if (!h)
|
|
761
|
+
return {
|
|
762
|
+
success: !1,
|
|
763
|
+
message: `No ${this.getModelName()} found.`,
|
|
764
|
+
code: D.NOT_FOUND.CODE
|
|
765
|
+
};
|
|
766
|
+
const m = yield this.populateDynamicVirtualsForDocument(h, n);
|
|
767
|
+
return { success: !0, result: (l = (u = m == null ? void 0 : m.toObject) == null ? void 0 : u.call(m)) != null ? l : m };
|
|
762
768
|
} catch (c) {
|
|
763
|
-
return
|
|
769
|
+
return f(c);
|
|
764
770
|
}
|
|
765
771
|
});
|
|
766
772
|
}
|
|
@@ -775,14 +781,17 @@ class we {
|
|
|
775
781
|
* @returns A promise that resolves to a standardized response with the found documents.
|
|
776
782
|
*/
|
|
777
783
|
findAll() {
|
|
778
|
-
return
|
|
784
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
779
785
|
try {
|
|
780
|
-
const
|
|
781
|
-
|
|
782
|
-
const
|
|
783
|
-
return { success: !0, result: yield this.populateDynamicVirtualsForDocuments(
|
|
784
|
-
|
|
785
|
-
|
|
786
|
+
const u = V(t), l = this.model.find(u, e, r), c = this.getDynamicVirtuals(), o = C(n, c);
|
|
787
|
+
o && l.populate(o);
|
|
788
|
+
const i = yield l.exec();
|
|
789
|
+
return { success: !0, result: (yield this.populateDynamicVirtualsForDocuments(i, n)).map((h) => {
|
|
790
|
+
var m, y;
|
|
791
|
+
return (y = (m = h == null ? void 0 : h.toObject) == null ? void 0 : m.call(h)) != null ? y : h;
|
|
792
|
+
}) };
|
|
793
|
+
} catch (u) {
|
|
794
|
+
return f(u);
|
|
786
795
|
}
|
|
787
796
|
});
|
|
788
797
|
}
|
|
@@ -795,14 +804,17 @@ class we {
|
|
|
795
804
|
* @returns A promise that resolves to a standardized response with paginated results.
|
|
796
805
|
*/
|
|
797
806
|
findPaging() {
|
|
798
|
-
return
|
|
807
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
799
808
|
try {
|
|
800
|
-
const r =
|
|
801
|
-
e.populate && (
|
|
802
|
-
const
|
|
803
|
-
return { success: !0, result:
|
|
809
|
+
const r = V(t), n = this.getDynamicVirtuals(), u = v({}, e);
|
|
810
|
+
e.populate && (u.populate = C(e.populate, n));
|
|
811
|
+
const l = yield this.model.paginate(r, u), c = yield this.populateDynamicVirtualsForDocuments(l.docs, e.populate);
|
|
812
|
+
return { success: !0, result: F(v({}, l), { docs: c.map((o) => {
|
|
813
|
+
var i, d;
|
|
814
|
+
return (d = (i = o == null ? void 0 : o.toObject) == null ? void 0 : i.call(o)) != null ? d : o;
|
|
815
|
+
}) }) };
|
|
804
816
|
} catch (r) {
|
|
805
|
-
return
|
|
817
|
+
return f(r);
|
|
806
818
|
}
|
|
807
819
|
});
|
|
808
820
|
}
|
|
@@ -814,17 +826,20 @@ class we {
|
|
|
814
826
|
* @returns A promise that resolves to a standardized response with paginated aggregation results.
|
|
815
827
|
*/
|
|
816
828
|
findPagingAggregate(t) {
|
|
817
|
-
return
|
|
829
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
818
830
|
try {
|
|
819
|
-
const
|
|
820
|
-
r.populate && (
|
|
821
|
-
const
|
|
831
|
+
const n = this.getDynamicVirtuals(), u = v({}, r);
|
|
832
|
+
r.populate && (u.populate = C(r.populate, n));
|
|
833
|
+
const l = yield this.model.aggregatePaginate(
|
|
822
834
|
this.model.aggregate(e),
|
|
823
|
-
|
|
824
|
-
),
|
|
825
|
-
return { success: !0, result:
|
|
826
|
-
|
|
827
|
-
|
|
835
|
+
u
|
|
836
|
+
), c = yield this.populateDynamicVirtualsForDocuments(l.docs, r.populate);
|
|
837
|
+
return { success: !0, result: F(v({}, l), { docs: c.map((o) => {
|
|
838
|
+
var i, d;
|
|
839
|
+
return (d = (i = o == null ? void 0 : o.toObject) == null ? void 0 : i.call(o)) != null ? d : o;
|
|
840
|
+
}) }) };
|
|
841
|
+
} catch (n) {
|
|
842
|
+
return f(n);
|
|
828
843
|
}
|
|
829
844
|
});
|
|
830
845
|
}
|
|
@@ -835,12 +850,12 @@ class we {
|
|
|
835
850
|
* @returns A promise that resolves to a standardized response with the document count.
|
|
836
851
|
*/
|
|
837
852
|
count() {
|
|
838
|
-
return
|
|
853
|
+
return a(this, arguments, function* (t = {}) {
|
|
839
854
|
try {
|
|
840
|
-
const e =
|
|
855
|
+
const e = V(t);
|
|
841
856
|
return { success: !0, result: yield this.model.countDocuments(e) };
|
|
842
857
|
} catch (e) {
|
|
843
|
-
return
|
|
858
|
+
return f(e);
|
|
844
859
|
}
|
|
845
860
|
});
|
|
846
861
|
}
|
|
@@ -851,11 +866,13 @@ class we {
|
|
|
851
866
|
* @returns A promise that resolves to a standardized response with the created document.
|
|
852
867
|
*/
|
|
853
868
|
createOne(t) {
|
|
854
|
-
return
|
|
869
|
+
return a(this, null, function* () {
|
|
870
|
+
var e, r;
|
|
855
871
|
try {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
872
|
+
const n = yield this.model.create(t);
|
|
873
|
+
return { success: !0, result: (r = (e = n == null ? void 0 : n.toObject) == null ? void 0 : e.call(n)) != null ? r : n };
|
|
874
|
+
} catch (n) {
|
|
875
|
+
return f(n);
|
|
859
876
|
}
|
|
860
877
|
});
|
|
861
878
|
}
|
|
@@ -867,11 +884,14 @@ class we {
|
|
|
867
884
|
* @returns A promise that resolves to a standardized response with the created documents.
|
|
868
885
|
*/
|
|
869
886
|
createMany(t) {
|
|
870
|
-
return
|
|
887
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
871
888
|
try {
|
|
872
|
-
return { success: !0, result: yield this.model.insertMany(e, r)
|
|
873
|
-
|
|
874
|
-
|
|
889
|
+
return { success: !0, result: (yield this.model.insertMany(e, r)).map((u) => {
|
|
890
|
+
var l, c;
|
|
891
|
+
return (c = (l = u == null ? void 0 : u.toObject) == null ? void 0 : l.call(u)) != null ? c : u;
|
|
892
|
+
}) };
|
|
893
|
+
} catch (n) {
|
|
894
|
+
return f(n);
|
|
875
895
|
}
|
|
876
896
|
});
|
|
877
897
|
}
|
|
@@ -884,18 +904,19 @@ class we {
|
|
|
884
904
|
* @returns A promise that resolves to a standardized response with the updated document.
|
|
885
905
|
*/
|
|
886
906
|
updateOne() {
|
|
887
|
-
return
|
|
907
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
908
|
+
var n, u;
|
|
888
909
|
try {
|
|
889
|
-
const
|
|
910
|
+
const l = V(t), c = yield this.model.findOneAndUpdate(l, e, v({
|
|
890
911
|
new: !0
|
|
891
|
-
}, r)).exec()
|
|
892
|
-
return c ? { success: !0, result: c } : {
|
|
912
|
+
}, r)).exec();
|
|
913
|
+
return c ? { success: !0, result: (u = (n = c == null ? void 0 : c.toObject) == null ? void 0 : n.call(c)) != null ? u : c } : {
|
|
893
914
|
success: !1,
|
|
894
915
|
message: `Failed to update ${this.getModelName()}.`,
|
|
895
|
-
code:
|
|
916
|
+
code: D.NOT_FOUND.CODE
|
|
896
917
|
};
|
|
897
|
-
} catch (
|
|
898
|
-
return
|
|
918
|
+
} catch (l) {
|
|
919
|
+
return f(l);
|
|
899
920
|
}
|
|
900
921
|
});
|
|
901
922
|
}
|
|
@@ -908,12 +929,12 @@ class we {
|
|
|
908
929
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
909
930
|
*/
|
|
910
931
|
updateMany() {
|
|
911
|
-
return
|
|
932
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
912
933
|
try {
|
|
913
|
-
const
|
|
914
|
-
return { success: !0, result: yield this.model.updateMany(
|
|
915
|
-
} catch (
|
|
916
|
-
return
|
|
934
|
+
const n = V(t);
|
|
935
|
+
return { success: !0, result: yield this.model.updateMany(n, e, r).exec() };
|
|
936
|
+
} catch (n) {
|
|
937
|
+
return f(n);
|
|
917
938
|
}
|
|
918
939
|
});
|
|
919
940
|
}
|
|
@@ -925,16 +946,17 @@ class we {
|
|
|
925
946
|
* @returns A promise that resolves to a standardized response with the deleted document.
|
|
926
947
|
*/
|
|
927
948
|
deleteOne() {
|
|
928
|
-
return
|
|
949
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
950
|
+
var r, n;
|
|
929
951
|
try {
|
|
930
|
-
const
|
|
931
|
-
return
|
|
952
|
+
const u = V(t), l = yield this.model.findOneAndDelete(u, e).exec();
|
|
953
|
+
return l ? { success: !0, result: (n = (r = l == null ? void 0 : l.toObject) == null ? void 0 : r.call(l)) != null ? n : l } : {
|
|
932
954
|
success: !1,
|
|
933
955
|
message: `No ${this.getModelName()} found to delete.`,
|
|
934
|
-
code:
|
|
956
|
+
code: D.NOT_FOUND.CODE
|
|
935
957
|
};
|
|
936
|
-
} catch (
|
|
937
|
-
return
|
|
958
|
+
} catch (u) {
|
|
959
|
+
return f(u);
|
|
938
960
|
}
|
|
939
961
|
});
|
|
940
962
|
}
|
|
@@ -946,16 +968,16 @@ class we {
|
|
|
946
968
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
947
969
|
*/
|
|
948
970
|
deleteMany() {
|
|
949
|
-
return
|
|
971
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
950
972
|
try {
|
|
951
|
-
const r =
|
|
952
|
-
return
|
|
973
|
+
const r = V(t), n = yield this.model.deleteMany(r, e).exec();
|
|
974
|
+
return n.deletedCount === 0 ? {
|
|
953
975
|
success: !1,
|
|
954
976
|
message: "No documents found to delete.",
|
|
955
|
-
code:
|
|
956
|
-
} : { success: !0, result:
|
|
977
|
+
code: D.NOT_FOUND.CODE
|
|
978
|
+
} : { success: !0, result: n };
|
|
957
979
|
} catch (r) {
|
|
958
|
-
return
|
|
980
|
+
return f(r);
|
|
959
981
|
}
|
|
960
982
|
});
|
|
961
983
|
}
|
|
@@ -968,23 +990,23 @@ class we {
|
|
|
968
990
|
* @returns A promise that resolves to a standardized response with the unique short ID.
|
|
969
991
|
*/
|
|
970
992
|
createShortId(t, e = 4) {
|
|
971
|
-
return
|
|
993
|
+
return a(this, null, function* () {
|
|
972
994
|
try {
|
|
973
|
-
const
|
|
974
|
-
|
|
975
|
-
)).findIndex((
|
|
976
|
-
if (
|
|
977
|
-
const
|
|
978
|
-
if (
|
|
979
|
-
return { success: !0, result:
|
|
995
|
+
const n = Array.from({ length: 10 }, (c, o) => ne(t, o + e)), l = (yield Promise.all(
|
|
996
|
+
n.map((c) => this.model.exists({ shortId: c }))
|
|
997
|
+
)).findIndex((c) => !c);
|
|
998
|
+
if (l !== -1) {
|
|
999
|
+
const c = n[l];
|
|
1000
|
+
if (c)
|
|
1001
|
+
return { success: !0, result: c };
|
|
980
1002
|
}
|
|
981
1003
|
return {
|
|
982
1004
|
success: !1,
|
|
983
1005
|
message: "Failed to create a unique shortId",
|
|
984
|
-
code:
|
|
1006
|
+
code: D.INTERNAL_SERVER_ERROR.CODE
|
|
985
1007
|
};
|
|
986
1008
|
} catch (r) {
|
|
987
|
-
return
|
|
1009
|
+
return f(r);
|
|
988
1010
|
}
|
|
989
1011
|
});
|
|
990
1012
|
}
|
|
@@ -1000,17 +1022,17 @@ class we {
|
|
|
1000
1022
|
* @param options.filter - Additional filter conditions to apply to the query.
|
|
1001
1023
|
* @returns A MongoDB query object for checking slug existence.
|
|
1002
1024
|
*/
|
|
1003
|
-
createSlugQuery({ slug: t, field: e, isObject: r, haveHistory:
|
|
1004
|
-
const
|
|
1005
|
-
return r ?
|
|
1025
|
+
createSlugQuery({ slug: t, field: e, isObject: r, haveHistory: n = !1, filter: u }) {
|
|
1026
|
+
const l = v({}, u != null ? u : {});
|
|
1027
|
+
return r ? F(v({}, l), {
|
|
1006
1028
|
$or: [
|
|
1007
1029
|
{ [`slug.${e}`]: t },
|
|
1008
|
-
...
|
|
1030
|
+
...n ? [{ slugHistory: { $elemMatch: { [`slug.${e}`]: t } } }] : []
|
|
1009
1031
|
]
|
|
1010
|
-
}) :
|
|
1032
|
+
}) : F(v({}, l), {
|
|
1011
1033
|
$or: [
|
|
1012
1034
|
{ slug: t },
|
|
1013
|
-
...
|
|
1035
|
+
...n ? [{ slugHistory: t }] : []
|
|
1014
1036
|
]
|
|
1015
1037
|
});
|
|
1016
1038
|
}
|
|
@@ -1027,23 +1049,23 @@ class we {
|
|
|
1027
1049
|
* @returns A promise that resolves to a unique slug string.
|
|
1028
1050
|
*/
|
|
1029
1051
|
createUniqueSlug(t) {
|
|
1030
|
-
return
|
|
1052
|
+
return a(this, arguments, function* ({ slug: e, field: r, isObject: n, haveHistory: u, filter: l }) {
|
|
1031
1053
|
if (!e || typeof e != "string")
|
|
1032
1054
|
throw new Error("Invalid slug provided: must be a non-empty string");
|
|
1033
|
-
const
|
|
1055
|
+
const c = I(e);
|
|
1034
1056
|
if (!(yield this.model.exists(
|
|
1035
|
-
this.createSlugQuery({ slug:
|
|
1057
|
+
this.createSlugQuery({ slug: c, field: r, isObject: n, haveHistory: u, filter: l })
|
|
1036
1058
|
)))
|
|
1037
|
-
return
|
|
1038
|
-
for (let h = 1; h <=
|
|
1039
|
-
const m = `${
|
|
1059
|
+
return c;
|
|
1060
|
+
for (let h = 1; h <= J; h++) {
|
|
1061
|
+
const m = `${c}-${h}`;
|
|
1040
1062
|
if (!(yield this.model.exists(
|
|
1041
|
-
this.createSlugQuery({ slug: m, field: r, isObject:
|
|
1063
|
+
this.createSlugQuery({ slug: m, field: r, isObject: n, haveHistory: u, filter: l })
|
|
1042
1064
|
)))
|
|
1043
1065
|
return m;
|
|
1044
1066
|
}
|
|
1045
|
-
const
|
|
1046
|
-
return `${
|
|
1067
|
+
const i = Date.now(), d = Math.random().toString(36).substring(2, 8);
|
|
1068
|
+
return `${c}-${i}-${d}`;
|
|
1047
1069
|
});
|
|
1048
1070
|
}
|
|
1049
1071
|
/**
|
|
@@ -1058,31 +1080,31 @@ class we {
|
|
|
1058
1080
|
* @returns A promise that resolves to a standardized response with the created slug(s).
|
|
1059
1081
|
*/
|
|
1060
1082
|
createSlug(t) {
|
|
1061
|
-
return
|
|
1083
|
+
return a(this, arguments, function* ({ field: e, from: r, filter: n, haveHistory: u }) {
|
|
1062
1084
|
try {
|
|
1063
|
-
const
|
|
1064
|
-
return
|
|
1085
|
+
const l = r[e];
|
|
1086
|
+
return T(l) ? { success: !0, result: Object.fromEntries(
|
|
1065
1087
|
yield Promise.all(
|
|
1066
|
-
Object.entries(
|
|
1067
|
-
const
|
|
1088
|
+
Object.entries(l).map((d) => a(this, [d], function* ([h, m]) {
|
|
1089
|
+
const y = yield this.createUniqueSlug({
|
|
1068
1090
|
slug: m,
|
|
1069
1091
|
field: h,
|
|
1070
1092
|
isObject: !0,
|
|
1071
|
-
haveHistory:
|
|
1072
|
-
filter:
|
|
1093
|
+
haveHistory: u,
|
|
1094
|
+
filter: n
|
|
1073
1095
|
});
|
|
1074
|
-
return [h,
|
|
1096
|
+
return [h, y];
|
|
1075
1097
|
}))
|
|
1076
1098
|
)
|
|
1077
1099
|
) } : { success: !0, result: yield this.createUniqueSlug({
|
|
1078
|
-
slug:
|
|
1100
|
+
slug: l,
|
|
1079
1101
|
field: e,
|
|
1080
1102
|
isObject: !1,
|
|
1081
|
-
haveHistory:
|
|
1082
|
-
filter:
|
|
1103
|
+
haveHistory: u,
|
|
1104
|
+
filter: n
|
|
1083
1105
|
}) };
|
|
1084
|
-
} catch (
|
|
1085
|
-
return
|
|
1106
|
+
} catch (l) {
|
|
1107
|
+
return f(l);
|
|
1086
1108
|
}
|
|
1087
1109
|
});
|
|
1088
1110
|
}
|
|
@@ -1099,32 +1121,32 @@ class we {
|
|
|
1099
1121
|
* @returns A promise that resolves to a standardized response indicating whether the slug exists.
|
|
1100
1122
|
*/
|
|
1101
1123
|
checkSlug(t) {
|
|
1102
|
-
return
|
|
1124
|
+
return a(this, arguments, function* ({ slug: e, field: r, from: n, filter: u, haveHistory: l }) {
|
|
1103
1125
|
try {
|
|
1104
|
-
const
|
|
1105
|
-
if (
|
|
1106
|
-
const m = Object.values(
|
|
1126
|
+
const c = n[r];
|
|
1127
|
+
if (T(c)) {
|
|
1128
|
+
const m = Object.values(c).map((R) => I(R));
|
|
1107
1129
|
return (yield Promise.all(
|
|
1108
1130
|
m.map(
|
|
1109
|
-
(
|
|
1110
|
-
slug:
|
|
1131
|
+
(R) => this.model.exists(this.createSlugQuery({
|
|
1132
|
+
slug: R,
|
|
1111
1133
|
field: r,
|
|
1112
1134
|
isObject: !0,
|
|
1113
|
-
haveHistory:
|
|
1114
|
-
filter:
|
|
1135
|
+
haveHistory: l,
|
|
1136
|
+
filter: u
|
|
1115
1137
|
}))
|
|
1116
1138
|
)
|
|
1117
|
-
)).some((
|
|
1139
|
+
)).some((R) => R) ? { success: !0, result: !0 } : { success: !0, result: !1 };
|
|
1118
1140
|
}
|
|
1119
|
-
const
|
|
1141
|
+
const i = I(e);
|
|
1120
1142
|
return { success: !0, result: (yield this.model.exists(this.createSlugQuery({
|
|
1121
|
-
slug:
|
|
1143
|
+
slug: i,
|
|
1122
1144
|
field: r,
|
|
1123
1145
|
isObject: !1,
|
|
1124
|
-
filter:
|
|
1146
|
+
filter: u
|
|
1125
1147
|
}))) !== null };
|
|
1126
|
-
} catch (
|
|
1127
|
-
return
|
|
1148
|
+
} catch (c) {
|
|
1149
|
+
return f(c);
|
|
1128
1150
|
}
|
|
1129
1151
|
});
|
|
1130
1152
|
}
|
|
@@ -1135,11 +1157,11 @@ class we {
|
|
|
1135
1157
|
* @returns A promise that resolves to a standardized response with the aggregation results.
|
|
1136
1158
|
*/
|
|
1137
1159
|
aggregate(t) {
|
|
1138
|
-
return
|
|
1160
|
+
return a(this, null, function* () {
|
|
1139
1161
|
try {
|
|
1140
1162
|
return { success: !0, result: yield this.model.aggregate(t) };
|
|
1141
1163
|
} catch (e) {
|
|
1142
|
-
return
|
|
1164
|
+
return f(e);
|
|
1143
1165
|
}
|
|
1144
1166
|
});
|
|
1145
1167
|
}
|
|
@@ -1152,17 +1174,17 @@ class we {
|
|
|
1152
1174
|
* @returns A promise that resolves to a standardized response with the array of distinct values.
|
|
1153
1175
|
*/
|
|
1154
1176
|
distinct(t) {
|
|
1155
|
-
return
|
|
1177
|
+
return a(this, arguments, function* (e, r = {}, n = {}) {
|
|
1156
1178
|
try {
|
|
1157
|
-
return { success: !0, result: yield this.model.distinct(e, r,
|
|
1158
|
-
} catch (
|
|
1159
|
-
return
|
|
1179
|
+
return { success: !0, result: yield this.model.distinct(e, r, n) };
|
|
1180
|
+
} catch (u) {
|
|
1181
|
+
return f(u);
|
|
1160
1182
|
}
|
|
1161
1183
|
});
|
|
1162
1184
|
}
|
|
1163
1185
|
}
|
|
1164
1186
|
export {
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1187
|
+
Ve as MongoController,
|
|
1188
|
+
xe as MongooseController,
|
|
1189
|
+
x as mongo
|
|
1168
1190
|
};
|