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