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