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