@cyberskill/shared 2.20.0 → 2.25.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.
Files changed (63) hide show
  1. package/dist/config/eslint/index.cjs +1 -1
  2. package/dist/config/eslint/index.d.ts +2 -7
  3. package/dist/config/eslint/index.js +1 -1
  4. package/dist/config/storybook/storybook.main.cjs +1 -1
  5. package/dist/config/storybook/storybook.main.js +1 -1
  6. package/dist/config/vitest/vitest.e2e.cjs +1 -1
  7. package/dist/config/vitest/vitest.e2e.js +6 -6
  8. package/dist/config/vitest/vitest.unit.cjs +1 -1
  9. package/dist/config/vitest/vitest.unit.js +5 -5
  10. package/dist/config/vitest/vitest.unit.setup.d.ts +0 -1
  11. package/dist/node/cli/index.cjs +4 -4
  12. package/dist/node/cli/index.js +56 -51
  13. package/dist/node/command/command.util.cjs +2 -2
  14. package/dist/node/command/command.util.d.ts +4 -1
  15. package/dist/node/command/command.util.js +125 -112
  16. package/dist/node/express/express.util.cjs +1 -1
  17. package/dist/node/express/express.util.js +24 -23
  18. package/dist/node/mongo/index.cjs +1 -1
  19. package/dist/node/mongo/index.d.ts +1 -0
  20. package/dist/node/mongo/index.js +15 -12
  21. package/dist/node/mongo/mongo.controller.cjs +1 -0
  22. package/dist/node/mongo/mongo.controller.d.ts +317 -0
  23. package/dist/node/mongo/mongo.controller.js +956 -0
  24. package/dist/node/mongo/mongo.controller.test.unit.d.ts +1 -0
  25. package/dist/node/mongo/mongo.type.d.ts +9 -10
  26. package/dist/node/mongo/mongo.util.cjs +5 -5
  27. package/dist/node/mongo/mongo.util.d.ts +32 -515
  28. package/dist/node/mongo/mongo.util.js +314 -1239
  29. package/dist/node/mongo/mongo.util.test.unit.d.ts +1 -0
  30. package/dist/node/path/path.constant.cjs +1 -1
  31. package/dist/node/path/path.constant.d.ts +2 -2
  32. package/dist/node/path/path.constant.js +120 -116
  33. package/dist/node/path/path.test.unit.d.ts +1 -0
  34. package/dist/node/path/path.util.d.ts +2 -2
  35. package/dist/react/loading/loading.component.cjs +2 -2
  36. package/dist/react/loading/loading.component.js +32 -19
  37. package/dist/react/loading/loading.test.unit.d.ts +1 -0
  38. package/dist/react/loading/loading.type.d.ts +2 -1
  39. package/dist/util/common/common.test.unit.d.ts +1 -0
  40. package/dist/util/common/common.util.cjs +1 -1
  41. package/dist/util/common/common.util.d.ts +3 -0
  42. package/dist/util/common/common.util.js +35 -35
  43. package/dist/util/index.cjs +1 -1
  44. package/dist/util/index.js +16 -15
  45. package/dist/util/object/index.cjs +1 -1
  46. package/dist/util/object/index.js +4 -3
  47. package/dist/util/object/object.test.unit.d.ts +1 -0
  48. package/dist/util/object/object.util.cjs +1 -1
  49. package/dist/util/object/object.util.d.ts +10 -30
  50. package/dist/util/object/object.util.js +110 -73
  51. package/dist/util/serializer/serializer.test.unit.d.ts +1 -0
  52. package/dist/util/serializer/serializer.util.cjs +1 -1
  53. package/dist/util/serializer/serializer.util.js +19 -16
  54. package/dist/util/string/string.test.unit.d.ts +1 -0
  55. package/dist/util/string/string.util.cjs +1 -1
  56. package/dist/util/string/string.util.d.ts +4 -3
  57. package/dist/util/string/string.util.js +32 -41
  58. package/dist/util/validate/validate.test.unit.d.ts +1 -0
  59. package/dist/util/validate/validate.util.cjs +1 -1
  60. package/dist/util/validate/validate.util.js +9 -9
  61. package/package.json +44 -51
  62. /package/dist/node_modules/.pnpm/{vitest@4.0.13_@types_debug@4.1.12_@types_node@24.10.1_jiti@2.6.1_jsdom@27.2.0_sass@1.94.2_tsx@4.20.6_yaml@2.8.1 → vitest@4.0.16_@types_node@25.0.3_jiti@2.6.1_jsdom@27.4.0_sass@1.97.2_tsx@4.21.0_yaml@2.8.2}/node_modules/vitest/dist/config.cjs +0 -0
  63. /package/dist/node_modules/.pnpm/{vitest@4.0.13_@types_debug@4.1.12_@types_node@24.10.1_jiti@2.6.1_jsdom@27.2.0_sass@1.94.2_tsx@4.20.6_yaml@2.8.1 → vitest@4.0.16_@types_node@25.0.3_jiti@2.6.1_jsdom@27.4.0_sass@1.97.2_tsx@4.21.0_yaml@2.8.2}/node_modules/vitest/dist/config.js +0 -0
@@ -0,0 +1,956 @@
1
+ import { MONGO_SLUG_MAX_ATTEMPTS as U } from "./mongo.constant.js";
2
+ import { mongo as T, applyNestedPopulate as W, convertEnumToModelName as B } from "./mongo.util.js";
3
+ import { normalizeMongoFilter as M, deepClone as G } from "../../util/object/object.util.js";
4
+ import { generateShortId as K, generateSlug as C } from "../../util/string/string.util.js";
5
+ import { RESPONSE_STATUS as j } from "../../constant/response-status.js";
6
+ import { catchError as m } from "../log/log.util.js";
7
+ var Q = Object.defineProperty, k = Object.defineProperties, X = Object.getOwnPropertyDescriptors, I = Object.getOwnPropertySymbols, J = Object.prototype.hasOwnProperty, Y = Object.prototype.propertyIsEnumerable, w = (l, r, e) => r in l ? Q(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[r] = e, R = (l, r) => {
8
+ for (var e in r || (r = {}))
9
+ J.call(r, e) && w(l, e, r[e]);
10
+ if (I)
11
+ for (var e of I(r))
12
+ Y.call(r, e) && w(l, e, r[e]);
13
+ return l;
14
+ }, A = (l, r) => k(l, X(r)), Z = (l, r, e) => w(l, typeof r != "symbol" ? r + "" : r, e), f = (l, r, e) => new Promise((t, s) => {
15
+ var n = (a) => {
16
+ try {
17
+ o(e.next(a));
18
+ } catch (c) {
19
+ s(c);
20
+ }
21
+ }, u = (a) => {
22
+ try {
23
+ o(e.throw(a));
24
+ } catch (c) {
25
+ s(c);
26
+ }
27
+ }, o = (a) => a.done ? t(a.value) : Promise.resolve(a.value).then(n, u);
28
+ o((e = e.apply(l, r)).next());
29
+ });
30
+ function L(l) {
31
+ return l != null && typeof l == "object";
32
+ }
33
+ function $(l, r) {
34
+ if (!l || !r || r.length === 0)
35
+ return l;
36
+ const e = new Set(r.map((t) => t.name));
37
+ if (Array.isArray(l)) {
38
+ const t = l.filter((s) => {
39
+ if (typeof s == "string")
40
+ return !Array.from(e).some(
41
+ (n) => s === n || s.startsWith(`${n}.`)
42
+ );
43
+ if (typeof s == "object" && s !== null) {
44
+ const n = s, u = n.path || n.populate || "";
45
+ return !Array.from(e).some(
46
+ (o) => u === o || u.startsWith(`${o}.`)
47
+ );
48
+ }
49
+ return !0;
50
+ });
51
+ return t.length > 0 ? t : void 0;
52
+ }
53
+ if (typeof l == "string")
54
+ return Array.from(e).some(
55
+ (t) => l === t || l.startsWith(`${t}.`)
56
+ ) ? void 0 : l;
57
+ if (typeof l == "object" && l !== null) {
58
+ const t = l, s = t.path || t.populate || "";
59
+ return Array.from(e).some(
60
+ (n) => s === n || s.startsWith(`${n}.`)
61
+ ) ? void 0 : l;
62
+ }
63
+ return l;
64
+ }
65
+ function H(l, r, e) {
66
+ if (!l.length || !r || !(e != null && e.ref))
67
+ return [];
68
+ const t = /* @__PURE__ */ new Map();
69
+ return l.forEach((s) => {
70
+ try {
71
+ const n = e.ref(s);
72
+ if (n == null)
73
+ return;
74
+ const u = typeof n == "string" ? n : String(n);
75
+ if (u && u.trim() !== "") {
76
+ const o = B(u);
77
+ t.has(o) || t.set(o, []), t.get(o).push(s);
78
+ }
79
+ } catch (n) {
80
+ m(new Error(`Dynamic ref function failed for virtual "${r}": ${n instanceof Error ? n.message : String(n)}`));
81
+ }
82
+ }), Array.from(t.entries()).map(([s, n]) => ({ model: s, docs: n }));
83
+ }
84
+ function ee(l) {
85
+ return l !== null && typeof l == "object" && "toObject" in l && typeof l.toObject == "function";
86
+ }
87
+ function z(l, r, e, t, s, n) {
88
+ return f(this, null, function* () {
89
+ if (!r.length || !e.length || !t)
90
+ return r;
91
+ const u = e.filter((c) => {
92
+ if (Array.isArray(t))
93
+ return t.length > 0 && t.some((d) => {
94
+ if (typeof d == "string")
95
+ return d === c.name || d.startsWith(`${c.name}.`);
96
+ if (d && typeof d == "object") {
97
+ const i = d, p = i.path || i.populate || "";
98
+ return p === c.name || p.startsWith(`${c.name}.`);
99
+ }
100
+ return !1;
101
+ });
102
+ if (typeof t == "string")
103
+ return t === c.name || t.startsWith(`${c.name}.`);
104
+ if (typeof t == "object" && t !== null) {
105
+ const d = t, i = d.path || d.populate || "";
106
+ return i === c.name || i.startsWith(`${c.name}.`);
107
+ }
108
+ return !1;
109
+ });
110
+ if (u.length === 0)
111
+ return r;
112
+ const o = G(r.map((c) => ee(c) ? c.toObject() : c));
113
+ o.forEach((c) => {
114
+ u.forEach(({ name: d, options: i }) => {
115
+ d in c || (c[d] = i.count ? 0 : i.justOne ? null : []);
116
+ });
117
+ });
118
+ const a = /* @__PURE__ */ new Map();
119
+ for (const c of u) {
120
+ const { name: d, options: i } = c, p = H(o, d, i);
121
+ for (const y of p) {
122
+ a.has(y.model) || a.set(y.model, {
123
+ virtuals: [],
124
+ localValueSets: /* @__PURE__ */ new Map(),
125
+ docsByLocalValue: /* @__PURE__ */ new Map()
126
+ });
127
+ const O = a.get(y.model);
128
+ O.virtuals.find((_) => _.name === d) || (O.virtuals.push(c), O.localValueSets.set(d, /* @__PURE__ */ new Set()));
129
+ const N = O.localValueSets.get(d);
130
+ y.docs.forEach((_) => {
131
+ const v = _[i.localField];
132
+ if (v != null) {
133
+ const g = String(v);
134
+ N.add(g);
135
+ let h = -1;
136
+ const S = _;
137
+ S.id !== void 0 ? h = o.findIndex((F) => F.id === S.id) : S._id !== void 0 && (h = o.findIndex((F) => {
138
+ var b, E, V, D;
139
+ return ((E = (b = F._id) == null ? void 0 : b.toString) == null ? void 0 : E.call(b)) === ((D = (V = S._id) == null ? void 0 : V.toString) == null ? void 0 : D.call(V));
140
+ })), h !== -1 && (O.docsByLocalValue.has(g) || O.docsByLocalValue.set(g, []), O.docsByLocalValue.get(g).push(h));
141
+ }
142
+ });
143
+ }
144
+ }
145
+ if (yield Promise.all(Array.from(a.entries()).map((c) => f(null, [c], function* ([d, i]) {
146
+ const p = l.models[d];
147
+ if (!p)
148
+ return;
149
+ const y = /* @__PURE__ */ new Set();
150
+ if (i.localValueSets.forEach((g) => {
151
+ g.forEach((h) => y.add(h));
152
+ }), y.size === 0)
153
+ return;
154
+ const O = [...new Set(i.virtuals.map((g) => g.options.foreignField))], N = Array.from(y);
155
+ let _;
156
+ O.length === 1 ? _ = { [String(O[0])]: { $in: N } } : _ = { $or: O.map((g) => ({ [g]: { $in: N } })) };
157
+ const v = yield p.find(_, s).lean();
158
+ for (const g of i.virtuals) {
159
+ const { name: h, options: S } = g, F = v.filter((b) => {
160
+ const E = b[S.foreignField];
161
+ return E != null && y.has(String(E));
162
+ });
163
+ if (S.count) {
164
+ const b = /* @__PURE__ */ new Map();
165
+ F.forEach((E) => {
166
+ var V;
167
+ const D = (V = E[S.foreignField]) == null ? void 0 : V.toString();
168
+ D && b.set(D, (b.get(D) || 0) + 1);
169
+ }), i.localValueSets.get(h).forEach((E) => {
170
+ const V = i.docsByLocalValue.get(E) || [], D = b.get(E) || 0;
171
+ V.forEach((P) => {
172
+ const x = o[P];
173
+ x[h] === void 0 && (x[h] = D);
174
+ });
175
+ });
176
+ } else {
177
+ const b = /* @__PURE__ */ new Map();
178
+ F.forEach((E) => {
179
+ var V;
180
+ const D = (V = E[S.foreignField]) == null ? void 0 : V.toString();
181
+ D && (b.has(D) || b.set(D, []), b.get(D).push(E));
182
+ }), i.localValueSets.get(h).forEach((E) => {
183
+ const V = i.docsByLocalValue.get(E) || [], D = b.get(E) || [], P = S.justOne ? D[0] || null : D;
184
+ V.forEach((x) => {
185
+ const q = o[x];
186
+ q[h] = P;
187
+ });
188
+ });
189
+ }
190
+ }
191
+ }))), t) {
192
+ const d = ((i) => {
193
+ const p = Array.isArray(i) ? i : [i], y = /* @__PURE__ */ new Map(), O = [];
194
+ for (const _ of p)
195
+ if (typeof _ == "string")
196
+ if (_.includes(".")) {
197
+ const v = _.split("."), g = v[0] || "", h = v.slice(1).join(".");
198
+ g && (y.has(g) || y.set(g, []), h && y.get(g).push(h));
199
+ } else
200
+ O.push(_);
201
+ else if (_ && typeof _ == "object") {
202
+ const v = _;
203
+ if (v.path && v.path.includes(".")) {
204
+ const g = v.path.split("."), h = g[0] || "", S = g.slice(1).join(".");
205
+ h && (y.has(h) || y.set(h, []), S && y.get(h).push(S), v.populate && y.get(h).push(v.populate));
206
+ } else
207
+ O.push(_);
208
+ }
209
+ const N = [...O];
210
+ return y.forEach((_, v) => {
211
+ const g = [];
212
+ for (const h of _)
213
+ (typeof h == "string" || h && typeof h == "object") && g.push(h);
214
+ g.length > 0 ? N.push({ path: v, populate: g }) : N.push(v);
215
+ }), N;
216
+ })(t);
217
+ yield W(l, o, d, e, n);
218
+ }
219
+ return o;
220
+ });
221
+ }
222
+ class le {
223
+ /**
224
+ * Creates a new MongoDB controller instance.
225
+ *
226
+ * @param db - The MongoDB database instance.
227
+ * @param collectionName - The name of the collection to operate on.
228
+ */
229
+ constructor(r, e) {
230
+ Z(this, "collection"), this.collection = r.collection(e);
231
+ }
232
+ /**
233
+ * Creates a single document in the collection.
234
+ * This method adds generic fields (id, isDel, timestamps) to the document before insertion.
235
+ *
236
+ * @param document - The document to create, with or without generic fields.
237
+ * @returns A promise that resolves to a standardized response with the created document.
238
+ */
239
+ createOne(r) {
240
+ return f(this, null, function* () {
241
+ try {
242
+ const e = R(R({}, T.createGenericFields()), r);
243
+ return (yield this.collection.insertOne(e)).acknowledged ? {
244
+ success: !0,
245
+ message: "Document created successfully",
246
+ result: e
247
+ } : {
248
+ success: !1,
249
+ message: "Document creation failed",
250
+ code: j.INTERNAL_SERVER_ERROR.CODE
251
+ };
252
+ } catch (e) {
253
+ return m(e);
254
+ }
255
+ });
256
+ }
257
+ /**
258
+ * Creates multiple documents in the collection.
259
+ * This method adds generic fields to each document before bulk insertion.
260
+ *
261
+ * @param documents - An array of documents to create.
262
+ * @returns A promise that resolves to a standardized response with the created documents.
263
+ */
264
+ createMany(r) {
265
+ return f(this, null, function* () {
266
+ try {
267
+ const e = r.map((s) => R(R({}, T.createGenericFields()), s)), t = yield this.collection.insertMany(e);
268
+ return t.insertedCount === 0 ? {
269
+ success: !1,
270
+ message: "No documents were inserted",
271
+ code: j.INTERNAL_SERVER_ERROR.CODE
272
+ } : {
273
+ success: !0,
274
+ message: `${t.insertedCount} documents created successfully`,
275
+ result: e
276
+ };
277
+ } catch (e) {
278
+ return m(e);
279
+ }
280
+ });
281
+ }
282
+ /**
283
+ * Finds a single document by filter criteria.
284
+ *
285
+ * @param filter - The filter criteria to find the document.
286
+ * @returns A promise that resolves to a standardized response with the found document.
287
+ */
288
+ findOne(r) {
289
+ return f(this, null, function* () {
290
+ try {
291
+ const e = yield this.collection.findOne(r);
292
+ return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code: j.NOT_FOUND.CODE };
293
+ } catch (e) {
294
+ return m(e);
295
+ }
296
+ });
297
+ }
298
+ /**
299
+ * Finds all documents matching the filter criteria.
300
+ *
301
+ * @param filter - The filter criteria to find documents (defaults to empty object for all documents).
302
+ * @returns A promise that resolves to a standardized response with the found documents.
303
+ */
304
+ findAll() {
305
+ return f(this, arguments, function* (r = {}) {
306
+ try {
307
+ return {
308
+ success: !0,
309
+ message: "Documents retrieved successfully",
310
+ result: yield this.collection.find(r).toArray()
311
+ };
312
+ } catch (e) {
313
+ return m(e);
314
+ }
315
+ });
316
+ }
317
+ /**
318
+ * Counts documents matching the filter criteria.
319
+ *
320
+ * @param filter - The filter criteria to count documents (defaults to empty object for all documents).
321
+ * @returns A promise that resolves to a standardized response with the document count.
322
+ */
323
+ count() {
324
+ return f(this, arguments, function* (r = {}) {
325
+ try {
326
+ const e = yield this.collection.countDocuments(r);
327
+ return {
328
+ success: !0,
329
+ message: `${e} documents counted successfully`,
330
+ result: e
331
+ };
332
+ } catch (e) {
333
+ return m(e);
334
+ }
335
+ });
336
+ }
337
+ /**
338
+ * Updates a single document matching the filter criteria.
339
+ *
340
+ * @param filter - The filter criteria to find the document to update.
341
+ * @param update - The update data to apply to the document.
342
+ * @returns A promise that resolves to a standardized response with the update result.
343
+ */
344
+ updateOne(r, e) {
345
+ return f(this, null, function* () {
346
+ try {
347
+ const t = yield this.collection.updateOne(r, {
348
+ $set: e
349
+ });
350
+ return t.matchedCount === 0 ? {
351
+ success: !1,
352
+ message: "No documents matched the filter",
353
+ code: j.INTERNAL_SERVER_ERROR.CODE
354
+ } : {
355
+ success: !0,
356
+ message: "Document updated successfully",
357
+ result: t
358
+ };
359
+ } catch (t) {
360
+ return m(t);
361
+ }
362
+ });
363
+ }
364
+ /**
365
+ * Updates multiple documents matching the filter criteria.
366
+ *
367
+ * @param filter - The filter criteria to find documents to update.
368
+ * @param update - The update data to apply to the documents.
369
+ * @returns A promise that resolves to a standardized response with the update result.
370
+ */
371
+ updateMany(r, e) {
372
+ return f(this, null, function* () {
373
+ try {
374
+ const t = yield this.collection.updateMany(r, {
375
+ $set: e
376
+ });
377
+ return t.matchedCount === 0 ? {
378
+ success: !1,
379
+ message: "No documents matched the filter",
380
+ code: j.INTERNAL_SERVER_ERROR.CODE
381
+ } : {
382
+ success: !0,
383
+ message: "Documents updated successfully",
384
+ result: t
385
+ };
386
+ } catch (t) {
387
+ return m(t);
388
+ }
389
+ });
390
+ }
391
+ /**
392
+ * Deletes a single document matching the filter criteria.
393
+ *
394
+ * @param filter - The filter criteria to find the document to delete.
395
+ * @returns A promise that resolves to a standardized response with the delete result.
396
+ */
397
+ deleteOne(r) {
398
+ return f(this, null, function* () {
399
+ try {
400
+ const e = yield this.collection.deleteOne(r);
401
+ return e.deletedCount === 0 ? {
402
+ success: !1,
403
+ message: "No documents matched the filter",
404
+ code: j.INTERNAL_SERVER_ERROR.CODE
405
+ } : {
406
+ success: !0,
407
+ message: "Document deleted successfully",
408
+ result: e
409
+ };
410
+ } catch (e) {
411
+ return m(e);
412
+ }
413
+ });
414
+ }
415
+ /**
416
+ * Deletes multiple documents matching the filter criteria.
417
+ *
418
+ * @param filter - The filter criteria to find documents to delete.
419
+ * @returns A promise that resolves to a standardized response with the delete result.
420
+ */
421
+ deleteMany(r) {
422
+ return f(this, null, function* () {
423
+ try {
424
+ const e = yield this.collection.deleteMany(r);
425
+ return e.deletedCount === 0 ? {
426
+ success: !1,
427
+ message: "No documents matched the filter",
428
+ code: j.INTERNAL_SERVER_ERROR.CODE
429
+ } : {
430
+ success: !0,
431
+ message: "Documents deleted successfully",
432
+ result: e
433
+ };
434
+ } catch (e) {
435
+ return m(e);
436
+ }
437
+ });
438
+ }
439
+ }
440
+ class ce {
441
+ /**
442
+ * Creates a new Mongoose controller instance.
443
+ *
444
+ * @param model - The Mongoose model to operate on.
445
+ */
446
+ constructor(r) {
447
+ this.model = r;
448
+ }
449
+ /**
450
+ * Gets the model name for logging and error messages.
451
+ *
452
+ * @returns The name of the model.
453
+ */
454
+ getModelName() {
455
+ return this.model.modelName;
456
+ }
457
+ /**
458
+ * Gets the dynamic virtuals configuration from the model instance.
459
+ *
460
+ * @returns Array of dynamic virtual configurations or undefined if none exist.
461
+ */
462
+ getDynamicVirtuals() {
463
+ if (this.model._virtualConfigs) {
464
+ const t = this.model._virtualConfigs.filter((s) => {
465
+ var n;
466
+ return typeof ((n = s.options) == null ? void 0 : n.ref) == "function";
467
+ });
468
+ if (t.length > 0)
469
+ return t;
470
+ }
471
+ return this.model.schema.statics._dynamicVirtuals;
472
+ }
473
+ /**
474
+ * Populates dynamic virtuals for a single document.
475
+ *
476
+ * @param result - The document to populate dynamic virtuals for.
477
+ * @param populate - The populate options to determine which virtuals to populate.
478
+ * @returns The document with dynamic virtuals populated.
479
+ */
480
+ populateDynamicVirtualsForDocument(r, e) {
481
+ return f(this, null, function* () {
482
+ const t = this.getDynamicVirtuals();
483
+ if (t && t.length > 0) {
484
+ const s = yield z(this.model.base, [r], t, e, void 0, this.model);
485
+ return s && s[0] ? s[0] : r;
486
+ }
487
+ return r;
488
+ });
489
+ }
490
+ /**
491
+ * Populates dynamic virtuals for an array of documents.
492
+ *
493
+ * @param results - The documents to populate dynamic virtuals for.
494
+ * @param populate - The populate options to determine which virtuals to populate.
495
+ * @returns The documents with dynamic virtuals populated.
496
+ */
497
+ populateDynamicVirtualsForDocuments(r, e) {
498
+ return f(this, null, function* () {
499
+ const t = this.getDynamicVirtuals();
500
+ return t && t.length > 0 && r.length > 0 ? yield z(this.model.base, r, t, e, void 0, this.model) : r;
501
+ });
502
+ }
503
+ /**
504
+ * Finds a single document with optional population and projection.
505
+ * Automatically handles dynamic virtual population if configured.
506
+ *
507
+ * @param filter - The filter criteria to find the document.
508
+ * @param projection - The fields to include/exclude in the result.
509
+ * @param options - Query options for the operation.
510
+ * @param populate - Population configuration for related documents.
511
+ * @returns A promise that resolves to a standardized response with the found document.
512
+ */
513
+ findOne() {
514
+ return f(this, arguments, function* (r = {}, e = {}, t = {}, s) {
515
+ var n, u;
516
+ try {
517
+ const o = M(r), a = this.model.findOne(o, e, t), c = this.getDynamicVirtuals(), d = $(s, c);
518
+ d && a.populate(d);
519
+ const i = yield a.exec();
520
+ if (!i)
521
+ return {
522
+ success: !1,
523
+ message: `No ${this.getModelName()} found.`,
524
+ code: j.NOT_FOUND.CODE
525
+ };
526
+ const p = yield this.populateDynamicVirtualsForDocument(i, s);
527
+ return { success: !0, result: (u = (n = p == null ? void 0 : p.toObject) == null ? void 0 : n.call(p)) != null ? u : p };
528
+ } catch (o) {
529
+ return m(o);
530
+ }
531
+ });
532
+ }
533
+ /**
534
+ * Finds all documents with optional population and projection.
535
+ * Automatically handles dynamic virtual population if configured.
536
+ *
537
+ * @param filter - The filter criteria to find documents.
538
+ * @param projection - The fields to include/exclude in the result.
539
+ * @param options - Query options for the operation.
540
+ * @param populate - Population configuration for related documents.
541
+ * @returns A promise that resolves to a standardized response with the found documents.
542
+ */
543
+ findAll() {
544
+ return f(this, arguments, function* (r = {}, e = {}, t = {}, s) {
545
+ try {
546
+ const n = M(r), u = this.model.find(n, e, t), o = this.getDynamicVirtuals(), a = $(s, o);
547
+ a && u.populate(a);
548
+ const c = yield u.exec();
549
+ return { success: !0, result: (yield this.populateDynamicVirtualsForDocuments(c, s)).map((i) => {
550
+ var p, y;
551
+ return (y = (p = i == null ? void 0 : i.toObject) == null ? void 0 : p.call(i)) != null ? y : i;
552
+ }) };
553
+ } catch (n) {
554
+ return m(n);
555
+ }
556
+ });
557
+ }
558
+ /**
559
+ * Finds documents with pagination support.
560
+ * Automatically handles dynamic virtual population if configured.
561
+ *
562
+ * @param filter - The filter criteria to find documents.
563
+ * @param options - Pagination options including page, limit, and population.
564
+ * @returns A promise that resolves to a standardized response with paginated results.
565
+ */
566
+ findPaging() {
567
+ return f(this, arguments, function* (r = {}, e = {}) {
568
+ try {
569
+ const t = M(r), s = this.getDynamicVirtuals(), n = R({}, e);
570
+ e.populate && (n.populate = $(e.populate, s));
571
+ const u = yield this.model.paginate(t, n);
572
+ if (s && s.length > 0) {
573
+ const o = yield this.populateDynamicVirtualsForDocuments(u.docs, e.populate);
574
+ return { success: !0, result: A(R({}, u), { docs: o.map((a) => {
575
+ var c, d;
576
+ return (d = (c = a == null ? void 0 : a.toObject) == null ? void 0 : c.call(a)) != null ? d : a;
577
+ }) }) };
578
+ }
579
+ return { success: !0, result: A(R({}, u), { docs: u.docs.map((o) => {
580
+ var a, c;
581
+ return (c = (a = o == null ? void 0 : o.toObject) == null ? void 0 : a.call(o)) != null ? c : o;
582
+ }) }) };
583
+ } catch (t) {
584
+ return m(t);
585
+ }
586
+ });
587
+ }
588
+ /**
589
+ * Performs aggregation with pagination support.
590
+ *
591
+ * @param pipeline - The aggregation pipeline stages.
592
+ * @param options - Pagination options for the aggregation result.
593
+ * @returns A promise that resolves to a standardized response with paginated aggregation results.
594
+ */
595
+ findPagingAggregate(r) {
596
+ return f(this, arguments, function* (e, t = {}) {
597
+ try {
598
+ const s = this.getDynamicVirtuals(), n = R({}, t);
599
+ t.populate && (n.populate = $(t.populate, s));
600
+ const u = yield this.model.aggregatePaginate(
601
+ this.model.aggregate(e),
602
+ n
603
+ ), o = yield this.populateDynamicVirtualsForDocuments(u.docs, t.populate);
604
+ return { success: !0, result: A(R({}, u), { docs: o.map((a) => {
605
+ var c, d;
606
+ return (d = (c = a == null ? void 0 : a.toObject) == null ? void 0 : c.call(a)) != null ? d : a;
607
+ }) }) };
608
+ } catch (s) {
609
+ return m(s);
610
+ }
611
+ });
612
+ }
613
+ /**
614
+ * Counts documents matching the filter criteria.
615
+ *
616
+ * @param filter - The filter criteria to count documents.
617
+ * @returns A promise that resolves to a standardized response with the document count.
618
+ */
619
+ count() {
620
+ return f(this, arguments, function* (r = {}) {
621
+ try {
622
+ const e = M(r);
623
+ return { success: !0, result: yield this.model.countDocuments(e) };
624
+ } catch (e) {
625
+ return m(e);
626
+ }
627
+ });
628
+ }
629
+ /**
630
+ * Creates a single document.
631
+ *
632
+ * @param doc - The document to create.
633
+ * @returns A promise that resolves to a standardized response with the created document.
634
+ */
635
+ createOne(r) {
636
+ return f(this, null, function* () {
637
+ var e, t;
638
+ try {
639
+ const s = yield this.model.create(r);
640
+ return { success: !0, result: (t = (e = s == null ? void 0 : s.toObject) == null ? void 0 : e.call(s)) != null ? t : s };
641
+ } catch (s) {
642
+ return m(s);
643
+ }
644
+ });
645
+ }
646
+ /**
647
+ * Creates multiple documents with bulk insertion.
648
+ *
649
+ * @param docs - An array of documents to create.
650
+ * @param options - Options for the bulk insertion operation.
651
+ * @returns A promise that resolves to a standardized response with the created documents.
652
+ */
653
+ createMany(r) {
654
+ return f(this, arguments, function* (e, t = {}) {
655
+ try {
656
+ return { success: !0, result: (yield this.model.insertMany(e, t)).map((n) => {
657
+ var u, o;
658
+ return (o = (u = n == null ? void 0 : n.toObject) == null ? void 0 : u.call(n)) != null ? o : n;
659
+ }) };
660
+ } catch (s) {
661
+ return m(s);
662
+ }
663
+ });
664
+ }
665
+ /**
666
+ * Updates a single document and returns the updated version.
667
+ *
668
+ * @param filter - The filter criteria to find the document to update.
669
+ * @param update - The update data to apply.
670
+ * @param options - Options for the update operation.
671
+ * @returns A promise that resolves to a standardized response with the updated document.
672
+ */
673
+ updateOne() {
674
+ return f(this, arguments, function* (r = {}, e = {}, t = {}) {
675
+ var s, n;
676
+ try {
677
+ const u = M(r), o = yield this.model.findOneAndUpdate(u, e, R({
678
+ new: !0
679
+ }, t)).exec();
680
+ return o ? { success: !0, result: (n = (s = o == null ? void 0 : o.toObject) == null ? void 0 : s.call(o)) != null ? n : o } : {
681
+ success: !1,
682
+ message: `Failed to update ${this.getModelName()}.`,
683
+ code: j.NOT_FOUND.CODE
684
+ };
685
+ } catch (u) {
686
+ return m(u);
687
+ }
688
+ });
689
+ }
690
+ /**
691
+ * Updates multiple documents matching the filter criteria.
692
+ *
693
+ * @param filter - The filter criteria to find documents to update.
694
+ * @param update - The update data to apply.
695
+ * @param options - Options for the update operation.
696
+ * @returns A promise that resolves to a standardized response with the update result.
697
+ */
698
+ updateMany() {
699
+ return f(this, arguments, function* (r = {}, e = {}, t = {}) {
700
+ try {
701
+ const s = M(r);
702
+ return { success: !0, result: yield this.model.updateMany(s, e, t).exec() };
703
+ } catch (s) {
704
+ return m(s);
705
+ }
706
+ });
707
+ }
708
+ /**
709
+ * Deletes a single document and returns the deleted version.
710
+ *
711
+ * @param filter - The filter criteria to find the document to delete.
712
+ * @param options - Options for the delete operation.
713
+ * @returns A promise that resolves to a standardized response with the deleted document.
714
+ */
715
+ deleteOne() {
716
+ return f(this, arguments, function* (r = {}, e = {}) {
717
+ var t, s;
718
+ try {
719
+ const n = M(r), u = yield this.model.findOneAndDelete(n, e).exec();
720
+ return u ? { success: !0, result: (s = (t = u == null ? void 0 : u.toObject) == null ? void 0 : t.call(u)) != null ? s : u } : {
721
+ success: !1,
722
+ message: `No ${this.getModelName()} found to delete.`,
723
+ code: j.NOT_FOUND.CODE
724
+ };
725
+ } catch (n) {
726
+ return m(n);
727
+ }
728
+ });
729
+ }
730
+ /**
731
+ * Deletes multiple documents matching the filter criteria.
732
+ *
733
+ * @param filter - The filter criteria to find documents to delete.
734
+ * @param options - Options for the delete operation.
735
+ * @returns A promise that resolves to a standardized response with the delete result.
736
+ */
737
+ deleteMany() {
738
+ return f(this, arguments, function* (r = {}, e = {}) {
739
+ try {
740
+ const t = M(r), s = yield this.model.deleteMany(t, e).exec();
741
+ return s.deletedCount === 0 ? {
742
+ success: !1,
743
+ message: "No documents found to delete.",
744
+ code: j.NOT_FOUND.CODE
745
+ } : { success: !0, result: s };
746
+ } catch (t) {
747
+ return m(t);
748
+ }
749
+ });
750
+ }
751
+ /**
752
+ * Creates a unique short ID based on a given ID.
753
+ * This method generates multiple short IDs with increasing lengths and finds the first available one.
754
+ *
755
+ * @param id - The base ID to generate short IDs from.
756
+ * @param length - The initial length for short ID generation (default: 4).
757
+ * @returns A promise that resolves to a standardized response with the unique short ID.
758
+ */
759
+ createShortId(r, e = 4) {
760
+ return f(this, null, function* () {
761
+ try {
762
+ const s = Array.from({ length: 10 }, (o, a) => K(r, a + e)), u = (yield Promise.all(
763
+ s.map((o) => this.model.exists({ shortId: o }))
764
+ )).findIndex((o) => !o);
765
+ if (u !== -1) {
766
+ const o = s[u];
767
+ if (o)
768
+ return { success: !0, result: o };
769
+ }
770
+ return {
771
+ success: !1,
772
+ message: "Failed to create a unique shortId",
773
+ code: j.INTERNAL_SERVER_ERROR.CODE
774
+ };
775
+ } catch (t) {
776
+ return m(t);
777
+ }
778
+ });
779
+ }
780
+ /**
781
+ * Creates a query for slug existence checking.
782
+ * This method generates a query that checks for slug existence in both current and historical slug fields.
783
+ *
784
+ * @param options - Configuration for slug query generation including slug, field, and filter.
785
+ * @param options.slug - The slug string to check for existence.
786
+ * @param options.field - The field name for object-based slug checking.
787
+ * @param options.isObject - Whether the slug is stored as an object with nested fields.
788
+ * @param options.haveHistory - Whether to check historical slug fields for existence.
789
+ * @param options.filter - Additional filter conditions to apply to the query.
790
+ * @returns A MongoDB query object for checking slug existence.
791
+ */
792
+ createSlugQuery({ slug: r, field: e, isObject: t, haveHistory: s = !1, filter: n }) {
793
+ const u = R({}, n != null ? n : {});
794
+ return t ? A(R({}, u), {
795
+ $or: [
796
+ { [`slug.${e}`]: r },
797
+ ...s ? [{ slugHistory: { $elemMatch: { [`slug.${e}`]: r } } }] : []
798
+ ]
799
+ }) : A(R({}, u), {
800
+ $or: [
801
+ { slug: r },
802
+ ...s ? [{ slugHistory: r }] : []
803
+ ]
804
+ });
805
+ }
806
+ /**
807
+ * Creates a unique slug based on a given string.
808
+ * This method generates multiple slug variations and finds the first available one.
809
+ *
810
+ * @param options - Configuration for slug generation including slug, field, and filter.
811
+ * @param options.slug - The base slug string to make unique.
812
+ * @param options.field - The field name for object-based slug checking.
813
+ * @param options.isObject - Whether the slug is stored as an object with nested fields.
814
+ * @param options.haveHistory - Whether to check historical slug fields for uniqueness.
815
+ * @param options.filter - Additional filter conditions to apply when checking slug existence.
816
+ * @returns A promise that resolves to a unique slug string.
817
+ */
818
+ createUniqueSlug(r) {
819
+ return f(this, arguments, function* ({ slug: e, field: t, isObject: s, haveHistory: n, filter: u }) {
820
+ if (!e || typeof e != "string")
821
+ throw new Error("Invalid slug provided: must be a non-empty string");
822
+ const o = C(e);
823
+ if (!(yield this.model.exists(
824
+ this.createSlugQuery({ slug: o, field: t, isObject: s, haveHistory: n, filter: u })
825
+ )))
826
+ return o;
827
+ for (let i = 1; i <= U; i++) {
828
+ const p = `${o}-${i}`;
829
+ if (!(yield this.model.exists(
830
+ this.createSlugQuery({ slug: p, field: t, isObject: s, haveHistory: n, filter: u })
831
+ )))
832
+ return p;
833
+ }
834
+ const c = Date.now(), d = Math.random().toString(36).substring(2, 8);
835
+ return `${o}-${c}-${d}`;
836
+ });
837
+ }
838
+ /**
839
+ * Creates a slug for a document field.
840
+ * This method handles both simple string fields and object fields with nested slug generation.
841
+ *
842
+ * @param options - Configuration for slug creation including field, source document, and filter.
843
+ * @param options.field - The field name to create a slug for.
844
+ * @param options.from - The source document containing the field value.
845
+ * @param options.haveHistory - Whether to check historical slug fields for uniqueness.
846
+ * @param options.filter - Additional filter conditions to apply when checking slug existence.
847
+ * @returns A promise that resolves to a standardized response with the created slug(s).
848
+ */
849
+ createSlug(r) {
850
+ return f(this, arguments, function* ({ field: e, from: t, filter: s, haveHistory: n }) {
851
+ try {
852
+ const u = t[e];
853
+ return L(u) ? { success: !0, result: Object.fromEntries(
854
+ yield Promise.all(
855
+ Object.entries(u).map((d) => f(this, [d], function* ([i, p]) {
856
+ const y = yield this.createUniqueSlug({
857
+ slug: p,
858
+ field: i,
859
+ isObject: !0,
860
+ haveHistory: n,
861
+ filter: s
862
+ });
863
+ return [i, y];
864
+ }))
865
+ )
866
+ ) } : { success: !0, result: yield this.createUniqueSlug({
867
+ slug: u,
868
+ field: e,
869
+ isObject: !1,
870
+ haveHistory: n,
871
+ filter: s
872
+ }) };
873
+ } catch (u) {
874
+ return m(u);
875
+ }
876
+ });
877
+ }
878
+ /**
879
+ * Checks if a slug already exists in the collection.
880
+ * This method verifies slug existence in both current and historical slug fields.
881
+ *
882
+ * @param options - Configuration for slug checking including slug, field, source document, and filter.
883
+ * @param options.slug - The slug string to check for existence.
884
+ * @param options.field - The field name for object-based slug checking.
885
+ * @param options.from - The source document containing the field value.
886
+ * @param options.haveHistory - Whether to check historical slug fields for existence.
887
+ * @param options.filter - Additional filter conditions to apply to the query.
888
+ * @returns A promise that resolves to a standardized response indicating whether the slug exists.
889
+ */
890
+ checkSlug(r) {
891
+ return f(this, arguments, function* ({ slug: e, field: t, from: s, filter: n, haveHistory: u }) {
892
+ try {
893
+ const o = s[t];
894
+ if (L(o)) {
895
+ const p = Object.values(o).map((O) => C(O));
896
+ return (yield Promise.all(
897
+ p.map(
898
+ (O) => this.model.exists(this.createSlugQuery({
899
+ slug: O,
900
+ field: t,
901
+ isObject: !0,
902
+ haveHistory: u,
903
+ filter: n
904
+ }))
905
+ )
906
+ )).some((O) => O) ? { success: !0, result: !0 } : { success: !0, result: !1 };
907
+ }
908
+ const c = C(e);
909
+ return { success: !0, result: (yield this.model.exists(this.createSlugQuery({
910
+ slug: c,
911
+ field: t,
912
+ isObject: !1,
913
+ filter: n
914
+ }))) !== null };
915
+ } catch (o) {
916
+ return m(o);
917
+ }
918
+ });
919
+ }
920
+ /**
921
+ * Performs aggregation operations on the collection.
922
+ *
923
+ * @param pipeline - The aggregation pipeline stages to execute.
924
+ * @returns A promise that resolves to a standardized response with the aggregation results.
925
+ */
926
+ aggregate(r) {
927
+ return f(this, null, function* () {
928
+ try {
929
+ return { success: !0, result: yield this.model.aggregate(r) };
930
+ } catch (e) {
931
+ return m(e);
932
+ }
933
+ });
934
+ }
935
+ /**
936
+ * Retrieves distinct values for the specified key from the collection.
937
+ *
938
+ * @param key - The field for which to return distinct values.
939
+ * @param filter - The filter query to apply (optional).
940
+ * @param options - Additional options for the distinct operation (optional).
941
+ * @returns A promise that resolves to a standardized response with the array of distinct values.
942
+ */
943
+ distinct(r) {
944
+ return f(this, arguments, function* (e, t = {}, s = {}) {
945
+ try {
946
+ return { success: !0, result: yield this.model.distinct(e, t, s) };
947
+ } catch (n) {
948
+ return m(n);
949
+ }
950
+ });
951
+ }
952
+ }
953
+ export {
954
+ le as MongoController,
955
+ ce as MongooseController
956
+ };