@cyberskill/shared 3.4.0 → 3.5.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 (65) hide show
  1. package/dist/config/graphql-codegen/graphql-codegen.type.d.ts +1 -1
  2. package/dist/config/storybook/storybook.preview.js +8 -15
  3. package/dist/config/storybook/storybook.preview.js.map +1 -1
  4. package/dist/config/vitest/vitest.unit.js.map +1 -1
  5. package/dist/constant/index.js +2 -2
  6. package/dist/constant/response-status.d.ts +254 -0
  7. package/dist/constant/response-status.js +65 -1
  8. package/dist/constant/response-status.js.map +1 -1
  9. package/dist/node/cli/index.js +10 -9
  10. package/dist/node/cli/index.js.map +1 -1
  11. package/dist/node/command/command.util.js +32 -20
  12. package/dist/node/command/command.util.js.map +1 -1
  13. package/dist/node/express/express.type.d.ts +2 -0
  14. package/dist/node/express/express.util.js +11 -4
  15. package/dist/node/express/express.util.js.map +1 -1
  16. package/dist/node/log/log.type.d.ts +1 -5
  17. package/dist/node/log/log.type.js.map +1 -1
  18. package/dist/node/log/log.util.d.ts +2 -4
  19. package/dist/node/log/log.util.js +38 -40
  20. package/dist/node/log/log.util.js.map +1 -1
  21. package/dist/node/mongo/mongo.controller.d.ts +3 -1
  22. package/dist/node/mongo/mongo.controller.mongoose.js +75 -78
  23. package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
  24. package/dist/node/mongo/mongo.controller.native.d.ts +9 -1
  25. package/dist/node/mongo/mongo.controller.native.js +17 -14
  26. package/dist/node/mongo/mongo.controller.native.js.map +1 -1
  27. package/dist/node/mongo/mongo.controller.type.d.ts +76 -0
  28. package/dist/node/mongo/mongo.dynamic-populate.d.ts +2 -1
  29. package/dist/node/mongo/mongo.dynamic-populate.js +12 -12
  30. package/dist/node/mongo/mongo.dynamic-populate.js.map +1 -1
  31. package/dist/node/mongo/mongo.internal-types.d.ts +81 -0
  32. package/dist/node/mongo/mongo.internal-types.js +40 -0
  33. package/dist/node/mongo/mongo.internal-types.js.map +1 -0
  34. package/dist/node/mongo/mongo.populate.d.ts +2 -1
  35. package/dist/node/mongo/mongo.populate.js +130 -171
  36. package/dist/node/mongo/mongo.populate.js.map +1 -1
  37. package/dist/node/mongo/mongo.type.d.ts +1 -1
  38. package/dist/node/mongo/mongo.type.js.map +1 -1
  39. package/dist/node/mongo/mongo.util.d.ts +7 -7
  40. package/dist/node/mongo/mongo.util.js +34 -32
  41. package/dist/node/mongo/mongo.util.js.map +1 -1
  42. package/dist/node/path/path.constant.d.ts +3 -6
  43. package/dist/node/path/path.constant.js +31 -25
  44. package/dist/node/path/path.constant.js.map +1 -1
  45. package/dist/node/storage/index.js +2 -2
  46. package/dist/node/storage/storage.util.d.ts +6 -0
  47. package/dist/node/storage/storage.util.js +4 -1
  48. package/dist/node/storage/storage.util.js.map +1 -1
  49. package/dist/react/log/log.type.d.ts +1 -5
  50. package/dist/react/log/log.util.d.ts +2 -5
  51. package/dist/react/log/log.util.js +21 -25
  52. package/dist/react/log/log.util.js.map +1 -1
  53. package/dist/util/common/common.util.d.ts +8 -0
  54. package/dist/util/common/common.util.js +4 -1
  55. package/dist/util/common/common.util.js.map +1 -1
  56. package/dist/util/common/index.js +2 -2
  57. package/dist/util/index.d.ts +1 -0
  58. package/dist/util/index.js +7 -6
  59. package/dist/util/log/index.d.ts +2 -0
  60. package/dist/util/log/index.js +2 -0
  61. package/dist/util/log/log.type.d.ts +8 -0
  62. package/dist/util/log/log.util.d.ts +17 -0
  63. package/dist/util/log/log.util.js +14 -0
  64. package/dist/util/log/log.util.js.map +1 -0
  65. package/package.json +83 -40
@@ -1,236 +1,195 @@
1
- import { convertEnumToModelName as e } from "./mongo.util.js";
1
+ import { getDynamicVirtualConfigs as e, resolveRef as t, searchVirtualsInSchema as n } from "./mongo.internal-types.js";
2
+ import { convertEnumToModelName as r } from "./mongo.util.js";
2
3
  //#region src/node/mongo/mongo.populate.ts
3
- async function t(e, t, i, a, o) {
4
- if (!t.length || !i) return t;
5
- let s = Array.isArray(i) ? i : [i];
6
- for (let i of s) typeof i == "string" ? await n(e, t, i, a, o) : i && typeof i == "object" && await r(e, t, i, a, o);
4
+ async function i(e, t, n, r, i) {
5
+ if (!t.length || !n) return t;
6
+ let a = Array.isArray(n) ? n : [n];
7
+ for (let n of a) typeof n == "string" ? await o(e, t, n, r, i) : n && typeof n == "object" && await s(e, t, n, r, i);
7
8
  return t;
8
9
  }
9
- async function n(t, n, r, i, a) {
10
- let o = r.split(".");
10
+ function a(n, i, a, o) {
11
+ if (!i) return;
12
+ if (i.schema?.virtuals) {
13
+ let e = i.schema.virtuals[a];
14
+ if (e?.options?.ref) {
15
+ let i = t(e.options.ref, o);
16
+ if (i) {
17
+ let e = r(i);
18
+ if (n.models[e]) return n.models[e];
19
+ }
20
+ }
21
+ }
22
+ let s = e(i).find((e) => e.name === a);
23
+ if (s?.options?.ref) {
24
+ let e = t(s.options.ref, o);
25
+ if (e) {
26
+ let t = r(e);
27
+ if (n.models[t]) return n.models[t];
28
+ }
29
+ }
30
+ return i;
31
+ }
32
+ async function o(e, t, n, r, i) {
33
+ let o = n.split(".");
11
34
  if (o.length < 2) {
12
- for (let e of n) await s(t, e, r, i, void 0, a);
35
+ for (let a of t) await d(e, a, n, r, void 0, i);
13
36
  return;
14
37
  }
15
- let c = o[0];
16
- if (!c || c.trim() === "") return;
17
- let l = o.slice(1).join(".");
18
- for (let r of n) {
19
- let n = r, o = n[c];
38
+ let s = o[0];
39
+ if (!s || s.trim() === "") return;
40
+ let c = o.slice(1).join(".");
41
+ for (let n of t) {
42
+ let t = n, o = t[s];
20
43
  if (o && typeof o == "object") {
21
- let r = a, u = r;
22
- if (a && a.schema && a.schema.virtuals) {
23
- let i = a.schema.virtuals[c];
24
- if (i && i.options && i.options.ref) {
25
- let a;
26
- if (typeof i.options.ref == "function" ? a = i.options.ref(n) : typeof i.options.ref == "string" && (a = i.options.ref), a) {
27
- let n = e(a);
28
- t.models[n] && (r = t.models[n]);
29
- }
30
- }
31
- }
32
- if (!r) {
33
- let i = (a?._virtualConfigs || (a?.schema?.statics ?? {})._dynamicVirtuals || []).find((e) => e.name === c);
34
- if (i && i.options && i.options.ref) {
35
- let a;
36
- if (typeof i.options.ref == "function" ? a = i.options.ref(n) : typeof i.options.ref == "string" && (a = i.options.ref), a) {
37
- let n = e(a);
38
- t.models[n] && (r = t.models[n]);
39
- }
40
- }
41
- }
42
- if (r === u) {
43
- let i = (a?._virtualConfigs || (a?.schema?.statics ?? {})._dynamicVirtuals || []).find((e) => e.name === c);
44
- if (i && i.options && i.options.ref) {
45
- let a;
46
- if (typeof i.options.ref == "function" ? a = i.options.ref(n) : typeof i.options.ref == "string" && (a = i.options.ref), a) {
47
- let n = e(a);
48
- t.models[n] && (r = t.models[n]);
49
- }
50
- }
51
- }
52
- if (Array.isArray(o)) for (let e of o) e && typeof e == "object" && await s(t, e, l, i, c, r);
53
- else o && typeof o == "object" && await s(t, o, l, i, c, r);
44
+ let n = a(e, i, s, t);
45
+ if (Array.isArray(o)) for (let t of o) t && typeof t == "object" && await d(e, t, c, r, s, n);
46
+ else o && typeof o == "object" && await d(e, o, c, r, s, n);
54
47
  }
55
48
  }
56
49
  }
57
- async function r(r, i, a, o, s) {
58
- let { path: c, populate: l } = a;
59
- if (!c) return;
60
- if (!l) {
61
- await n(r, i, c, o, s);
50
+ async function s(e, n, a, s, c) {
51
+ let { path: l, populate: u } = a;
52
+ if (!l) return;
53
+ if (!u) {
54
+ await o(e, n, l, s, c);
62
55
  return;
63
56
  }
64
- let u = c;
65
- for (let n of i) {
66
- let i = n, a = i[u], c = s;
67
- if (s && s.schema && s.schema.virtuals) {
68
- let t = s.schema.virtuals[u];
69
- if (t && t.options && t.options.ref) {
70
- let n;
71
- if (typeof t.options.ref == "function" ? n = t.options.ref(i) : typeof t.options.ref == "string" && (n = t.options.ref), n) {
72
- let t = e(n);
73
- r.models[t] && (c = r.models[t]);
57
+ let d = l;
58
+ for (let a of n) {
59
+ let n = a, o = n[d], l = c;
60
+ if (c?.schema?.virtuals) {
61
+ let i = c.schema.virtuals[d];
62
+ if (i?.options?.ref) {
63
+ let a = t(i.options.ref, n);
64
+ if (a) {
65
+ let t = r(a);
66
+ e.models[t] && (l = e.models[t]);
74
67
  }
75
68
  }
76
69
  }
77
- if (!c && typeof a == "object" && a && "entityType" in a) {
78
- let t = e(String(a.entityType));
79
- r.models[t] && (c = r.models[t]);
70
+ if (!l && typeof o == "object" && o && "entityType" in o) {
71
+ let t = r(String(o.entityType));
72
+ e.models[t] && (l = e.models[t]);
80
73
  }
81
- if (a && typeof a == "object") if (Array.isArray(a)) for (let e of a) e && typeof e == "object" && await t(r, [e], l, o, c);
82
- else a && typeof a == "object" && await t(r, [a], l, o, c);
74
+ if (o && typeof o == "object") if (Array.isArray(o)) for (let t of o) t && typeof t == "object" && await i(e, [t], u, s, l);
75
+ else o && typeof o == "object" && await i(e, [o], u, s, l);
83
76
  }
84
77
  }
85
- function i(e, t, n, r) {
86
- if (!n || !t || !t.schema) return;
87
- let i = n.split("."), a = t.schema;
88
- for (let t = 0; t < i.length; t++) {
89
- let n = i[t];
90
- if (a && a.virtuals && n) {
91
- let o = a.virtuals[n];
92
- if (o && o.options && o.options.ref) {
93
- let n;
94
- if (typeof o.options.ref == "function" ? n = o.options.ref(r) : typeof o.options.ref == "string" && (n = o.options.ref), n && typeof n == "string") {
95
- if (t === i.length - 1) return n;
96
- let r = e.models[n];
97
- if (r && r.schema) {
98
- a = r.schema;
78
+ function c(e, n, r, i) {
79
+ if (!r || !n || !n.schema) return;
80
+ let a = r.split("."), o = n.schema;
81
+ for (let n = 0; n < a.length; n++) {
82
+ let r = a[n];
83
+ if (o?.virtuals && r) {
84
+ let s = o.virtuals[r];
85
+ if (s?.options?.ref) {
86
+ let r = t(s.options.ref, i);
87
+ if (r && typeof r == "string") {
88
+ if (n === a.length - 1) return r;
89
+ let t = e.models[r];
90
+ if (t && t.schema) {
91
+ o = t.schema;
99
92
  continue;
100
93
  }
101
94
  }
102
95
  }
103
96
  }
104
- if (a && a.paths && n) {
105
- let e = a.paths[n];
106
- if (e && e.schema) {
107
- a = e.schema;
97
+ if (o?.paths && r) {
98
+ let e = o.paths[r];
99
+ if (e?.schema) {
100
+ o = e.schema;
108
101
  continue;
109
102
  }
110
103
  }
111
104
  return;
112
105
  }
113
106
  }
114
- function a(e, t) {
107
+ function l(e, t) {
115
108
  if (t) for (let n of Object.keys(e.models)) {
116
109
  let r = e.models[n], i = r?.schema;
117
110
  if (i && (i.paths && i.paths[t] || i.virtuals && i.virtuals[t])) return r;
118
111
  }
119
112
  }
120
- function o(e, t) {
113
+ function u(e, t) {
121
114
  let n = (t || "").split(".")[0] || "";
122
115
  if (n) for (let t of Object.keys(e.models)) {
123
116
  let r = e.models[t], i = r?.schema;
124
117
  if (i && (i.paths && i.paths[n] || i.virtuals && i.virtuals[n])) return r;
125
118
  }
126
119
  }
127
- async function s(e, t, n, r, s, c) {
128
- let l = t.__t;
129
- if (!l) {
130
- if (c) {
131
- let r = s ? `${s}.${n}` : n, u = (r || "").split(".")[0] || "", d = c;
132
- if (!(d?.schema?.paths && d.schema.paths[u] || d?.schema?.virtuals && d.schema.virtuals[u])) {
133
- let t = o(e, r);
134
- t && (d = t);
120
+ async function d(e, r, i, a, o, s) {
121
+ let d = r.__t;
122
+ if (!d) {
123
+ if (s) {
124
+ let t = o ? `${o}.${i}` : i, n = (t || "").split(".")[0] || "", a = s;
125
+ if (!(a?.schema?.paths && a.schema.paths[n] || a?.schema?.virtuals && a.schema.virtuals[n])) {
126
+ let n = u(e, t);
127
+ n && (a = n);
135
128
  }
136
- let f = i(e, d, r, t);
137
- if (f) l = f;
129
+ let f = c(e, a, t, r);
130
+ if (f) d = f;
138
131
  else {
139
- let n = (r.includes(".") ? r.split(".").pop() : r) || "";
132
+ let n = (t.includes(".") ? t.split(".").pop() : t) || "";
140
133
  if (n) {
141
- let o = a(e, n);
142
- if (o) {
143
- let n = i(e, o, r, t);
144
- n && (l = n);
134
+ let i = l(e, n);
135
+ if (i) {
136
+ let n = c(e, i, t, r);
137
+ n && (d = n);
145
138
  }
146
139
  }
147
140
  }
148
141
  }
149
- if (r && r.length > 0) {
150
- let e = n.split(".").pop() || "", i = r.find((t) => t.name === e);
151
- if (i && i.options.ref) {
152
- let e;
153
- typeof i.options.ref == "function" ? e = i.options.ref(t) : typeof i.options.ref == "string" && (e = i.options.ref), e && typeof e == "string" && (l = e);
142
+ if (a && a.length > 0) {
143
+ let e = i.split(".").pop() || "", n = a.find((t) => t.name === e);
144
+ if (n?.options.ref) {
145
+ let e = t(n.options.ref, r);
146
+ e && typeof e == "string" && (d = e);
154
147
  }
155
148
  }
156
- if (!l) {
157
- for (let [e, n] of Object.entries(t)) if (e === "entityType" && typeof n == "string") {
158
- l = n;
149
+ if (!d) {
150
+ for (let [e, t] of Object.entries(r)) if (e === "entityType" && typeof t == "string") {
151
+ d = t;
159
152
  break;
160
153
  }
161
- if (l && e.models[l]) {
162
- let r = e.models[l];
163
- if (r && r.schema) {
164
- let e = r.schema, i = n.split(".").pop() || "", a = (e, n = "root") => {
165
- if (!e || !e.virtuals) return;
166
- let r = e.virtuals;
167
- for (let e of Object.keys(r)) if (e === i) {
168
- let n = r[e];
169
- if (n && n.options && n.options.ref) {
170
- let e;
171
- if (typeof n.options.ref == "function" ? e = n.options.ref(t) : typeof n.options.ref == "string" && (e = n.options.ref), e && typeof e == "string") return e;
172
- }
173
- }
174
- if (e.paths) for (let t of Object.keys(e.paths)) {
175
- let r = e.paths[t];
176
- if (r && r.schema) {
177
- let e = a(r.schema, `${n}.${t}`);
178
- if (e) return e;
179
- }
180
- }
181
- }, o = a(e);
182
- o && (l = o);
154
+ if (d && e.models[d]) {
155
+ let t = e.models[d];
156
+ if (t?.schema) {
157
+ let e = i.split(".").pop() || "", a = n(t.schema, e, r);
158
+ a && (d = a);
183
159
  }
184
160
  }
185
161
  }
186
- if (!l) {
187
- let a = n.split(".").pop() || "";
188
- if (c && c.schema) {
189
- let e = c.schema, n = (e, r = "root") => {
190
- if (!e || !e.virtuals) return;
191
- let i = e.virtuals;
192
- for (let e of Object.keys(i)) if (e === a) {
193
- let n = i[e];
194
- if (n && n.options && n.options.ref) {
195
- let e;
196
- if (typeof n.options.ref == "function" ? e = n.options.ref(t) : typeof n.options.ref == "string" && (e = n.options.ref), e && typeof e == "string") return e;
197
- }
198
- }
199
- if (e.paths) for (let t of Object.keys(e.paths)) {
200
- let i = e.paths[t];
201
- if (i && i.schema) {
202
- let e = n(i.schema, `${r}.${t}`);
203
- if (e) return e;
204
- }
205
- }
206
- }, r = n(e);
207
- r && (l = r);
162
+ if (!d) {
163
+ let o = i.split(".").pop() || "";
164
+ if (s?.schema) {
165
+ let e = n(s.schema, o, r);
166
+ e && (d = e);
208
167
  }
209
- if (!l && c) {
210
- let r = i(e, c, n, t);
211
- r && (l = r);
168
+ if (!d && s) {
169
+ let t = c(e, s, i, r);
170
+ t && (d = t);
212
171
  }
213
- if (!l && r && r.length > 0) {
214
- let e = r.find((e) => e.name === a);
215
- if (e && e.options.ref) {
216
- let n;
217
- typeof e.options.ref == "function" ? n = e.options.ref(t) : typeof e.options.ref == "string" && (n = e.options.ref), n && typeof n == "string" && (l = n);
172
+ if (!d && a && a.length > 0) {
173
+ let e = a.find((e) => e.name === o);
174
+ if (e?.options.ref) {
175
+ let n = t(e.options.ref, r);
176
+ n && typeof n == "string" && (d = n);
218
177
  }
219
178
  }
220
179
  }
221
- if (!l) {
222
- let t = n.split(".").pop() || "", r = t ? t.charAt(0).toUpperCase() + t.slice(1) : "";
223
- r && e.models[r] && (l = r);
180
+ if (!d) {
181
+ let t = i.split(".").pop() || "", n = t ? t.charAt(0).toUpperCase() + t.slice(1) : "";
182
+ n && e.models[n] && (d = n);
224
183
  }
225
184
  }
226
- let u = e.models[l];
227
- if (!u) return;
228
- let d = `${n}Id`, f = `${n}Ids`, p = t[d] || t[f] || t[n];
229
- if (!p) return;
230
- let m = await u.find({ id: { $in: Array.isArray(p) ? p : [p] } }).lean();
231
- m.length > 0 && (Array.isArray(p) ? t[n] = m : t[n] = m[0]);
185
+ let f = e.models[d];
186
+ if (!f) return;
187
+ let p = `${i}Id`, m = `${i}Ids`, h = r[p] || r[m] || r[i];
188
+ if (!h) return;
189
+ let g = await f.find({ id: { $in: Array.isArray(h) ? h : [h] } }).lean();
190
+ g.length > 0 && (Array.isArray(h) ? r[i] = g : r[i] = g[0]);
232
191
  }
233
192
  //#endregion
234
- export { t as applyNestedPopulate };
193
+ export { i as applyNestedPopulate };
235
194
 
236
195
  //# sourceMappingURL=mongo.populate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mongo.populate.js","names":[],"sources":["../../../src/node/mongo/mongo.populate.ts"],"sourcesContent":["import type mongooseRaw from 'mongoose';\n\nimport type { I_DynamicVirtualConfig, T_Input_Populate } from './mongo.type.js';\n\nimport { convertEnumToModelName } from './mongo.util.js';\n\n/**\n * Recursively applies nested populate options to populated documents.\n * This function handles cases where a populated field needs further population.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to apply nested populations to\n * @param populateOptions - The populate options to apply\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n * @returns Promise with documents that have nested populations applied\n */\nexport async function applyNestedPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populateOptions: T_Input_Populate,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: any,\n): Promise<T[]> {\n if (!documents.length || !populateOptions) {\n return documents;\n }\n\n const populateArray = Array.isArray(populateOptions) ? populateOptions : [populateOptions];\n\n for (const populateOption of populateArray) {\n if (typeof populateOption === 'string') {\n await applyStringPopulate(mongoose, documents, populateOption, virtualConfigs, currentModel);\n }\n else if (populateOption && typeof populateOption === 'object') {\n const popObj = populateOption as { path?: string; populate?: T_Input_Populate; [key: string]: unknown };\n\n await applyObjectPopulate(mongoose, documents, popObj, virtualConfigs, currentModel);\n }\n }\n\n return documents;\n}\n\n/**\n * Applies string-based populate options (e.g., \"field.subfield\") to documents.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to populate\n * @param populatePath - The populate path string\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function applyStringPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populatePath: string,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: any,\n): Promise<void> {\n const pathParts = populatePath.split('.');\n\n if (pathParts.length < 2) {\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n\n await populateNestedFieldOnParent(mongoose, docObj, populatePath, virtualConfigs, undefined, currentModel);\n }\n\n return;\n }\n\n const mainField = pathParts[0];\n\n if (!mainField || mainField.trim() === '') {\n return;\n }\n\n const nestedPath = pathParts.slice(1).join('.');\n\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n const mainValue = docObj[mainField];\n\n if (mainValue && typeof mainValue === 'object') {\n let nextModelForChildren = currentModel;\n const originalModelForChildren = nextModelForChildren;\n\n if (currentModel && currentModel.schema && currentModel.schema.virtuals) {\n const virtual = currentModel.schema.virtuals[mainField];\n\n if (virtual && virtual.options && virtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof virtual.options.ref === 'function') {\n refResult = virtual.options.ref(docObj);\n }\n else if (typeof virtual.options.ref === 'string') {\n refResult = virtual.options.ref;\n }\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n\n if (mongoose.models[modelName]) {\n nextModelForChildren = mongoose.models[modelName];\n }\n }\n }\n }\n\n if (!nextModelForChildren) {\n const schemaStatics = (currentModel?.schema?.statics ?? {}) as Record<string, unknown>;\n const dynamicVirtuals = ((currentModel as any)?._virtualConfigs as Array<{ name: string; options?: { ref?: unknown } }>)\n || (schemaStatics['_dynamicVirtuals'] as Array<{ name: string; options?: { ref?: unknown } }> | undefined)\n || [];\n const dyn = (dynamicVirtuals as Array<{ name: string; options?: { ref?: unknown } }>).find(v => v.name === mainField);\n\n if (dyn && dyn.options && dyn.options.ref) {\n let refResult: string | undefined;\n\n if (typeof dyn.options.ref === 'function') {\n refResult = (dyn.options.ref as (d: unknown) => string | undefined)(docObj);\n }\n else if (typeof dyn.options.ref === 'string') {\n refResult = dyn.options.ref as string;\n }\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n\n if (mongoose.models[modelName]) {\n nextModelForChildren = mongoose.models[modelName];\n }\n }\n }\n }\n\n if (nextModelForChildren === originalModelForChildren) {\n const schemaStatics = (currentModel?.schema?.statics ?? {}) as Record<string, unknown>;\n const dynamicVirtuals = ((currentModel as any)?._virtualConfigs as Array<{ name: string; options?: { ref?: unknown } }>)\n || (schemaStatics['_dynamicVirtuals'] as Array<{ name: string; options?: { ref?: unknown } }> | undefined)\n || [];\n const dyn = (dynamicVirtuals as Array<{ name: string; options?: { ref?: unknown } }>).find(v => v.name === mainField);\n\n if (dyn && dyn.options && dyn.options.ref) {\n let refResult: string | undefined;\n\n if (typeof dyn.options.ref === 'function') {\n refResult = (dyn.options.ref as (d: unknown) => string | undefined)(docObj);\n }\n else if (typeof dyn.options.ref === 'string') {\n refResult = dyn.options.ref as string;\n }\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n\n if (mongoose.models[modelName]) {\n nextModelForChildren = mongoose.models[modelName];\n }\n }\n }\n }\n\n if (Array.isArray(mainValue)) {\n for (const item of mainValue) {\n if (item && typeof item === 'object') {\n await populateNestedFieldOnParent(mongoose, item as { [key: string]: unknown }, nestedPath, virtualConfigs, mainField, nextModelForChildren);\n }\n }\n }\n else if (mainValue && typeof mainValue === 'object') {\n await populateNestedFieldOnParent(mongoose, mainValue as { [key: string]: unknown }, nestedPath, virtualConfigs, mainField, nextModelForChildren);\n }\n }\n }\n}\n\n/**\n * Applies object-based populate options with nested populate to documents.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to populate\n * @param populateOption - The populate option object\n * @param populateOption.path - The path to populate\n * @param populateOption.populate - The nested populate options\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function applyObjectPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populateOption: { path?: string; populate?: T_Input_Populate; [key: string]: unknown },\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: any,\n): Promise<void> {\n const { path, populate: nestedPopulate } = populateOption;\n\n if (!path) {\n return;\n }\n\n if (!nestedPopulate) {\n await applyStringPopulate(mongoose, documents, path, virtualConfigs, currentModel);\n\n return;\n }\n\n const pathString = path;\n\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n const fieldValue = docObj[pathString];\n\n let nextModelForChildren = currentModel;\n\n if (currentModel && currentModel.schema && currentModel.schema.virtuals) {\n const virtual = currentModel.schema.virtuals[pathString];\n\n if (virtual && virtual.options && virtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof virtual.options.ref === 'function') {\n refResult = virtual.options.ref(docObj);\n }\n else if (typeof virtual.options.ref === 'string') {\n refResult = virtual.options.ref;\n }\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n if (mongoose.models[modelName]) {\n nextModelForChildren = mongoose.models[modelName];\n }\n }\n }\n }\n if (!nextModelForChildren && typeof fieldValue === 'object' && fieldValue && 'entityType' in (fieldValue as object)) {\n const maybeModel = convertEnumToModelName(String((fieldValue as { [k: string]: unknown })['entityType']));\n\n if (mongoose.models[maybeModel]) {\n nextModelForChildren = mongoose.models[maybeModel];\n }\n }\n\n if (fieldValue && typeof fieldValue === 'object') {\n if (Array.isArray(fieldValue)) {\n for (const item of fieldValue) {\n if (item && typeof item === 'object') {\n await applyNestedPopulate(mongoose, [item as { [key: string]: unknown }], nestedPopulate, virtualConfigs, nextModelForChildren);\n }\n }\n }\n else if (fieldValue && typeof fieldValue === 'object') {\n await applyNestedPopulate(mongoose, [fieldValue as { [key: string]: unknown }], nestedPopulate, virtualConfigs, nextModelForChildren);\n }\n }\n }\n}\n\n/**\n * Resolves the target model for a given populate path by following the path step by step.\n * This function traverses the schema hierarchy to find the correct model for population.\n *\n * @param mongoose - The Mongoose instance\n * @param startModel - The starting model (usually the model containing the document)\n * @param path - The populate path to resolve (e.g., \"entity.partner1.gallery\")\n * @param document - The document being populated (for dynamic virtual resolution)\n * @returns The resolved model name or undefined if not found\n */\nfunction resolveModelFromPath(\n mongoose: typeof mongooseRaw,\n startModel: any,\n path: string,\n document: { [key: string]: unknown },\n): string | undefined {\n if (!path || !startModel || !startModel.schema) {\n return undefined;\n }\n\n const pathParts = path.split('.');\n let currentSchema = startModel.schema;\n\n for (let i = 0; i < pathParts.length; i++) {\n const pathPart = pathParts[i];\n\n if (currentSchema && currentSchema.virtuals && pathPart) {\n const virtual = currentSchema.virtuals[pathPart];\n if (virtual && virtual.options && virtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof virtual.options.ref === 'function') {\n refResult = virtual.options.ref(document);\n }\n else if (typeof virtual.options.ref === 'string') {\n refResult = virtual.options.ref;\n }\n if (refResult && typeof refResult === 'string') {\n if (i === pathParts.length - 1) {\n return refResult;\n }\n\n const nextModel = mongoose.models[refResult];\n\n if (nextModel && nextModel.schema) {\n currentSchema = nextModel.schema;\n continue;\n }\n }\n }\n }\n\n if (currentSchema && currentSchema.paths && pathPart) {\n const pathSchema = currentSchema.paths[pathPart];\n\n if (pathSchema && pathSchema.schema) {\n currentSchema = pathSchema.schema;\n continue;\n }\n }\n\n return undefined;\n }\n\n return undefined;\n}\n\n/**\n * Attempts to find a model whose schema contains the given field\n * either as a direct path or a virtual. Searches only at the root level\n * of each model schema.\n */\nfunction findModelBySchemaField(\n mongoose: typeof mongooseRaw,\n fieldName: string,\n): any | undefined {\n if (!fieldName) {\n return undefined;\n }\n for (const modelName of Object.keys(mongoose.models)) {\n const Model = mongoose.models[modelName];\n const schema = Model?.schema as any;\n\n if (!schema) {\n continue;\n }\n if ((schema.paths && schema.paths[fieldName]) || (schema.virtuals && schema.virtuals[fieldName])) {\n return Model;\n }\n }\n\n return undefined;\n}\n\n/**\n * Finds a start model whose root schema contains the first segment of the path\n * as either a direct path or a virtual. Useful to jump into the correct model\n * when the current model context is misaligned (e.g., 'partner1.*' should start from User).\n */\nfunction findStartModelByFirstSegment(\n mongoose: typeof mongooseRaw,\n fullPath: string,\n): any | undefined {\n const first = (fullPath || '').split('.')[0] || '';\n\n if (!first) {\n return undefined;\n }\n for (const modelName of Object.keys(mongoose.models)) {\n const Model = mongoose.models[modelName];\n const schema = Model?.schema as any;\n\n if (!schema) {\n continue;\n }\n if ((schema.paths && schema.paths[first]) || (schema.virtuals && schema.virtuals[first])) {\n return Model;\n }\n }\n\n return undefined;\n}\n\n/**\n * Populates a nested field on the parent object by finding the referenced document and applying populate options.\n *\n * @param mongoose - The Mongoose instance\n * @param document - The document containing the field to populate\n * @param nestedPath - The nested populate path\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param pathPrefix - The parent path prefix (if nested within another populated field)\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function populateNestedFieldOnParent(\n mongoose: typeof mongooseRaw,\n document: { [key: string]: unknown },\n nestedPath: string,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n pathPrefix?: string,\n currentModel?: any,\n): Promise<void> {\n let modelName = document['__t'];\n\n if (!modelName) {\n if (currentModel) {\n const fullPath = pathPrefix ? `${pathPrefix}.${nestedPath}` : nestedPath;\n\n const firstSegment = (fullPath || '').split('.')[0] || '';\n let startModel = currentModel;\n const hasFirstOnCurrent = Boolean(\n (startModel?.schema?.paths && startModel.schema.paths[firstSegment])\n || (startModel?.schema?.virtuals && startModel.schema.virtuals[firstSegment]),\n );\n\n if (!hasFirstOnCurrent) {\n const betterStart = findStartModelByFirstSegment(mongoose, fullPath);\n\n if (betterStart) {\n startModel = betterStart;\n }\n }\n\n const resolvedFromPath = resolveModelFromPath(mongoose, startModel, fullPath, document);\n\n if (resolvedFromPath) {\n modelName = resolvedFromPath;\n }\n else {\n const lastSegment = (fullPath.includes('.') ? fullPath.split('.').pop() : fullPath) || '';\n\n if (lastSegment) {\n const candidateModel = findModelBySchemaField(mongoose, lastSegment);\n\n if (candidateModel) {\n const rerun = resolveModelFromPath(mongoose, candidateModel, fullPath, document);\n\n if (rerun) {\n modelName = rerun;\n }\n }\n }\n }\n }\n\n if (virtualConfigs && virtualConfigs.length > 0) {\n const fieldName = nestedPath.split('.').pop() || '';\n const matchingVirtual = virtualConfigs.find(v => v.name === fieldName);\n\n if (matchingVirtual && matchingVirtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof matchingVirtual.options.ref === 'function') {\n refResult = matchingVirtual.options.ref(document);\n }\n else if (typeof matchingVirtual.options.ref === 'string') {\n refResult = matchingVirtual.options.ref;\n }\n\n if (refResult && typeof refResult === 'string') {\n modelName = refResult;\n }\n }\n }\n\n if (!modelName) {\n for (const [key, value] of Object.entries(document as Record<string, unknown>)) {\n if (key === 'entityType' && typeof value === 'string') {\n modelName = value;\n break;\n }\n }\n\n if (modelName && mongoose.models[modelName as string]) {\n const Model = mongoose.models[modelName as string];\n\n if (Model && Model.schema) {\n const schema = Model.schema;\n const fieldName = nestedPath.split('.').pop() || '';\n\n const searchVirtualsInSchema = (schemaToSearch: any, schemaPath = 'root'): string | undefined => {\n if (!schemaToSearch || !schemaToSearch.virtuals) {\n return undefined;\n }\n\n const virtuals = schemaToSearch.virtuals;\n\n for (const virtualName of Object.keys(virtuals)) {\n if (virtualName === fieldName) {\n const virtual = virtuals[virtualName];\n if (virtual && virtual.options && virtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof virtual.options.ref === 'function') {\n refResult = virtual.options.ref(document);\n }\n else if (typeof virtual.options.ref === 'string') {\n refResult = virtual.options.ref;\n }\n if (refResult && typeof refResult === 'string') {\n return refResult;\n }\n }\n }\n }\n\n if (schemaToSearch.paths) {\n for (const pathName of Object.keys(schemaToSearch.paths)) {\n const pathSchema = schemaToSearch.paths[pathName];\n\n if (pathSchema && pathSchema.schema) {\n const nestedResult = searchVirtualsInSchema(pathSchema.schema, `${schemaPath}.${pathName}`);\n\n if (nestedResult) {\n return nestedResult;\n }\n }\n }\n }\n\n return undefined;\n };\n\n const foundModelName = searchVirtualsInSchema(schema);\n if (foundModelName) {\n modelName = foundModelName;\n }\n }\n }\n }\n\n if (!modelName) {\n const fieldName = nestedPath.split('.').pop() || '';\n\n if (currentModel && currentModel.schema) {\n const schema = currentModel.schema;\n\n const searchVirtualsInSchema = (schemaToSearch: any, schemaPath = 'root'): string | undefined => {\n if (!schemaToSearch || !schemaToSearch.virtuals) {\n return undefined;\n }\n\n const virtuals = schemaToSearch.virtuals;\n\n for (const virtualName of Object.keys(virtuals)) {\n if (virtualName === fieldName) {\n const virtual = virtuals[virtualName];\n if (virtual && virtual.options && virtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof virtual.options.ref === 'function') {\n refResult = virtual.options.ref(document);\n }\n else if (typeof virtual.options.ref === 'string') {\n refResult = virtual.options.ref;\n }\n\n if (refResult && typeof refResult === 'string') {\n return refResult;\n }\n }\n }\n }\n\n if (schemaToSearch.paths) {\n for (const pathName of Object.keys(schemaToSearch.paths)) {\n const pathSchema = schemaToSearch.paths[pathName];\n\n if (pathSchema && pathSchema.schema) {\n const nestedResult = searchVirtualsInSchema(pathSchema.schema, `${schemaPath}.${pathName}`);\n\n if (nestedResult) {\n return nestedResult;\n }\n }\n }\n }\n\n return undefined;\n };\n\n const foundModelName = searchVirtualsInSchema(schema);\n if (foundModelName) {\n modelName = foundModelName;\n }\n }\n\n if (!modelName) {\n if (currentModel) {\n const resolvedModel = resolveModelFromPath(mongoose, currentModel, nestedPath, document);\n\n if (resolvedModel) {\n modelName = resolvedModel;\n }\n }\n }\n\n if (!modelName && virtualConfigs && virtualConfigs.length > 0) {\n const matchingVirtual = virtualConfigs.find(v => v.name === fieldName);\n\n if (matchingVirtual && matchingVirtual.options.ref) {\n let refResult: string | undefined;\n\n if (typeof matchingVirtual.options.ref === 'function') {\n refResult = matchingVirtual.options.ref(document);\n }\n else if (typeof matchingVirtual.options.ref === 'string') {\n refResult = matchingVirtual.options.ref;\n }\n if (refResult && typeof refResult === 'string') {\n modelName = refResult;\n }\n }\n }\n }\n\n if (!modelName) {\n const fieldName = nestedPath.split('.').pop() || '';\n const candidate = fieldName ? fieldName.charAt(0).toUpperCase() + fieldName.slice(1) : '';\n\n if (candidate && mongoose.models[candidate]) {\n modelName = candidate;\n }\n }\n }\n\n const Model = mongoose.models[modelName as string];\n\n if (!Model) {\n return;\n }\n\n const fieldIdKey = `${nestedPath}Id`;\n const fieldIdsKey = `${nestedPath}Ids`;\n\n const docIds = document[fieldIdKey] || document[fieldIdsKey] || document[nestedPath];\n\n if (!docIds) {\n return;\n }\n\n const idsArray = Array.isArray(docIds) ? docIds : [docIds];\n\n const populatedDocs = await Model.find({ id: { $in: idsArray } }).lean();\n\n if (populatedDocs.length > 0) {\n if (Array.isArray(docIds)) {\n document[nestedPath] = populatedDocs;\n }\n else {\n document[nestedPath] = populatedDocs[0];\n }\n }\n}\n"],"mappings":";;AAkBA,eAAsB,EAClB,GACA,GACA,GACA,GACA,GACY;AACZ,KAAI,CAAC,EAAU,UAAU,CAAC,EACtB,QAAO;CAGX,IAAM,IAAgB,MAAM,QAAQ,EAAgB,GAAG,IAAkB,CAAC,EAAgB;AAE1F,MAAK,IAAM,KAAkB,EACzB,CAAI,OAAO,KAAmB,WAC1B,MAAM,EAAoB,GAAU,GAAW,GAAgB,GAAgB,EAAa,GAEvF,KAAkB,OAAO,KAAmB,YAGjD,MAAM,EAAoB,GAAU,GAFrB,GAEwC,GAAgB,EAAa;AAI5F,QAAO;;AAaX,eAAe,EACX,GACA,GACA,GACA,GACA,GACa;CACb,IAAM,IAAY,EAAa,MAAM,IAAI;AAEzC,KAAI,EAAU,SAAS,GAAG;AACtB,OAAK,IAAM,KAAO,EAGd,OAAM,EAA4B,GAFnB,GAE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K,MAAK,EAAE,SAAS,EAAU;AAEtE,QAAI,KAAmB,EAAgB,QAAQ,KAAK;KAChD,IAAI;AAQJ,KANI,OAAO,EAAgB,QAAQ,OAAQ,aACvC,IAAY,EAAgB,QAAQ,IAAI,EAAS,GAE5C,OAAO,EAAgB,QAAQ,OAAQ,aAC5C,IAAY,EAAgB,QAAQ,MAEpC,KAAa,OAAO,KAAc,aAClC,IAAY;;;;AAM5B,MAAI,CAAC,GAAW;GACZ,IAAM,IAAY,EAAW,MAAM,IAAI,CAAC,KAAK,IAAI,IAC3C,IAAY,IAAY,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,GAAG;AAEvF,GAAI,KAAa,EAAS,OAAO,OAC7B,IAAY;;;CAKxB,IAAM,IAAQ,EAAS,OAAO;AAE9B,KAAI,CAAC,EACD;CAGJ,IAAM,IAAa,GAAG,EAAW,KAC3B,IAAc,GAAG,EAAW,MAE5B,IAAS,EAAS,MAAe,EAAS,MAAgB,EAAS;AAEzE,KAAI,CAAC,EACD;CAKJ,IAAM,IAAgB,MAAM,EAAM,KAAK,EAAE,IAAI,EAAE,KAF9B,MAAM,QAAQ,EAAO,GAAG,IAAS,CAAC,EAAO,EAEI,EAAE,CAAC,CAAC,MAAM;AAExE,CAAI,EAAc,SAAS,MACnB,MAAM,QAAQ,EAAO,GACrB,EAAS,KAAc,IAGvB,EAAS,KAAc,EAAc"}
1
+ {"version":3,"file":"mongo.populate.js","names":[],"sources":["../../../src/node/mongo/mongo.populate.ts"],"sourcesContent":["import type mongooseRaw from 'mongoose';\n\nimport type { I_ModelWithSchema } from './mongo.internal-types.js';\nimport type { I_DynamicVirtualConfig, T_Input_Populate } from './mongo.type.js';\n\nimport { getDynamicVirtualConfigs, resolveRef, searchVirtualsInSchema } from './mongo.internal-types.js';\nimport { convertEnumToModelName } from './mongo.util.js';\n\n/**\n * Recursively applies nested populate options to populated documents.\n * This function handles cases where a populated field needs further population.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to apply nested populations to\n * @param populateOptions - The populate options to apply\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n * @returns Promise with documents that have nested populations applied\n */\nexport async function applyNestedPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populateOptions: T_Input_Populate,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: I_ModelWithSchema,\n): Promise<T[]> {\n if (!documents.length || !populateOptions) {\n return documents;\n }\n\n const populateArray = Array.isArray(populateOptions) ? populateOptions : [populateOptions];\n\n for (const populateOption of populateArray) {\n if (typeof populateOption === 'string') {\n await applyStringPopulate(mongoose, documents, populateOption, virtualConfigs, currentModel);\n }\n else if (populateOption && typeof populateOption === 'object') {\n const popObj = populateOption as { path?: string; populate?: T_Input_Populate; [key: string]: unknown };\n\n await applyObjectPopulate(mongoose, documents, popObj, virtualConfigs, currentModel);\n }\n }\n\n return documents;\n}\n\n/**\n * Resolves the next child model from virtual/dynamic configurations on the current model.\n * Consolidates the repeated _virtualConfigs / schema.virtuals / _dynamicVirtuals lookup.\n *\n * @param mongoose - The Mongoose instance\n * @param currentModel - The model whose schema to search\n * @param fieldName - The field name to look up\n * @param document - The document context for function refs\n * @returns The resolved child model, or the original currentModel if not resolved\n */\nfunction resolveChildModel(\n mongoose: typeof mongooseRaw,\n currentModel: I_ModelWithSchema | undefined,\n fieldName: string,\n document: Record<string, unknown>,\n): I_ModelWithSchema | undefined {\n if (!currentModel) {\n return undefined;\n }\n\n // 1. Try schema virtuals\n if (currentModel.schema?.virtuals) {\n const virtual = currentModel.schema.virtuals[fieldName];\n if (virtual?.options?.ref) {\n const refResult = resolveRef(virtual.options.ref, document);\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n if (mongoose.models[modelName]) {\n return mongoose.models[modelName] as unknown as I_ModelWithSchema;\n }\n }\n }\n }\n\n // 2. Try dynamic virtual configs (_virtualConfigs / _dynamicVirtuals)\n const dynamicVirtuals = getDynamicVirtualConfigs(currentModel);\n const dyn = dynamicVirtuals.find(v => v.name === fieldName);\n\n if (dyn?.options?.ref) {\n const refResult = resolveRef(dyn.options.ref, document);\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n if (mongoose.models[modelName]) {\n return mongoose.models[modelName] as unknown as I_ModelWithSchema;\n }\n }\n }\n\n return currentModel;\n}\n\n/**\n * Applies string-based populate options (e.g., \"field.subfield\") to documents.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to populate\n * @param populatePath - The populate path string\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function applyStringPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populatePath: string,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: I_ModelWithSchema,\n): Promise<void> {\n const pathParts = populatePath.split('.');\n\n if (pathParts.length < 2) {\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n\n await populateNestedFieldOnParent(mongoose, docObj, populatePath, virtualConfigs, undefined, currentModel);\n }\n\n return;\n }\n\n const mainField = pathParts[0];\n\n if (!mainField || mainField.trim() === '') {\n return;\n }\n\n const nestedPath = pathParts.slice(1).join('.');\n\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n const mainValue = docObj[mainField];\n\n if (mainValue && typeof mainValue === 'object') {\n const nextModelForChildren = resolveChildModel(mongoose, currentModel, mainField, docObj);\n\n if (Array.isArray(mainValue)) {\n for (const item of mainValue) {\n if (item && typeof item === 'object') {\n await populateNestedFieldOnParent(mongoose, item as { [key: string]: unknown }, nestedPath, virtualConfigs, mainField, nextModelForChildren);\n }\n }\n }\n else if (mainValue && typeof mainValue === 'object') {\n await populateNestedFieldOnParent(mongoose, mainValue as { [key: string]: unknown }, nestedPath, virtualConfigs, mainField, nextModelForChildren);\n }\n }\n }\n}\n\n/**\n * Applies object-based populate options with nested populate to documents.\n *\n * @template T - The document type\n * @param mongoose - The Mongoose instance\n * @param documents - The documents to populate\n * @param populateOption - The populate option object\n * @param populateOption.path - The path to populate\n * @param populateOption.populate - The nested populate options\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function applyObjectPopulate<T extends object>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n populateOption: { path?: string; populate?: T_Input_Populate; [key: string]: unknown },\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n currentModel?: I_ModelWithSchema,\n): Promise<void> {\n const { path, populate: nestedPopulate } = populateOption;\n\n if (!path) {\n return;\n }\n\n if (!nestedPopulate) {\n await applyStringPopulate(mongoose, documents, path, virtualConfigs, currentModel);\n\n return;\n }\n\n const pathString = path;\n\n for (const doc of documents) {\n const docObj = doc as { [key: string]: unknown };\n const fieldValue = docObj[pathString];\n\n let nextModelForChildren: I_ModelWithSchema | undefined = currentModel;\n\n // Try resolving from schema virtuals\n if (currentModel?.schema?.virtuals) {\n const virtual = currentModel.schema.virtuals[pathString];\n\n if (virtual?.options?.ref) {\n const refResult = resolveRef(virtual.options.ref, docObj);\n if (refResult) {\n const modelName = convertEnumToModelName(refResult);\n if (mongoose.models[modelName]) {\n nextModelForChildren = mongoose.models[modelName] as unknown as I_ModelWithSchema;\n }\n }\n }\n }\n if (!nextModelForChildren && typeof fieldValue === 'object' && fieldValue && 'entityType' in (fieldValue as object)) {\n const maybeModel = convertEnumToModelName(String((fieldValue as { [k: string]: unknown })['entityType']));\n\n if (mongoose.models[maybeModel]) {\n nextModelForChildren = mongoose.models[maybeModel] as unknown as I_ModelWithSchema;\n }\n }\n\n if (fieldValue && typeof fieldValue === 'object') {\n if (Array.isArray(fieldValue)) {\n for (const item of fieldValue) {\n if (item && typeof item === 'object') {\n await applyNestedPopulate(mongoose, [item as { [key: string]: unknown }], nestedPopulate, virtualConfigs, nextModelForChildren);\n }\n }\n }\n else if (fieldValue && typeof fieldValue === 'object') {\n await applyNestedPopulate(mongoose, [fieldValue as { [key: string]: unknown }], nestedPopulate, virtualConfigs, nextModelForChildren);\n }\n }\n }\n}\n\n/**\n * Resolves the target model for a given populate path by following the path step by step.\n * This function traverses the schema hierarchy to find the correct model for population.\n *\n * @param mongoose - The Mongoose instance\n * @param startModel - The starting model (usually the model containing the document)\n * @param path - The populate path to resolve (e.g., \"entity.partner1.gallery\")\n * @param document - The document being populated (for dynamic virtual resolution)\n * @returns The resolved model name or undefined if not found\n */\nfunction resolveModelFromPath(\n mongoose: typeof mongooseRaw,\n startModel: I_ModelWithSchema,\n path: string,\n document: { [key: string]: unknown },\n): string | undefined {\n if (!path || !startModel || !startModel.schema) {\n return undefined;\n }\n\n const pathParts = path.split('.');\n let currentSchema = startModel.schema;\n\n for (let i = 0; i < pathParts.length; i++) {\n const pathPart = pathParts[i];\n\n if (currentSchema?.virtuals && pathPart) {\n const virtual = currentSchema.virtuals[pathPart];\n if (virtual?.options?.ref) {\n const refResult = resolveRef(virtual.options.ref, document);\n\n if (refResult && typeof refResult === 'string') {\n if (i === pathParts.length - 1) {\n return refResult;\n }\n\n const nextModel = mongoose.models[refResult];\n\n if (nextModel && (nextModel as unknown as I_ModelWithSchema).schema) {\n currentSchema = (nextModel as unknown as I_ModelWithSchema).schema!;\n continue;\n }\n }\n }\n }\n\n if (currentSchema?.paths && pathPart) {\n const pathSchema = currentSchema.paths[pathPart];\n\n if (pathSchema?.schema) {\n currentSchema = pathSchema.schema;\n continue;\n }\n }\n\n return undefined;\n }\n\n return undefined;\n}\n\n/**\n * Attempts to find a model whose schema contains the given field\n * either as a direct path or a virtual. Searches only at the root level\n * of each model schema.\n */\nfunction findModelBySchemaField(\n mongoose: typeof mongooseRaw,\n fieldName: string,\n): I_ModelWithSchema | undefined {\n if (!fieldName) {\n return undefined;\n }\n for (const modelName of Object.keys(mongoose.models)) {\n const Model = mongoose.models[modelName] as unknown as I_ModelWithSchema;\n const schema = Model?.schema;\n\n if (!schema) {\n continue;\n }\n if ((schema.paths && schema.paths[fieldName]) || (schema.virtuals && schema.virtuals[fieldName])) {\n return Model;\n }\n }\n\n return undefined;\n}\n\n/**\n * Finds a start model whose root schema contains the first segment of the path\n * as either a direct path or a virtual. Useful to jump into the correct model\n * when the current model context is misaligned (e.g., 'partner1.*' should start from User).\n */\nfunction findStartModelByFirstSegment(\n mongoose: typeof mongooseRaw,\n fullPath: string,\n): I_ModelWithSchema | undefined {\n const first = (fullPath || '').split('.')[0] || '';\n\n if (!first) {\n return undefined;\n }\n for (const modelName of Object.keys(mongoose.models)) {\n const Model = mongoose.models[modelName] as unknown as I_ModelWithSchema;\n const schema = Model?.schema;\n\n if (!schema) {\n continue;\n }\n if ((schema.paths && schema.paths[first]) || (schema.virtuals && schema.virtuals[first])) {\n return Model;\n }\n }\n\n return undefined;\n}\n\n/**\n * Populates a nested field on the parent object by finding the referenced document and applying populate options.\n *\n * @param mongoose - The Mongoose instance\n * @param document - The document containing the field to populate\n * @param nestedPath - The nested populate path\n * @param virtualConfigs - Optional virtual configurations for model inference\n * @param pathPrefix - The parent path prefix (if nested within another populated field)\n * @param currentModel - The current model context (which model's schema to search for virtuals)\n */\nasync function populateNestedFieldOnParent(\n mongoose: typeof mongooseRaw,\n document: { [key: string]: unknown },\n nestedPath: string,\n virtualConfigs?: I_DynamicVirtualConfig<unknown, string>[],\n pathPrefix?: string,\n currentModel?: I_ModelWithSchema,\n): Promise<void> {\n let modelName = document['__t'];\n\n if (!modelName) {\n if (currentModel) {\n const fullPath = pathPrefix ? `${pathPrefix}.${nestedPath}` : nestedPath;\n\n const firstSegment = (fullPath || '').split('.')[0] || '';\n let startModel: I_ModelWithSchema = currentModel;\n const hasFirstOnCurrent = Boolean(\n (startModel?.schema?.paths && startModel.schema.paths[firstSegment])\n || (startModel?.schema?.virtuals && startModel.schema.virtuals[firstSegment]),\n );\n\n if (!hasFirstOnCurrent) {\n const betterStart = findStartModelByFirstSegment(mongoose, fullPath);\n\n if (betterStart) {\n startModel = betterStart;\n }\n }\n\n const resolvedFromPath = resolveModelFromPath(mongoose, startModel, fullPath, document);\n\n if (resolvedFromPath) {\n modelName = resolvedFromPath;\n }\n else {\n const lastSegment = (fullPath.includes('.') ? fullPath.split('.').pop() : fullPath) || '';\n\n if (lastSegment) {\n const candidateModel = findModelBySchemaField(mongoose, lastSegment);\n\n if (candidateModel) {\n const rerun = resolveModelFromPath(mongoose, candidateModel, fullPath, document);\n\n if (rerun) {\n modelName = rerun;\n }\n }\n }\n }\n }\n\n if (virtualConfigs && virtualConfigs.length > 0) {\n const fieldName = nestedPath.split('.').pop() || '';\n const matchingVirtual = virtualConfigs.find(v => v.name === fieldName);\n\n if (matchingVirtual?.options.ref) {\n const refResult = resolveRef(matchingVirtual.options.ref as string | ((doc: unknown) => string | undefined), document);\n\n if (refResult && typeof refResult === 'string') {\n modelName = refResult;\n }\n }\n }\n\n if (!modelName) {\n for (const [key, value] of Object.entries(document as Record<string, unknown>)) {\n if (key === 'entityType' && typeof value === 'string') {\n modelName = value;\n break;\n }\n }\n\n if (modelName && mongoose.models[modelName as string]) {\n const Model = mongoose.models[modelName as string] as unknown as I_ModelWithSchema;\n\n if (Model?.schema) {\n const fieldName = nestedPath.split('.').pop() || '';\n const foundModelName = searchVirtualsInSchema(Model.schema, fieldName, document);\n if (foundModelName) {\n modelName = foundModelName;\n }\n }\n }\n }\n\n if (!modelName) {\n const fieldName = nestedPath.split('.').pop() || '';\n\n if (currentModel?.schema) {\n const foundModelName = searchVirtualsInSchema(currentModel.schema, fieldName, document);\n if (foundModelName) {\n modelName = foundModelName;\n }\n }\n\n if (!modelName) {\n if (currentModel) {\n const resolvedModel = resolveModelFromPath(mongoose, currentModel, nestedPath, document);\n\n if (resolvedModel) {\n modelName = resolvedModel;\n }\n }\n }\n\n if (!modelName && virtualConfigs && virtualConfigs.length > 0) {\n const matchingVirtual = virtualConfigs.find(v => v.name === fieldName);\n\n if (matchingVirtual?.options.ref) {\n const refResult = resolveRef(matchingVirtual.options.ref as string | ((doc: unknown) => string | undefined), document);\n\n if (refResult && typeof refResult === 'string') {\n modelName = refResult;\n }\n }\n }\n }\n\n if (!modelName) {\n const fieldName = nestedPath.split('.').pop() || '';\n const candidate = fieldName ? fieldName.charAt(0).toUpperCase() + fieldName.slice(1) : '';\n\n if (candidate && mongoose.models[candidate]) {\n modelName = candidate;\n }\n }\n }\n\n const Model = mongoose.models[modelName as string];\n\n if (!Model) {\n return;\n }\n\n const fieldIdKey = `${nestedPath}Id`;\n const fieldIdsKey = `${nestedPath}Ids`;\n\n const docIds = document[fieldIdKey] || document[fieldIdsKey] || document[nestedPath];\n\n if (!docIds) {\n return;\n }\n\n const idsArray = Array.isArray(docIds) ? docIds : [docIds];\n\n const populatedDocs = await Model.find({ id: { $in: idsArray } }).lean();\n\n if (populatedDocs.length > 0) {\n if (Array.isArray(docIds)) {\n document[nestedPath] = populatedDocs;\n }\n else {\n document[nestedPath] = populatedDocs[0];\n }\n }\n}\n"],"mappings":";;;AAoBA,eAAsB,EAClB,GACA,GACA,GACA,GACA,GACY;AACZ,KAAI,CAAC,EAAU,UAAU,CAAC,EACtB,QAAO;CAGX,IAAM,IAAgB,MAAM,QAAQ,EAAgB,GAAG,IAAkB,CAAC,EAAgB;AAE1F,MAAK,IAAM,KAAkB,EACzB,CAAI,OAAO,KAAmB,WAC1B,MAAM,EAAoB,GAAU,GAAW,GAAgB,GAAgB,EAAa,GAEvF,KAAkB,OAAO,KAAmB,YAGjD,MAAM,EAAoB,GAAU,GAFrB,GAEwC,GAAgB,EAAa;AAI5F,QAAO;;AAaX,SAAS,EACL,GACA,GACA,GACA,GAC6B;AAC7B,KAAI,CAAC,EACD;AAIJ,KAAI,EAAa,QAAQ,UAAU;EAC/B,IAAM,IAAU,EAAa,OAAO,SAAS;AAC7C,MAAI,GAAS,SAAS,KAAK;GACvB,IAAM,IAAY,EAAW,EAAQ,QAAQ,KAAK,EAAS;AAC3D,OAAI,GAAW;IACX,I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AAe,MAAK,MAAK,EAAE,SAAS,EAAU;AAEtE,QAAI,GAAiB,QAAQ,KAAK;KAC9B,IAAM,IAAY,EAAW,EAAgB,QAAQ,KAAwD,EAAS;AAEtH,KAAI,KAAa,OAAO,KAAc,aAClC,IAAY;;;;AAM5B,MAAI,CAAC,GAAW;GACZ,IAAM,IAAY,EAAW,MAAM,IAAI,CAAC,KAAK,IAAI,IAC3C,IAAY,IAAY,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,GAAG;AAEvF,GAAI,KAAa,EAAS,OAAO,OAC7B,IAAY;;;CAKxB,IAAM,IAAQ,EAAS,OAAO;AAE9B,KAAI,CAAC,EACD;CAGJ,IAAM,IAAa,GAAG,EAAW,KAC3B,IAAc,GAAG,EAAW,MAE5B,IAAS,EAAS,MAAe,EAAS,MAAgB,EAAS;AAEzE,KAAI,CAAC,EACD;CAKJ,IAAM,IAAgB,MAAM,EAAM,KAAK,EAAE,IAAI,EAAE,KAF9B,MAAM,QAAQ,EAAO,GAAG,IAAS,CAAC,EAAO,EAEI,EAAE,CAAC,CAAC,MAAM;AAExE,CAAI,EAAc,SAAS,MACnB,MAAM,QAAQ,EAAO,GACrB,EAAS,KAAc,IAGvB,EAAS,KAAc,EAAc"}
@@ -34,7 +34,7 @@ export type T_PreSaveMiddlewareFunction<T> = PreSaveMiddlewareFunction<T>;
34
34
  export type T_PostMiddlewareFunction<T> = PostMiddlewareFunction<T>;
35
35
  export type T_ErrorHandlingMiddlewareFunction<T> = ErrorHandlingMiddlewareFunction<T>;
36
36
  export type T_ErrorHandlingMiddlewareWithOption<T> = ErrorHandlingMiddlewareWithOption<T>;
37
- export type T_MongooseShema<T> = mongoose.Schema<T>;
37
+ export type T_MongooseSchema<T> = mongoose.Schema<T>;
38
38
  export type T_Input_MongooseSchema<T> = SchemaDefinition<T>;
39
39
  export type T_MongoosePlugin = (schema: Schema, options?: Record<string, unknown>) => void;
40
40
  export interface I_GenericDocument extends Partial<C_Document> {
@@ -1 +1 @@
1
- {"version":3,"file":"mongo.type.js","names":[],"sources":["../../../src/node/mongo/mongo.type.ts"],"sourcesContent":["import type {\n DeleteResult,\n Filter,\n InsertManyResult,\n InsertOneResult,\n OptionalUnlessRequiredId,\n UpdateResult,\n WithId,\n} from 'mongodb';\nimport type {\n AggregatePaginateModel,\n AggregatePaginateResult,\n ClientSession,\n ErrorHandlingMiddlewareFunction,\n ErrorHandlingMiddlewareWithOption,\n InsertManyOptions,\n PaginateModel,\n PaginateOptions,\n PaginateResult,\n PipelineStage,\n PopulateOption,\n PopulateOptions,\n PostMiddlewareFunction,\n PreMiddlewareFunction,\n PreSaveMiddlewareFunction,\n ProjectionType,\n QueryFilter,\n QueryOptions,\n QueryWithHelpers,\n Schema,\n SchemaDefinition,\n UpdateQuery,\n UpdateWriteOpResult,\n} from 'mongoose';\nimport type mongoose from 'mongoose';\n\nimport { Collection, Db } from 'mongodb';\nimport {\n Document,\n Model,\n} from 'mongoose';\n\nexport class C_Db extends Db { }\n\nexport type T_Filter<T> = Filter<T>;\n\nexport type T_InsertOneResult<T> = InsertOneResult<T>;\n\nexport type T_InsertManyResult<T> = InsertManyResult<T>;\n\nexport type T_UpdateResult = UpdateResult | UpdateWriteOpResult;\n\nexport type T_DeleteResult = DeleteResult;\n\nexport type T_WithId<T> = WithId<T>;\n\nexport type T_OptionalUnlessRequiredId<T> = OptionalUnlessRequiredId<T>;\n\nexport class C_Document extends Document { }\n\nexport class C_Model extends Model { }\n\nexport class C_Collection<\n T extends Partial<C_Document>,\n> extends Collection<T> { }\n\nexport interface I_ExtendedModel<T extends Partial<C_Document>>\n extends Model<T>, PaginateModel<T>,\n AggregatePaginateModel<T> { }\n\nexport type T_QueryFilter<T> = QueryFilter<T>;\n\nexport type T_ProjectionType<T> = ProjectionType<T>;\n\nexport type T_QueryOptions<T> = QueryOptions<T>;\n\nexport type T_PaginateOptions = PaginateOptions;\n\nexport type T_PopulateOption = PopulateOption;\n\nexport type T_PopulateOptions = PopulateOptions;\n\nexport type T_PipelineStage = PipelineStage;\n\nexport type T_PaginateResult<T> = PaginateResult<T>;\n\nexport type T_AggregatePaginateResult<T> = AggregatePaginateResult<T>;\n\nexport type T_InsertManyOptions = InsertManyOptions;\n\nexport type T_UpdateQuery<T> = UpdateQuery<T>;\n\nexport type T_QueryWithHelpers<T> = QueryWithHelpers<T, T>;\n\nexport type T_PreMiddlewareFunction<T> = PreMiddlewareFunction<T>;\n\nexport type T_PreSaveMiddlewareFunction<T> = PreSaveMiddlewareFunction<T>;\n\nexport type T_PostMiddlewareFunction<T> = PostMiddlewareFunction<T>;\n\nexport type T_ErrorHandlingMiddlewareFunction<T> = ErrorHandlingMiddlewareFunction<T>;\n\nexport type T_ErrorHandlingMiddlewareWithOption<T> = ErrorHandlingMiddlewareWithOption<T>;\n\nexport type T_MongooseShema<T> = mongoose.Schema<T>;\n\nexport type T_Input_MongooseSchema<T> = SchemaDefinition<T>;\n\nexport type T_MongoosePlugin = (schema: Schema, options?: Record<string, unknown>) => void;\n\nexport interface I_GenericDocument extends Partial<C_Document> {\n id: string;\n isDel: boolean;\n createdAt: string | Date;\n updatedAt: string | Date;\n}\n\nexport type T_Omit_Create = 'id' | 'isDel' | 'createdAt' | 'updatedAt';\n\nexport type T_Omit_Update = 'id' | 'createdAt' | 'updatedAt';\n\ninterface I_VirtualNestedOptions {\n [key: string]: I_VirtualNestedOptions | number | string | boolean;\n}\n\n/**\n * Function type for dynamically determining the reference model name.\n * Can return either a string (model name) or an enum value that represents the model.\n * The function can also return undefined, which will be handled gracefully.\n * This allows for optional properties in documents without requiring non-null assertions.\n *\n * @template T - The document type\n * @template R - The return type (string or enum)\n */\ntype T_DynamicRefFunction<T = unknown, R extends string = string> = (doc: T) => R | undefined;\n\ninterface I_VirtualBaseOptions {\n localField: string;\n foreignField: string;\n count?: boolean;\n justOne?: boolean;\n options?: I_VirtualNestedOptions;\n}\n\ninterface I_VirtualOptions extends I_VirtualBaseOptions {\n ref: string;\n}\n\nexport interface I_DynamicVirtualOptions<T, R extends string = string> extends I_VirtualBaseOptions {\n ref: T_DynamicRefFunction<T, R>;\n}\n\nexport type T_VirtualOptions<T, R extends string = string> = I_VirtualOptions | I_DynamicVirtualOptions<T, R>;\n\nexport interface I_DynamicVirtualConfig<T, R extends string = string> {\n name: string;\n options: I_DynamicVirtualOptions<T, R>;\n}\n\ninterface I_MongooseOptions<T, R extends string = string> {\n mongoose: typeof mongoose;\n virtuals?: {\n name: keyof T | string;\n options?: T_VirtualOptions<T, R>;\n get?: (this: T) => void;\n }[];\n}\n\nexport interface I_CreateSchemaOptions<T, R extends string = string>\n extends I_MongooseOptions<T, R> {\n schema: T_Input_MongooseSchema<T>;\n standalone?: boolean;\n}\n\nexport type T_MongooseMiddlewareMethod = string | RegExp;\n\nexport type T_MongooseMiddlewarePreFunction<T> = T_PreMiddlewareFunction<T> & T_PreSaveMiddlewareFunction<T>;\n\nexport type T_MongooseMiddlewarePostFunction<T> = T_PostMiddlewareFunction<T> & T_ErrorHandlingMiddlewareFunction<T> & T_ErrorHandlingMiddlewareWithOption<T>;\n\nexport interface I_MongooseModelMiddleware<T extends Partial<C_Document>> {\n method: T_MongooseMiddlewareMethod;\n pre?: T_MongooseMiddlewarePreFunction<T & T_QueryWithHelpers<T>>;\n post?: T_MongooseMiddlewarePostFunction<T>;\n}\n\nexport interface I_CreateModelOptions<T extends Partial<C_Document>, R extends string = string>\n extends I_MongooseOptions<T, R> {\n schema: T_Input_MongooseSchema<T>;\n name: string;\n pagination?: boolean;\n aggregate?: boolean;\n middlewares?: I_MongooseModelMiddleware<T>[];\n}\n\nexport type T_Input_Populate = string | string[] | T_PopulateOptions | T_PopulateOptions[];\n\nexport interface I_PaginateOptionsWithPopulate\n extends T_PaginateOptions,\n Omit<T_PopulateOption, 'populate'> {\n populate?: T_Input_Populate;\n}\n\nexport interface I_Input_FindOne<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n projection?: T_ProjectionType<T>;\n options?: T_QueryOptions<T>;\n}\n\nexport interface I_Input_FindAll<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n projection?: T_ProjectionType<T>;\n options?: T_QueryOptions<T>;\n}\n\nexport interface I_Input_FindPaging<T = undefined> {\n filter?: T_QueryFilter<T>;\n options?: I_PaginateOptionsWithPopulate;\n};\n\nexport interface I_Input_FindPagingAggregate {\n pipeline: T_PipelineStage[];\n options?: I_PaginateOptionsWithPopulate;\n}\n\nexport interface I_Input_CreateOne<T> {\n doc: T;\n}\n\nexport interface I_Input_CreateMany<T> {\n docs: T[];\n}\n\nexport interface I_UpdateOptionsExtended extends Omit<QueryOptions, 'session'> {\n session?: ClientSession;\n}\n\nexport interface I_Input_UpdateOne<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n update: T_UpdateQuery<T>;\n options?: I_UpdateOptionsExtended;\n}\n\nexport interface I_Input_UpdateMany<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n update: T_UpdateQuery<T>;\n options?: I_UpdateOptionsExtended;\n}\n\nexport interface I_DeleteOptionsExtended extends Omit<QueryOptions, 'session'> {\n session?: ClientSession;\n}\n\nexport interface I_Input_DeleteOne<T> {\n filter: T_QueryFilter<T>;\n options?: I_DeleteOptionsExtended;\n}\n\nexport interface I_Input_DeleteMany<T> {\n filter: T_QueryFilter<T>;\n options?: I_DeleteOptionsExtended;\n}\n\nexport interface I_InputSlug<T> {\n field: string;\n filter?: T_QueryFilter<T>;\n haveHistory?: boolean;\n}\n\nexport interface I_Input_CreateSlug<T> extends I_InputSlug<T> {\n from: T;\n}\n\nexport interface I_Input_CheckSlug<T> extends I_InputSlug<T> {\n slug: string;\n from: T;\n}\n\nexport interface I_Input_GenerateSlug<T> extends I_InputSlug<T> {\n slug: string;\n isObject: boolean;\n haveHistory?: boolean;\n}\n"],"mappings":";;;AA0CA,IAAa,IAAb,cAA0B,EAAG,IAgBhB,IAAb,cAAgC,EAAS,IAE5B,IAAb,cAA6B,EAAM,IAEtB,IAAb,cAEU,EAAc"}
1
+ {"version":3,"file":"mongo.type.js","names":[],"sources":["../../../src/node/mongo/mongo.type.ts"],"sourcesContent":["import type {\n DeleteResult,\n Filter,\n InsertManyResult,\n InsertOneResult,\n OptionalUnlessRequiredId,\n UpdateResult,\n WithId,\n} from 'mongodb';\nimport type {\n AggregatePaginateModel,\n AggregatePaginateResult,\n ClientSession,\n ErrorHandlingMiddlewareFunction,\n ErrorHandlingMiddlewareWithOption,\n InsertManyOptions,\n PaginateModel,\n PaginateOptions,\n PaginateResult,\n PipelineStage,\n PopulateOption,\n PopulateOptions,\n PostMiddlewareFunction,\n PreMiddlewareFunction,\n PreSaveMiddlewareFunction,\n ProjectionType,\n QueryFilter,\n QueryOptions,\n QueryWithHelpers,\n Schema,\n SchemaDefinition,\n UpdateQuery,\n UpdateWriteOpResult,\n} from 'mongoose';\nimport type mongoose from 'mongoose';\n\nimport { Collection, Db } from 'mongodb';\nimport {\n Document,\n Model,\n} from 'mongoose';\n\nexport class C_Db extends Db { }\n\nexport type T_Filter<T> = Filter<T>;\n\nexport type T_InsertOneResult<T> = InsertOneResult<T>;\n\nexport type T_InsertManyResult<T> = InsertManyResult<T>;\n\nexport type T_UpdateResult = UpdateResult | UpdateWriteOpResult;\n\nexport type T_DeleteResult = DeleteResult;\n\nexport type T_WithId<T> = WithId<T>;\n\nexport type T_OptionalUnlessRequiredId<T> = OptionalUnlessRequiredId<T>;\n\nexport class C_Document extends Document { }\n\nexport class C_Model extends Model { }\n\nexport class C_Collection<\n T extends Partial<C_Document>,\n> extends Collection<T> { }\n\nexport interface I_ExtendedModel<T extends Partial<C_Document>>\n extends Model<T>, PaginateModel<T>,\n AggregatePaginateModel<T> { }\n\nexport type T_QueryFilter<T> = QueryFilter<T>;\n\nexport type T_ProjectionType<T> = ProjectionType<T>;\n\nexport type T_QueryOptions<T> = QueryOptions<T>;\n\nexport type T_PaginateOptions = PaginateOptions;\n\nexport type T_PopulateOption = PopulateOption;\n\nexport type T_PopulateOptions = PopulateOptions;\n\nexport type T_PipelineStage = PipelineStage;\n\nexport type T_PaginateResult<T> = PaginateResult<T>;\n\nexport type T_AggregatePaginateResult<T> = AggregatePaginateResult<T>;\n\nexport type T_InsertManyOptions = InsertManyOptions;\n\nexport type T_UpdateQuery<T> = UpdateQuery<T>;\n\nexport type T_QueryWithHelpers<T> = QueryWithHelpers<T, T>;\n\nexport type T_PreMiddlewareFunction<T> = PreMiddlewareFunction<T>;\n\nexport type T_PreSaveMiddlewareFunction<T> = PreSaveMiddlewareFunction<T>;\n\nexport type T_PostMiddlewareFunction<T> = PostMiddlewareFunction<T>;\n\nexport type T_ErrorHandlingMiddlewareFunction<T> = ErrorHandlingMiddlewareFunction<T>;\n\nexport type T_ErrorHandlingMiddlewareWithOption<T> = ErrorHandlingMiddlewareWithOption<T>;\n\nexport type T_MongooseSchema<T> = mongoose.Schema<T>;\n\nexport type T_Input_MongooseSchema<T> = SchemaDefinition<T>;\n\nexport type T_MongoosePlugin = (schema: Schema, options?: Record<string, unknown>) => void;\n\nexport interface I_GenericDocument extends Partial<C_Document> {\n id: string;\n isDel: boolean;\n createdAt: string | Date;\n updatedAt: string | Date;\n}\n\nexport type T_Omit_Create = 'id' | 'isDel' | 'createdAt' | 'updatedAt';\n\nexport type T_Omit_Update = 'id' | 'createdAt' | 'updatedAt';\n\ninterface I_VirtualNestedOptions {\n [key: string]: I_VirtualNestedOptions | number | string | boolean;\n}\n\n/**\n * Function type for dynamically determining the reference model name.\n * Can return either a string (model name) or an enum value that represents the model.\n * The function can also return undefined, which will be handled gracefully.\n * This allows for optional properties in documents without requiring non-null assertions.\n *\n * @template T - The document type\n * @template R - The return type (string or enum)\n */\ntype T_DynamicRefFunction<T = unknown, R extends string = string> = (doc: T) => R | undefined;\n\ninterface I_VirtualBaseOptions {\n localField: string;\n foreignField: string;\n count?: boolean;\n justOne?: boolean;\n options?: I_VirtualNestedOptions;\n}\n\ninterface I_VirtualOptions extends I_VirtualBaseOptions {\n ref: string;\n}\n\nexport interface I_DynamicVirtualOptions<T, R extends string = string> extends I_VirtualBaseOptions {\n ref: T_DynamicRefFunction<T, R>;\n}\n\nexport type T_VirtualOptions<T, R extends string = string> = I_VirtualOptions | I_DynamicVirtualOptions<T, R>;\n\nexport interface I_DynamicVirtualConfig<T, R extends string = string> {\n name: string;\n options: I_DynamicVirtualOptions<T, R>;\n}\n\ninterface I_MongooseOptions<T, R extends string = string> {\n mongoose: typeof mongoose;\n virtuals?: {\n name: keyof T | string;\n options?: T_VirtualOptions<T, R>;\n get?: (this: T) => void;\n }[];\n}\n\nexport interface I_CreateSchemaOptions<T, R extends string = string>\n extends I_MongooseOptions<T, R> {\n schema: T_Input_MongooseSchema<T>;\n standalone?: boolean;\n}\n\nexport type T_MongooseMiddlewareMethod = string | RegExp;\n\nexport type T_MongooseMiddlewarePreFunction<T> = T_PreMiddlewareFunction<T> & T_PreSaveMiddlewareFunction<T>;\n\nexport type T_MongooseMiddlewarePostFunction<T> = T_PostMiddlewareFunction<T> & T_ErrorHandlingMiddlewareFunction<T> & T_ErrorHandlingMiddlewareWithOption<T>;\n\nexport interface I_MongooseModelMiddleware<T extends Partial<C_Document>> {\n method: T_MongooseMiddlewareMethod;\n pre?: T_MongooseMiddlewarePreFunction<T & T_QueryWithHelpers<T>>;\n post?: T_MongooseMiddlewarePostFunction<T>;\n}\n\nexport interface I_CreateModelOptions<T extends Partial<C_Document>, R extends string = string>\n extends I_MongooseOptions<T, R> {\n schema: T_Input_MongooseSchema<T>;\n name: string;\n pagination?: boolean;\n aggregate?: boolean;\n middlewares?: I_MongooseModelMiddleware<T>[];\n}\n\nexport type T_Input_Populate = string | string[] | T_PopulateOptions | T_PopulateOptions[];\n\nexport interface I_PaginateOptionsWithPopulate\n extends T_PaginateOptions,\n Omit<T_PopulateOption, 'populate'> {\n populate?: T_Input_Populate;\n}\n\nexport interface I_Input_FindOne<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n projection?: T_ProjectionType<T>;\n options?: T_QueryOptions<T>;\n}\n\nexport interface I_Input_FindAll<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n projection?: T_ProjectionType<T>;\n options?: T_QueryOptions<T>;\n}\n\nexport interface I_Input_FindPaging<T = undefined> {\n filter?: T_QueryFilter<T>;\n options?: I_PaginateOptionsWithPopulate;\n};\n\nexport interface I_Input_FindPagingAggregate {\n pipeline: T_PipelineStage[];\n options?: I_PaginateOptionsWithPopulate;\n}\n\nexport interface I_Input_CreateOne<T> {\n doc: T;\n}\n\nexport interface I_Input_CreateMany<T> {\n docs: T[];\n}\n\nexport interface I_UpdateOptionsExtended extends Omit<QueryOptions, 'session'> {\n session?: ClientSession;\n}\n\nexport interface I_Input_UpdateOne<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n update: T_UpdateQuery<T>;\n options?: I_UpdateOptionsExtended;\n}\n\nexport interface I_Input_UpdateMany<T> extends T_PopulateOption {\n filter: T_QueryFilter<T>;\n update: T_UpdateQuery<T>;\n options?: I_UpdateOptionsExtended;\n}\n\nexport interface I_DeleteOptionsExtended extends Omit<QueryOptions, 'session'> {\n session?: ClientSession;\n}\n\nexport interface I_Input_DeleteOne<T> {\n filter: T_QueryFilter<T>;\n options?: I_DeleteOptionsExtended;\n}\n\nexport interface I_Input_DeleteMany<T> {\n filter: T_QueryFilter<T>;\n options?: I_DeleteOptionsExtended;\n}\n\nexport interface I_InputSlug<T> {\n field: string;\n filter?: T_QueryFilter<T>;\n haveHistory?: boolean;\n}\n\nexport interface I_Input_CreateSlug<T> extends I_InputSlug<T> {\n from: T;\n}\n\nexport interface I_Input_CheckSlug<T> extends I_InputSlug<T> {\n slug: string;\n from: T;\n}\n\nexport interface I_Input_GenerateSlug<T> extends I_InputSlug<T> {\n slug: string;\n isObject: boolean;\n haveHistory?: boolean;\n}\n"],"mappings":";;;AA0CA,IAAa,IAAb,cAA0B,EAAG,IAgBhB,IAAb,cAAgC,EAAS,IAE5B,IAAb,cAA6B,EAAM,IAEtB,IAAb,cAEU,EAAc"}
@@ -1,7 +1,7 @@
1
1
  import { default as migrate } from 'migrate-mongo';
2
2
  import { default as mongooseRaw } from 'mongoose';
3
3
  import { MongoController } from './mongo.controller.js';
4
- import { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_MongoosePlugin, T_MongooseShema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';
4
+ import { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_MongoosePlugin, T_MongooseSchema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';
5
5
  /**
6
6
  * Converts enum values to proper model names.
7
7
  * Handles common naming conventions like converting 'USER' to 'User'.
@@ -16,10 +16,10 @@ export declare function convertEnumToModelName(enumValue: string): string;
16
16
  */
17
17
  interface I_MongoUtils {
18
18
  createGenericFields: () => I_GenericDocument;
19
- applyPlugins: <T>(schema: T_MongooseShema<T>, plugins: Array<T_MongoosePlugin | false>) => void;
20
- applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseShema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;
21
- createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseShema<I_GenericDocument>;
22
- createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseShema<T>;
19
+ applyPlugins: <T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) => void;
20
+ applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseSchema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;
21
+ createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseSchema<I_GenericDocument>;
22
+ createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseSchema<T>;
23
23
  createModel: <T extends Partial<C_Document>, R extends string = string>(options: I_CreateModelOptions<T, R>) => I_ExtendedModel<T>;
24
24
  validator: {
25
25
  isRequired: <T>() => (this: T, value: unknown) => Promise<boolean>;
@@ -40,8 +40,8 @@ interface I_MongoUtils {
40
40
  };
41
41
  regexify: <T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]) => T_QueryFilter<T>;
42
42
  isDynamicVirtual: <T, R extends string>(options?: T_VirtualOptions<T, R>) => options is I_DynamicVirtualOptions<T, R>;
43
- getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean) => Promise<T[]>;
44
- getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean) => Promise<T_WithId<T>[]>;
43
+ getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: Record<string, unknown>) => Promise<T[]>;
44
+ getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: Record<string, unknown>) => Promise<T_WithId<T>[]>;
45
45
  }
46
46
  /**
47
47
  * MongoDB utility object providing comprehensive database operations and utilities.