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