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