@cyberskill/shared 2.5.0 → 2.7.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/index.cjs +1 -1
- package/dist/node/mongo/index.js +11 -10
- package/dist/node/mongo/mongo.constant.cjs +1 -1
- package/dist/node/mongo/mongo.constant.d.ts +6 -0
- package/dist/node/mongo/mongo.constant.js +3 -2
- package/dist/node/mongo/mongo.type.d.ts +2 -0
- package/dist/node/mongo/mongo.util.cjs +4 -4
- package/dist/node/mongo/mongo.util.d.ts +9 -9
- package/dist/node/mongo/mongo.util.js +315 -304
- package/dist/util/index.cjs +1 -1
- package/dist/util/index.js +16 -15
- package/dist/util/object/index.cjs +1 -1
- package/dist/util/object/index.js +6 -5
- package/dist/util/object/object.util.cjs +1 -1
- package/dist/util/object/object.util.d.ts +17 -0
- package/dist/util/object/object.util.js +48 -32
- package/package.json +2 -2
|
@@ -1,44 +1,45 @@
|
|
|
1
1
|
import { cloneDeep as B, isObject as $ } from "lodash-es";
|
|
2
|
-
import
|
|
2
|
+
import Q from "migrate-mongo";
|
|
3
3
|
import { Document as W } from "mongoose";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import { v4 as
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
4
|
+
import k from "mongoose-aggregate-paginate-v2";
|
|
5
|
+
import J from "mongoose-paginate-v2";
|
|
6
|
+
import { v4 as G } from "uuid";
|
|
7
|
+
import { MONGO_SLUG_MAX_ATTEMPTS as X } from "./mongo.constant.js";
|
|
8
|
+
import { getNestedValue as H, setNestedValue as Y, normalizeMongoFilter as b } from "../../util/object/object.util.js";
|
|
9
|
+
import { regexSearchMapper as Z } from "../../util/common/common.util.js";
|
|
10
|
+
import { writeFileSync as q, pathExistsSync as ee, readFileSync as te, appendFileSync as re } from "../fs/fs.util.js";
|
|
11
|
+
import { PATH as w, MIGRATE_MONGO_CONFIG as L } from "../path/path.constant.js";
|
|
12
|
+
import { validate as se } from "../../util/validate/validate.util.js";
|
|
13
|
+
import { generateShortId as ne, generateSlug as I } from "../../util/string/string.util.js";
|
|
14
|
+
import { RESPONSE_STATUS as R } from "../../constant/response-status.js";
|
|
14
15
|
import { catchError as d } from "../log/log.util.js";
|
|
15
|
-
var
|
|
16
|
+
var ue = Object.defineProperty, ce = Object.defineProperties, ie = Object.getOwnPropertyDescriptors, U = Object.getOwnPropertySymbols, oe = Object.prototype.hasOwnProperty, le = Object.prototype.propertyIsEnumerable, T = (n, t, e) => t in n ? ue(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, S = (n, t) => {
|
|
16
17
|
for (var e in t || (t = {}))
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
for (var e of
|
|
20
|
-
|
|
21
|
-
return
|
|
22
|
-
},
|
|
23
|
-
var u = (
|
|
18
|
+
oe.call(t, e) && T(n, e, t[e]);
|
|
19
|
+
if (U)
|
|
20
|
+
for (var e of U(t))
|
|
21
|
+
le.call(t, e) && T(n, e, t[e]);
|
|
22
|
+
return n;
|
|
23
|
+
}, F = (n, t) => ce(n, ie(t)), ae = (n, t, e) => T(n, typeof t != "symbol" ? t + "" : t, e), a = (n, t, e) => new Promise((r, s) => {
|
|
24
|
+
var u = (o) => {
|
|
24
25
|
try {
|
|
25
|
-
|
|
26
|
-
} catch (
|
|
27
|
-
|
|
26
|
+
i(e.next(o));
|
|
27
|
+
} catch (l) {
|
|
28
|
+
s(l);
|
|
28
29
|
}
|
|
29
|
-
}, c = (
|
|
30
|
+
}, c = (o) => {
|
|
30
31
|
try {
|
|
31
|
-
|
|
32
|
-
} catch (
|
|
33
|
-
|
|
32
|
+
i(e.throw(o));
|
|
33
|
+
} catch (l) {
|
|
34
|
+
s(l);
|
|
34
35
|
}
|
|
35
|
-
},
|
|
36
|
-
|
|
36
|
+
}, i = (o) => o.done ? r(o.value) : Promise.resolve(o.value).then(u, c);
|
|
37
|
+
i((e = e.apply(n, t)).next());
|
|
37
38
|
});
|
|
38
|
-
function
|
|
39
|
-
return
|
|
39
|
+
function de(n) {
|
|
40
|
+
return n === n.toUpperCase() ? n.charAt(0).toUpperCase() + n.slice(1).toLowerCase() : n;
|
|
40
41
|
}
|
|
41
|
-
const
|
|
42
|
+
const x = {
|
|
42
43
|
/**
|
|
43
44
|
* Creates generic fields that are commonly used across MongoDB documents.
|
|
44
45
|
* This function generates standard fields including a UUID, deletion flag, and timestamps
|
|
@@ -48,7 +49,7 @@ const V = {
|
|
|
48
49
|
*/
|
|
49
50
|
createGenericFields() {
|
|
50
51
|
return {
|
|
51
|
-
id:
|
|
52
|
+
id: G(),
|
|
52
53
|
isDel: !1,
|
|
53
54
|
createdAt: /* @__PURE__ */ new Date(),
|
|
54
55
|
updatedAt: /* @__PURE__ */ new Date()
|
|
@@ -62,8 +63,8 @@ const V = {
|
|
|
62
63
|
* @param schema - The Mongoose schema to apply plugins to.
|
|
63
64
|
* @param plugins - An array of plugin functions or false values to filter and apply.
|
|
64
65
|
*/
|
|
65
|
-
applyPlugins(
|
|
66
|
-
t.filter((e) => typeof e == "function").forEach((e) =>
|
|
66
|
+
applyPlugins(n, t) {
|
|
67
|
+
t.filter((e) => typeof e == "function").forEach((e) => n.plugin(e));
|
|
67
68
|
},
|
|
68
69
|
/**
|
|
69
70
|
* Applies middleware functions to a Mongoose schema.
|
|
@@ -72,9 +73,9 @@ const V = {
|
|
|
72
73
|
* @param schema - The Mongoose schema to apply middleware to.
|
|
73
74
|
* @param middlewares - An array of middleware configurations with method, pre, and post functions.
|
|
74
75
|
*/
|
|
75
|
-
applyMiddlewares(
|
|
76
|
-
t.forEach(({ method: e, pre: r, post:
|
|
77
|
-
e && r &&
|
|
76
|
+
applyMiddlewares(n, t) {
|
|
77
|
+
t.forEach(({ method: e, pre: r, post: s }) => {
|
|
78
|
+
e && r && n.pre(e, r), e && s && n.post(e, s);
|
|
78
79
|
});
|
|
79
80
|
},
|
|
80
81
|
/**
|
|
@@ -85,10 +86,10 @@ const V = {
|
|
|
85
86
|
* @param mongoose - The Mongoose instance to create the schema with.
|
|
86
87
|
* @returns A Mongoose schema with generic document fields.
|
|
87
88
|
*/
|
|
88
|
-
createGenericSchema(
|
|
89
|
-
return new
|
|
89
|
+
createGenericSchema(n) {
|
|
90
|
+
return new n.Schema(
|
|
90
91
|
{
|
|
91
|
-
id: { type: String, default:
|
|
92
|
+
id: { type: String, default: G, unique: !0 },
|
|
92
93
|
isDel: { type: Boolean, default: !1 }
|
|
93
94
|
},
|
|
94
95
|
{ timestamps: !0 }
|
|
@@ -107,34 +108,34 @@ const V = {
|
|
|
107
108
|
* @returns A configured Mongoose schema.
|
|
108
109
|
*/
|
|
109
110
|
createSchema({
|
|
110
|
-
mongoose:
|
|
111
|
+
mongoose: n,
|
|
111
112
|
schema: t,
|
|
112
113
|
virtuals: e = [],
|
|
113
114
|
standalone: r = !1
|
|
114
115
|
}) {
|
|
115
|
-
const
|
|
116
|
+
const s = new n.Schema(t, {
|
|
116
117
|
toJSON: { virtuals: !0 },
|
|
117
118
|
// So `res.json()` and other `JSON.stringify()` functions include virtuals
|
|
118
119
|
toObject: { virtuals: !0 }
|
|
119
120
|
// So `console.log()` and other functions that use `toObject()` include virtuals
|
|
120
121
|
});
|
|
121
|
-
return e.forEach(({ name: u, options: c, get:
|
|
122
|
-
if (
|
|
123
|
-
const
|
|
124
|
-
|
|
122
|
+
return e.forEach(({ name: u, options: c, get: i }) => {
|
|
123
|
+
if (x.isDynamicVirtual(c)) {
|
|
124
|
+
const o = s.statics;
|
|
125
|
+
o._dynamicVirtuals || (o._dynamicVirtuals = []), o._dynamicVirtuals.push({
|
|
125
126
|
name: u,
|
|
126
127
|
options: c
|
|
127
128
|
});
|
|
128
|
-
const
|
|
129
|
-
|
|
129
|
+
const l = s.virtual(u);
|
|
130
|
+
i ? l.get(i) : l.get(function() {
|
|
130
131
|
var f;
|
|
131
132
|
return ((f = this._populated) == null ? void 0 : f[u]) || (c != null && c.count ? 0 : c != null && c.justOne ? null : []);
|
|
132
133
|
});
|
|
133
134
|
} else {
|
|
134
|
-
const
|
|
135
|
-
|
|
135
|
+
const o = s.virtual(u, c);
|
|
136
|
+
i && o.get(i);
|
|
136
137
|
}
|
|
137
|
-
}), r ||
|
|
138
|
+
}), r || s.add(x.createGenericSchema(n)), s;
|
|
138
139
|
},
|
|
139
140
|
/**
|
|
140
141
|
* Creates a Mongoose model with plugins, middleware, and pagination support.
|
|
@@ -153,23 +154,23 @@ const V = {
|
|
|
153
154
|
* @throws {Error} When the model name is not provided.
|
|
154
155
|
*/
|
|
155
156
|
createModel({
|
|
156
|
-
mongoose:
|
|
157
|
+
mongoose: n,
|
|
157
158
|
name: t,
|
|
158
159
|
schema: e,
|
|
159
160
|
virtuals: r = [],
|
|
160
|
-
pagination:
|
|
161
|
+
pagination: s = !0,
|
|
161
162
|
aggregate: u = !0,
|
|
162
163
|
middlewares: c = []
|
|
163
164
|
}) {
|
|
164
165
|
if (!t)
|
|
165
166
|
throw new Error("Model name is required.");
|
|
166
|
-
if (
|
|
167
|
-
return
|
|
168
|
-
const
|
|
169
|
-
return (
|
|
170
|
-
|
|
171
|
-
u &&
|
|
172
|
-
]),
|
|
167
|
+
if (n.models[t])
|
|
168
|
+
return n.models[t];
|
|
169
|
+
const i = x.createSchema({ mongoose: n, schema: e, virtuals: r });
|
|
170
|
+
return (s || u) && x.applyPlugins(i, [
|
|
171
|
+
s && J,
|
|
172
|
+
u && k
|
|
173
|
+
]), x.applyMiddlewares(i, c), n.model(t, i);
|
|
173
174
|
},
|
|
174
175
|
/**
|
|
175
176
|
* Validation utilities for Mongoose schemas.
|
|
@@ -184,9 +185,9 @@ const V = {
|
|
|
184
185
|
* @returns A validation function that returns true if the field is not empty.
|
|
185
186
|
*/
|
|
186
187
|
isRequired() {
|
|
187
|
-
return function(
|
|
188
|
-
return
|
|
189
|
-
return !
|
|
188
|
+
return function(n) {
|
|
189
|
+
return a(this, null, function* () {
|
|
190
|
+
return !se.isEmpty(n);
|
|
190
191
|
});
|
|
191
192
|
};
|
|
192
193
|
},
|
|
@@ -199,12 +200,12 @@ const V = {
|
|
|
199
200
|
* @returns A validation function that returns true if the value is unique across the specified fields.
|
|
200
201
|
* @throws {Error} When fields is not a non-empty array of strings.
|
|
201
202
|
*/
|
|
202
|
-
isUnique(
|
|
203
|
+
isUnique(n) {
|
|
203
204
|
return function(t) {
|
|
204
|
-
return
|
|
205
|
-
if (!Array.isArray(
|
|
205
|
+
return a(this, null, function* () {
|
|
206
|
+
if (!Array.isArray(n) || n.length === 0)
|
|
206
207
|
throw new Error("Fields must be a non-empty array of strings.");
|
|
207
|
-
const e = { $or:
|
|
208
|
+
const e = { $or: n.map((s) => ({ [s]: t })) };
|
|
208
209
|
return !(yield this.constructor.exists(e));
|
|
209
210
|
});
|
|
210
211
|
};
|
|
@@ -218,12 +219,12 @@ const V = {
|
|
|
218
219
|
* @returns A validation function that returns true if the value matches all regex patterns.
|
|
219
220
|
* @throws {Error} When regexArray is not an array of valid RegExp objects.
|
|
220
221
|
*/
|
|
221
|
-
matchesRegex(
|
|
222
|
+
matchesRegex(n) {
|
|
222
223
|
return function(t) {
|
|
223
|
-
return
|
|
224
|
-
if (!Array.isArray(
|
|
224
|
+
return a(this, null, function* () {
|
|
225
|
+
if (!Array.isArray(n) || n.some((e) => !(e instanceof RegExp)))
|
|
225
226
|
throw new Error("regexArray must be an array of valid RegExp objects.");
|
|
226
|
-
return
|
|
227
|
+
return n.every((e) => e.test(t));
|
|
227
228
|
});
|
|
228
229
|
};
|
|
229
230
|
}
|
|
@@ -232,7 +233,7 @@ const V = {
|
|
|
232
233
|
* Migration utilities for MongoDB.
|
|
233
234
|
* This object extends the migrate-mongo library with additional configuration utilities.
|
|
234
235
|
*/
|
|
235
|
-
migrate:
|
|
236
|
+
migrate: F(S({}, Q), {
|
|
236
237
|
/**
|
|
237
238
|
* Sets the migration configuration and updates .gitignore.
|
|
238
239
|
* This function creates a migration configuration file and ensures it's properly
|
|
@@ -240,15 +241,15 @@ const V = {
|
|
|
240
241
|
*
|
|
241
242
|
* @param options - Migration configuration options to write to the config file.
|
|
242
243
|
*/
|
|
243
|
-
setConfig: (
|
|
244
|
+
setConfig: (n) => {
|
|
244
245
|
const t = `// This file is automatically generated by the Cyberskill CLI.
|
|
245
|
-
module.exports = ${JSON.stringify(
|
|
246
|
-
|
|
246
|
+
module.exports = ${JSON.stringify(n, null, 4)}`;
|
|
247
|
+
q(w.MIGRATE_MONGO_CONFIG, t);
|
|
247
248
|
const e = `
|
|
248
|
-
${
|
|
249
|
+
${L}
|
|
249
250
|
`;
|
|
250
|
-
|
|
251
|
-
`).includes(
|
|
251
|
+
ee(w.GIT_IGNORE) ? te(w.GIT_IGNORE, "utf-8").split(`
|
|
252
|
+
`).includes(L) || re(w.GIT_IGNORE, e) : q(w.GIT_IGNORE, e);
|
|
252
253
|
}
|
|
253
254
|
}),
|
|
254
255
|
/**
|
|
@@ -260,20 +261,20 @@ ${q}
|
|
|
260
261
|
* @param fields - An array of field names to convert to regex patterns.
|
|
261
262
|
* @returns A new filter object with string values converted to regex patterns.
|
|
262
263
|
*/
|
|
263
|
-
regexify(
|
|
264
|
-
if (!
|
|
264
|
+
regexify(n, t) {
|
|
265
|
+
if (!n)
|
|
265
266
|
return {};
|
|
266
|
-
let e = B(
|
|
267
|
+
let e = B(n);
|
|
267
268
|
if (!t || t.length === 0)
|
|
268
269
|
return e;
|
|
269
270
|
for (const r of t) {
|
|
270
|
-
const
|
|
271
|
+
const s = r.toString().split("."), u = H(e, s);
|
|
271
272
|
if (typeof u == "string" && u.length > 0) {
|
|
272
273
|
const c = {
|
|
273
|
-
$regex: `.*${
|
|
274
|
+
$regex: `.*${Z(u)}.*`,
|
|
274
275
|
$options: "i"
|
|
275
276
|
};
|
|
276
|
-
e =
|
|
277
|
+
e = Y(e, s, c);
|
|
277
278
|
}
|
|
278
279
|
}
|
|
279
280
|
return e;
|
|
@@ -284,8 +285,8 @@ ${q}
|
|
|
284
285
|
* @param options - The virtual options to check.
|
|
285
286
|
* @returns True if the options contain a dynamic ref function.
|
|
286
287
|
*/
|
|
287
|
-
isDynamicVirtual(
|
|
288
|
-
return !!(
|
|
288
|
+
isDynamicVirtual(n) {
|
|
289
|
+
return !!(n && typeof n.ref == "function");
|
|
289
290
|
},
|
|
290
291
|
/**
|
|
291
292
|
* Generic utility function to get new records from the database
|
|
@@ -294,9 +295,9 @@ ${q}
|
|
|
294
295
|
* @param filterFn - Function to determine if a record already exists
|
|
295
296
|
* @returns Array of records that don't exist in the database
|
|
296
297
|
*/
|
|
297
|
-
getNewRecords(
|
|
298
|
-
return
|
|
299
|
-
const r = yield
|
|
298
|
+
getNewRecords(n, t, e) {
|
|
299
|
+
return a(this, null, function* () {
|
|
300
|
+
const r = yield n.findAll({});
|
|
300
301
|
return r.success ? t.filter(
|
|
301
302
|
(u) => !r.result.some(
|
|
302
303
|
(c) => e(c, u)
|
|
@@ -311,9 +312,9 @@ ${q}
|
|
|
311
312
|
* @param filterFn - Function to determine if a record exists
|
|
312
313
|
* @returns Array of existing records that match the filter criteria
|
|
313
314
|
*/
|
|
314
|
-
getExistingRecords(
|
|
315
|
-
return
|
|
316
|
-
const r = yield
|
|
315
|
+
getExistingRecords(n, t, e) {
|
|
316
|
+
return a(this, null, function* () {
|
|
317
|
+
const r = yield n.findAll({});
|
|
317
318
|
return r.success ? r.result.filter(
|
|
318
319
|
(u) => t.some(
|
|
319
320
|
(c) => e(u, c)
|
|
@@ -322,149 +323,149 @@ ${q}
|
|
|
322
323
|
});
|
|
323
324
|
}
|
|
324
325
|
};
|
|
325
|
-
function
|
|
326
|
-
return
|
|
326
|
+
function fe(n) {
|
|
327
|
+
return n !== null && typeof n == "object" && "toObject" in n && typeof n.toObject == "function";
|
|
327
328
|
}
|
|
328
|
-
function
|
|
329
|
-
if (!
|
|
330
|
-
return
|
|
329
|
+
function j(n, t) {
|
|
330
|
+
if (!n || !t || t.length === 0)
|
|
331
|
+
return n;
|
|
331
332
|
const e = new Set(t.map((r) => r.name));
|
|
332
|
-
if (Array.isArray(
|
|
333
|
-
const r =
|
|
334
|
-
if (typeof
|
|
335
|
-
return !e.has(
|
|
336
|
-
if (typeof
|
|
337
|
-
const u =
|
|
333
|
+
if (Array.isArray(n)) {
|
|
334
|
+
const r = n.filter((s) => {
|
|
335
|
+
if (typeof s == "string")
|
|
336
|
+
return !e.has(s);
|
|
337
|
+
if (typeof s == "object" && s !== null) {
|
|
338
|
+
const u = s;
|
|
338
339
|
return !e.has(u.path || u.populate || "");
|
|
339
340
|
}
|
|
340
341
|
return !0;
|
|
341
342
|
});
|
|
342
343
|
return r.length > 0 ? r : void 0;
|
|
343
344
|
}
|
|
344
|
-
if (typeof
|
|
345
|
-
return e.has(
|
|
346
|
-
if (typeof
|
|
347
|
-
const r =
|
|
348
|
-
return e.has(
|
|
345
|
+
if (typeof n == "string")
|
|
346
|
+
return e.has(n) ? void 0 : n;
|
|
347
|
+
if (typeof n == "object" && n !== null) {
|
|
348
|
+
const r = n, s = r.path || r.populate || "";
|
|
349
|
+
return e.has(s) ? void 0 : n;
|
|
349
350
|
}
|
|
350
|
-
return
|
|
351
|
+
return n;
|
|
351
352
|
}
|
|
352
|
-
function
|
|
353
|
-
if (!
|
|
353
|
+
function me(n, t, e) {
|
|
354
|
+
if (!n.length || !t || !(e != null && e.ref))
|
|
354
355
|
return [];
|
|
355
356
|
const r = /* @__PURE__ */ new Map();
|
|
356
|
-
return
|
|
357
|
+
return n.forEach((s) => {
|
|
357
358
|
try {
|
|
358
|
-
const u = e.ref(
|
|
359
|
+
const u = e.ref(s);
|
|
359
360
|
if (u == null)
|
|
360
361
|
return;
|
|
361
362
|
const c = typeof u == "string" ? u : String(u);
|
|
362
363
|
if (c && c.trim() !== "") {
|
|
363
|
-
const
|
|
364
|
-
r.has(
|
|
364
|
+
const i = de(c);
|
|
365
|
+
r.has(i) || r.set(i, []), r.get(i).push(s);
|
|
365
366
|
}
|
|
366
367
|
} catch (u) {
|
|
367
368
|
d(new Error(`Dynamic ref function failed for virtual "${t}": ${u instanceof Error ? u.message : String(u)}`));
|
|
368
369
|
}
|
|
369
|
-
}), Array.from(r.entries()).map(([
|
|
370
|
+
}), Array.from(r.entries()).map(([s, u]) => ({ model: s, docs: u }));
|
|
370
371
|
}
|
|
371
|
-
function
|
|
372
|
-
return
|
|
372
|
+
function z(n, t, e, r, s) {
|
|
373
|
+
return a(this, null, function* () {
|
|
373
374
|
if (!t.length || !e.length || !r)
|
|
374
375
|
return t;
|
|
375
|
-
const u = e.filter((
|
|
376
|
+
const u = e.filter((o) => {
|
|
376
377
|
if (Array.isArray(r))
|
|
377
|
-
return r.length > 0 && r.some((
|
|
378
|
+
return r.length > 0 && r.some((l) => typeof l == "string" ? l === o.name : l.path === o.name);
|
|
378
379
|
if (typeof r == "string")
|
|
379
|
-
return r ===
|
|
380
|
+
return r === o.name;
|
|
380
381
|
if (typeof r == "object" && r !== null) {
|
|
381
|
-
const
|
|
382
|
-
return
|
|
382
|
+
const l = r;
|
|
383
|
+
return l.path && l.path === o.name || l.populate && l.populate === o.name;
|
|
383
384
|
}
|
|
384
385
|
return !1;
|
|
385
386
|
});
|
|
386
387
|
if (u.length === 0)
|
|
387
388
|
return t;
|
|
388
389
|
const c = B(t.map(
|
|
389
|
-
(
|
|
390
|
+
(o) => fe(o) ? o.toObject() : o
|
|
390
391
|
));
|
|
391
|
-
c.forEach((
|
|
392
|
-
u.forEach(({ name:
|
|
393
|
-
if (!(
|
|
394
|
-
const
|
|
395
|
-
|
|
392
|
+
c.forEach((o) => {
|
|
393
|
+
u.forEach(({ name: l, options: f }) => {
|
|
394
|
+
if (!(l in o)) {
|
|
395
|
+
const O = o;
|
|
396
|
+
O[l] = f.count ? 0 : f.justOne ? null : [];
|
|
396
397
|
}
|
|
397
398
|
});
|
|
398
399
|
});
|
|
399
|
-
const
|
|
400
|
-
for (const
|
|
401
|
-
const { name:
|
|
402
|
-
for (const
|
|
403
|
-
|
|
400
|
+
const i = /* @__PURE__ */ new Map();
|
|
401
|
+
for (const o of u) {
|
|
402
|
+
const { name: l, options: f } = o, O = me(c, l, f);
|
|
403
|
+
for (const m of O) {
|
|
404
|
+
i.has(m.model) || i.set(m.model, {
|
|
404
405
|
virtuals: [],
|
|
405
406
|
localValueSets: /* @__PURE__ */ new Map(),
|
|
406
407
|
docsByLocalValue: /* @__PURE__ */ new Map()
|
|
407
408
|
});
|
|
408
|
-
const
|
|
409
|
-
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
const
|
|
413
|
-
if (
|
|
414
|
-
const D = String(
|
|
415
|
-
|
|
409
|
+
const g = i.get(m.model);
|
|
410
|
+
g.virtuals.find((V) => V.name === l) || (g.virtuals.push(o), g.localValueSets.set(l, /* @__PURE__ */ new Set()));
|
|
411
|
+
const v = g.localValueSets.get(l);
|
|
412
|
+
m.docs.forEach((V) => {
|
|
413
|
+
const A = V[f.localField];
|
|
414
|
+
if (A != null) {
|
|
415
|
+
const D = String(A);
|
|
416
|
+
v.add(D);
|
|
416
417
|
let _ = -1;
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
var
|
|
420
|
-
return ((
|
|
421
|
-
})), _ !== -1 && (
|
|
418
|
+
const N = V;
|
|
419
|
+
N.id !== void 0 ? _ = c.findIndex((M) => M.id === N.id) : N._id !== void 0 && (_ = c.findIndex((M) => {
|
|
420
|
+
var p, h, E, y;
|
|
421
|
+
return ((h = (p = M._id) == null ? void 0 : p.toString) == null ? void 0 : h.call(p)) === ((y = (E = N._id) == null ? void 0 : E.toString) == null ? void 0 : y.call(E));
|
|
422
|
+
})), _ !== -1 && (g.docsByLocalValue.has(D) || g.docsByLocalValue.set(D, []), g.docsByLocalValue.get(D).push(_));
|
|
422
423
|
}
|
|
423
424
|
});
|
|
424
425
|
}
|
|
425
426
|
}
|
|
426
|
-
return yield Promise.all(Array.from(
|
|
427
|
-
const
|
|
428
|
-
if (!
|
|
427
|
+
return yield Promise.all(Array.from(i.entries()).map((o) => a(null, [o], function* ([l, f]) {
|
|
428
|
+
const O = n.models[l];
|
|
429
|
+
if (!O)
|
|
429
430
|
return;
|
|
430
|
-
const
|
|
431
|
+
const m = /* @__PURE__ */ new Set();
|
|
431
432
|
if (f.localValueSets.forEach((D) => {
|
|
432
|
-
D.forEach((_) =>
|
|
433
|
-
}),
|
|
433
|
+
D.forEach((_) => m.add(_));
|
|
434
|
+
}), m.size === 0)
|
|
434
435
|
return;
|
|
435
|
-
const
|
|
436
|
-
let
|
|
437
|
-
|
|
438
|
-
const
|
|
436
|
+
const g = [...new Set(f.virtuals.map((D) => D.options.foreignField))], v = Array.from(m);
|
|
437
|
+
let V;
|
|
438
|
+
g.length === 1 ? V = { [String(g[0])]: { $in: v } } : V = { $or: g.map((D) => ({ [D]: { $in: v } })) };
|
|
439
|
+
const A = yield O.find(V, s).lean();
|
|
439
440
|
for (const D of f.virtuals) {
|
|
440
|
-
const { name: _, options:
|
|
441
|
-
const
|
|
442
|
-
return
|
|
441
|
+
const { name: _, options: N } = D, M = A.filter((p) => {
|
|
442
|
+
const h = p[N.foreignField];
|
|
443
|
+
return h != null && m.has(String(h));
|
|
443
444
|
});
|
|
444
|
-
if (
|
|
445
|
-
const
|
|
446
|
-
|
|
445
|
+
if (N.count) {
|
|
446
|
+
const p = /* @__PURE__ */ new Map();
|
|
447
|
+
M.forEach((h) => {
|
|
447
448
|
var E;
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
}), f.localValueSets.get(_).forEach((
|
|
451
|
-
const E = f.docsByLocalValue.get(
|
|
452
|
-
E.forEach((
|
|
453
|
-
const
|
|
454
|
-
|
|
449
|
+
const y = (E = h[N.foreignField]) == null ? void 0 : E.toString();
|
|
450
|
+
y && p.set(y, (p.get(y) || 0) + 1);
|
|
451
|
+
}), f.localValueSets.get(_).forEach((h) => {
|
|
452
|
+
const E = f.docsByLocalValue.get(h) || [], y = p.get(h) || 0;
|
|
453
|
+
E.forEach((P) => {
|
|
454
|
+
const C = c[P];
|
|
455
|
+
C[_] === void 0 && (C[_] = y);
|
|
455
456
|
});
|
|
456
457
|
});
|
|
457
458
|
} else {
|
|
458
|
-
const
|
|
459
|
-
|
|
459
|
+
const p = /* @__PURE__ */ new Map();
|
|
460
|
+
M.forEach((h) => {
|
|
460
461
|
var E;
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
}), f.localValueSets.get(_).forEach((
|
|
464
|
-
const E = f.docsByLocalValue.get(
|
|
465
|
-
E.forEach((
|
|
466
|
-
const
|
|
467
|
-
|
|
462
|
+
const y = (E = h[N.foreignField]) == null ? void 0 : E.toString();
|
|
463
|
+
y && (p.has(y) || p.set(y, []), p.get(y).push(h));
|
|
464
|
+
}), f.localValueSets.get(_).forEach((h) => {
|
|
465
|
+
const E = f.docsByLocalValue.get(h) || [], y = p.get(h) || [], P = N.justOne ? y[0] || null : y;
|
|
466
|
+
E.forEach((C) => {
|
|
467
|
+
const K = c[C];
|
|
468
|
+
K[_] = P;
|
|
468
469
|
});
|
|
469
470
|
});
|
|
470
471
|
}
|
|
@@ -472,7 +473,7 @@ function U(s, t, e, r, n) {
|
|
|
472
473
|
}))), c;
|
|
473
474
|
});
|
|
474
475
|
}
|
|
475
|
-
class
|
|
476
|
+
class Me {
|
|
476
477
|
/**
|
|
477
478
|
* Creates a new MongoDB controller instance.
|
|
478
479
|
*
|
|
@@ -480,7 +481,7 @@ class Ve {
|
|
|
480
481
|
* @param collectionName - The name of the collection to operate on.
|
|
481
482
|
*/
|
|
482
483
|
constructor(t, e) {
|
|
483
|
-
|
|
484
|
+
ae(this, "collection"), this.collection = t.collection(e);
|
|
484
485
|
}
|
|
485
486
|
/**
|
|
486
487
|
* Creates a single document in the collection.
|
|
@@ -490,9 +491,9 @@ class Ve {
|
|
|
490
491
|
* @returns A promise that resolves to a standardized response with the created document.
|
|
491
492
|
*/
|
|
492
493
|
createOne(t) {
|
|
493
|
-
return
|
|
494
|
+
return a(this, null, function* () {
|
|
494
495
|
try {
|
|
495
|
-
const e = S(S({},
|
|
496
|
+
const e = S(S({}, x.createGenericFields()), t);
|
|
496
497
|
return (yield this.collection.insertOne(e)).acknowledged ? {
|
|
497
498
|
success: !0,
|
|
498
499
|
message: "Document created successfully",
|
|
@@ -500,7 +501,7 @@ class Ve {
|
|
|
500
501
|
} : {
|
|
501
502
|
success: !1,
|
|
502
503
|
message: "Document creation failed",
|
|
503
|
-
code:
|
|
504
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
504
505
|
};
|
|
505
506
|
} catch (e) {
|
|
506
507
|
return d(e);
|
|
@@ -515,13 +516,13 @@ class Ve {
|
|
|
515
516
|
* @returns A promise that resolves to a standardized response with the created documents.
|
|
516
517
|
*/
|
|
517
518
|
createMany(t) {
|
|
518
|
-
return
|
|
519
|
+
return a(this, null, function* () {
|
|
519
520
|
try {
|
|
520
|
-
const e = t.map((
|
|
521
|
+
const e = t.map((s) => S(S({}, x.createGenericFields()), s)), r = yield this.collection.insertMany(e);
|
|
521
522
|
return r.insertedCount === 0 ? {
|
|
522
523
|
success: !1,
|
|
523
524
|
message: "No documents were inserted",
|
|
524
|
-
code:
|
|
525
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
525
526
|
} : {
|
|
526
527
|
success: !0,
|
|
527
528
|
message: `${r.insertedCount} documents created successfully`,
|
|
@@ -539,10 +540,10 @@ class Ve {
|
|
|
539
540
|
* @returns A promise that resolves to a standardized response with the found document.
|
|
540
541
|
*/
|
|
541
542
|
findOne(t) {
|
|
542
|
-
return
|
|
543
|
+
return a(this, null, function* () {
|
|
543
544
|
try {
|
|
544
545
|
const e = yield this.collection.findOne(t);
|
|
545
|
-
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code:
|
|
546
|
+
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code: R.NOT_FOUND.CODE };
|
|
546
547
|
} catch (e) {
|
|
547
548
|
return d(e);
|
|
548
549
|
}
|
|
@@ -555,7 +556,7 @@ class Ve {
|
|
|
555
556
|
* @returns A promise that resolves to a standardized response with the found documents.
|
|
556
557
|
*/
|
|
557
558
|
findAll() {
|
|
558
|
-
return
|
|
559
|
+
return a(this, arguments, function* (t = {}) {
|
|
559
560
|
try {
|
|
560
561
|
return {
|
|
561
562
|
success: !0,
|
|
@@ -574,7 +575,7 @@ class Ve {
|
|
|
574
575
|
* @returns A promise that resolves to a standardized response with the document count.
|
|
575
576
|
*/
|
|
576
577
|
count() {
|
|
577
|
-
return
|
|
578
|
+
return a(this, arguments, function* (t = {}) {
|
|
578
579
|
try {
|
|
579
580
|
const e = yield this.collection.countDocuments(t);
|
|
580
581
|
return {
|
|
@@ -595,7 +596,7 @@ class Ve {
|
|
|
595
596
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
596
597
|
*/
|
|
597
598
|
updateOne(t, e) {
|
|
598
|
-
return
|
|
599
|
+
return a(this, null, function* () {
|
|
599
600
|
try {
|
|
600
601
|
const r = yield this.collection.updateOne(t, {
|
|
601
602
|
$set: e
|
|
@@ -603,7 +604,7 @@ class Ve {
|
|
|
603
604
|
return r.matchedCount === 0 ? {
|
|
604
605
|
success: !1,
|
|
605
606
|
message: "No documents matched the filter",
|
|
606
|
-
code:
|
|
607
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
607
608
|
} : {
|
|
608
609
|
success: !0,
|
|
609
610
|
message: "Document updated successfully",
|
|
@@ -622,7 +623,7 @@ class Ve {
|
|
|
622
623
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
623
624
|
*/
|
|
624
625
|
updateMany(t, e) {
|
|
625
|
-
return
|
|
626
|
+
return a(this, null, function* () {
|
|
626
627
|
try {
|
|
627
628
|
const r = yield this.collection.updateMany(t, {
|
|
628
629
|
$set: e
|
|
@@ -630,7 +631,7 @@ class Ve {
|
|
|
630
631
|
return r.matchedCount === 0 ? {
|
|
631
632
|
success: !1,
|
|
632
633
|
message: "No documents matched the filter",
|
|
633
|
-
code:
|
|
634
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
634
635
|
} : {
|
|
635
636
|
success: !0,
|
|
636
637
|
message: "Documents updated successfully",
|
|
@@ -648,13 +649,13 @@ class Ve {
|
|
|
648
649
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
649
650
|
*/
|
|
650
651
|
deleteOne(t) {
|
|
651
|
-
return
|
|
652
|
+
return a(this, null, function* () {
|
|
652
653
|
try {
|
|
653
654
|
const e = yield this.collection.deleteOne(t);
|
|
654
655
|
return e.deletedCount === 0 ? {
|
|
655
656
|
success: !1,
|
|
656
657
|
message: "No documents matched the filter",
|
|
657
|
-
code:
|
|
658
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
658
659
|
} : {
|
|
659
660
|
success: !0,
|
|
660
661
|
message: "Document deleted successfully",
|
|
@@ -672,13 +673,13 @@ class Ve {
|
|
|
672
673
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
673
674
|
*/
|
|
674
675
|
deleteMany(t) {
|
|
675
|
-
return
|
|
676
|
+
return a(this, null, function* () {
|
|
676
677
|
try {
|
|
677
678
|
const e = yield this.collection.deleteMany(t);
|
|
678
679
|
return e.deletedCount === 0 ? {
|
|
679
680
|
success: !1,
|
|
680
681
|
message: "No documents matched the filter",
|
|
681
|
-
code:
|
|
682
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
682
683
|
} : {
|
|
683
684
|
success: !0,
|
|
684
685
|
message: "Documents deleted successfully",
|
|
@@ -690,7 +691,7 @@ class Ve {
|
|
|
690
691
|
});
|
|
691
692
|
}
|
|
692
693
|
}
|
|
693
|
-
class
|
|
694
|
+
class we {
|
|
694
695
|
/**
|
|
695
696
|
* Creates a new Mongoose controller instance.
|
|
696
697
|
*
|
|
@@ -723,11 +724,11 @@ class be {
|
|
|
723
724
|
* @returns The document with dynamic virtuals populated.
|
|
724
725
|
*/
|
|
725
726
|
populateDynamicVirtualsForDocument(t, e) {
|
|
726
|
-
return
|
|
727
|
+
return a(this, null, function* () {
|
|
727
728
|
const r = this.getDynamicVirtuals();
|
|
728
729
|
if (r && r.length > 0) {
|
|
729
|
-
const
|
|
730
|
-
return
|
|
730
|
+
const s = yield z(this.model.base, [t], r, e);
|
|
731
|
+
return s && s[0] ? s[0] : t;
|
|
731
732
|
}
|
|
732
733
|
return t;
|
|
733
734
|
});
|
|
@@ -740,9 +741,9 @@ class be {
|
|
|
740
741
|
* @returns The documents with dynamic virtuals populated.
|
|
741
742
|
*/
|
|
742
743
|
populateDynamicVirtualsForDocuments(t, e) {
|
|
743
|
-
return
|
|
744
|
+
return a(this, null, function* () {
|
|
744
745
|
const r = this.getDynamicVirtuals();
|
|
745
|
-
return r && r.length > 0 && t.length > 0 ? yield
|
|
746
|
+
return r && r.length > 0 && t.length > 0 ? yield z(this.model.base, t, r, e) : t;
|
|
746
747
|
});
|
|
747
748
|
}
|
|
748
749
|
/**
|
|
@@ -756,15 +757,15 @@ class be {
|
|
|
756
757
|
* @returns A promise that resolves to a standardized response with the found document.
|
|
757
758
|
*/
|
|
758
759
|
findOne() {
|
|
759
|
-
return
|
|
760
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, s) {
|
|
760
761
|
try {
|
|
761
|
-
const u = this.model.findOne(
|
|
762
|
-
o &&
|
|
763
|
-
const
|
|
764
|
-
return
|
|
762
|
+
const u = b(t), c = this.model.findOne(u, e, r), i = this.getDynamicVirtuals(), o = j(s, i);
|
|
763
|
+
o && c.populate(o);
|
|
764
|
+
const l = yield c.exec();
|
|
765
|
+
return l ? { success: !0, result: yield this.populateDynamicVirtualsForDocument(l, s) } : {
|
|
765
766
|
success: !1,
|
|
766
767
|
message: `No ${this.getModelName()} found.`,
|
|
767
|
-
code:
|
|
768
|
+
code: R.NOT_FOUND.CODE
|
|
768
769
|
};
|
|
769
770
|
} catch (u) {
|
|
770
771
|
return d(u);
|
|
@@ -782,12 +783,12 @@ class be {
|
|
|
782
783
|
* @returns A promise that resolves to a standardized response with the found documents.
|
|
783
784
|
*/
|
|
784
785
|
findAll() {
|
|
785
|
-
return
|
|
786
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, s) {
|
|
786
787
|
try {
|
|
787
|
-
const u = this.model.find(
|
|
788
|
-
o &&
|
|
789
|
-
const
|
|
790
|
-
return { success: !0, result: yield this.populateDynamicVirtualsForDocuments(
|
|
788
|
+
const u = b(t), c = this.model.find(u, e, r), i = this.getDynamicVirtuals(), o = j(s, i);
|
|
789
|
+
o && c.populate(o);
|
|
790
|
+
const l = yield c.exec();
|
|
791
|
+
return { success: !0, result: yield this.populateDynamicVirtualsForDocuments(l, s) };
|
|
791
792
|
} catch (u) {
|
|
792
793
|
return d(u);
|
|
793
794
|
}
|
|
@@ -802,12 +803,12 @@ class be {
|
|
|
802
803
|
* @returns A promise that resolves to a standardized response with paginated results.
|
|
803
804
|
*/
|
|
804
805
|
findPaging() {
|
|
805
|
-
return
|
|
806
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
806
807
|
try {
|
|
807
|
-
const r = this.getDynamicVirtuals(),
|
|
808
|
-
e.populate && (
|
|
809
|
-
const
|
|
810
|
-
return { success: !0, result:
|
|
808
|
+
const r = b(t), s = this.getDynamicVirtuals(), u = S({}, e);
|
|
809
|
+
e.populate && (u.populate = j(e.populate, s));
|
|
810
|
+
const c = yield this.model.paginate(r, u), i = yield this.populateDynamicVirtualsForDocuments(c.docs, e.populate);
|
|
811
|
+
return { success: !0, result: F(S({}, c), { docs: i }) };
|
|
811
812
|
} catch (r) {
|
|
812
813
|
return d(r);
|
|
813
814
|
}
|
|
@@ -821,17 +822,17 @@ class be {
|
|
|
821
822
|
* @returns A promise that resolves to a standardized response with paginated aggregation results.
|
|
822
823
|
*/
|
|
823
824
|
findPagingAggregate(t) {
|
|
824
|
-
return
|
|
825
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
825
826
|
try {
|
|
826
|
-
const
|
|
827
|
-
r.populate && (u.populate =
|
|
827
|
+
const s = this.getDynamicVirtuals(), u = S({}, r);
|
|
828
|
+
r.populate && (u.populate = j(r.populate, s));
|
|
828
829
|
const c = yield this.model.aggregatePaginate(
|
|
829
830
|
this.model.aggregate(e),
|
|
830
831
|
u
|
|
831
|
-
),
|
|
832
|
-
return { success: !0, result:
|
|
833
|
-
} catch (
|
|
834
|
-
return d(
|
|
832
|
+
), i = yield this.populateDynamicVirtualsForDocuments(c.docs, r.populate);
|
|
833
|
+
return { success: !0, result: F(S({}, c), { docs: i }) };
|
|
834
|
+
} catch (s) {
|
|
835
|
+
return d(s);
|
|
835
836
|
}
|
|
836
837
|
});
|
|
837
838
|
}
|
|
@@ -842,9 +843,10 @@ class be {
|
|
|
842
843
|
* @returns A promise that resolves to a standardized response with the document count.
|
|
843
844
|
*/
|
|
844
845
|
count() {
|
|
845
|
-
return
|
|
846
|
+
return a(this, arguments, function* (t = {}) {
|
|
846
847
|
try {
|
|
847
|
-
|
|
848
|
+
const e = b(t);
|
|
849
|
+
return { success: !0, result: yield this.model.countDocuments(e) };
|
|
848
850
|
} catch (e) {
|
|
849
851
|
return d(e);
|
|
850
852
|
}
|
|
@@ -857,7 +859,7 @@ class be {
|
|
|
857
859
|
* @returns A promise that resolves to a standardized response with the created document.
|
|
858
860
|
*/
|
|
859
861
|
createOne(t) {
|
|
860
|
-
return
|
|
862
|
+
return a(this, null, function* () {
|
|
861
863
|
try {
|
|
862
864
|
return { success: !0, result: yield this.model.create(t) };
|
|
863
865
|
} catch (e) {
|
|
@@ -873,11 +875,11 @@ class be {
|
|
|
873
875
|
* @returns A promise that resolves to a standardized response with the created documents.
|
|
874
876
|
*/
|
|
875
877
|
createMany(t) {
|
|
876
|
-
return
|
|
878
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
877
879
|
try {
|
|
878
880
|
return { success: !0, result: (yield this.model.insertMany(e, r)).map((c) => c instanceof W ? c.toObject() : null).filter((c) => c !== null) };
|
|
879
|
-
} catch (
|
|
880
|
-
return d(
|
|
881
|
+
} catch (s) {
|
|
882
|
+
return d(s);
|
|
881
883
|
}
|
|
882
884
|
});
|
|
883
885
|
}
|
|
@@ -890,18 +892,18 @@ class be {
|
|
|
890
892
|
* @returns A promise that resolves to a standardized response with the updated document.
|
|
891
893
|
*/
|
|
892
894
|
updateOne() {
|
|
893
|
-
return
|
|
895
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
894
896
|
try {
|
|
895
|
-
const
|
|
897
|
+
const s = b(t), u = yield this.model.findOneAndUpdate(s, e, S({
|
|
896
898
|
new: !0
|
|
897
899
|
}, r)).exec();
|
|
898
|
-
return
|
|
900
|
+
return u ? { success: !0, result: u } : {
|
|
899
901
|
success: !1,
|
|
900
902
|
message: `Failed to update ${this.getModelName()}.`,
|
|
901
|
-
code:
|
|
903
|
+
code: R.NOT_FOUND.CODE
|
|
902
904
|
};
|
|
903
|
-
} catch (
|
|
904
|
-
return d(
|
|
905
|
+
} catch (s) {
|
|
906
|
+
return d(s);
|
|
905
907
|
}
|
|
906
908
|
});
|
|
907
909
|
}
|
|
@@ -914,11 +916,12 @@ class be {
|
|
|
914
916
|
* @returns A promise that resolves to a standardized response with the update result.
|
|
915
917
|
*/
|
|
916
918
|
updateMany() {
|
|
917
|
-
return
|
|
919
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
918
920
|
try {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
921
|
+
const s = b(t);
|
|
922
|
+
return { success: !0, result: yield this.model.updateMany(s, e, r).exec() };
|
|
923
|
+
} catch (s) {
|
|
924
|
+
return d(s);
|
|
922
925
|
}
|
|
923
926
|
});
|
|
924
927
|
}
|
|
@@ -930,13 +933,13 @@ class be {
|
|
|
930
933
|
* @returns A promise that resolves to a standardized response with the deleted document.
|
|
931
934
|
*/
|
|
932
935
|
deleteOne() {
|
|
933
|
-
return
|
|
936
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
934
937
|
try {
|
|
935
|
-
const r = yield this.model.findOneAndDelete(
|
|
936
|
-
return
|
|
938
|
+
const r = b(t), s = yield this.model.findOneAndDelete(r, e).exec();
|
|
939
|
+
return s ? { success: !0, result: s } : {
|
|
937
940
|
success: !1,
|
|
938
941
|
message: `No ${this.getModelName()} found to delete.`,
|
|
939
|
-
code:
|
|
942
|
+
code: R.NOT_FOUND.CODE
|
|
940
943
|
};
|
|
941
944
|
} catch (r) {
|
|
942
945
|
return d(r);
|
|
@@ -951,14 +954,14 @@ class be {
|
|
|
951
954
|
* @returns A promise that resolves to a standardized response with the delete result.
|
|
952
955
|
*/
|
|
953
956
|
deleteMany() {
|
|
954
|
-
return
|
|
957
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
955
958
|
try {
|
|
956
|
-
const r = yield this.model.deleteMany(
|
|
957
|
-
return
|
|
959
|
+
const r = b(t), s = yield this.model.deleteMany(r, e).exec();
|
|
960
|
+
return s.deletedCount === 0 ? {
|
|
958
961
|
success: !1,
|
|
959
962
|
message: "No documents found to delete.",
|
|
960
|
-
code:
|
|
961
|
-
} : { success: !0, result:
|
|
963
|
+
code: R.NOT_FOUND.CODE
|
|
964
|
+
} : { success: !0, result: s };
|
|
962
965
|
} catch (r) {
|
|
963
966
|
return d(r);
|
|
964
967
|
}
|
|
@@ -973,20 +976,20 @@ class be {
|
|
|
973
976
|
* @returns A promise that resolves to a standardized response with the unique short ID.
|
|
974
977
|
*/
|
|
975
978
|
createShortId(t, e = 4) {
|
|
976
|
-
return
|
|
979
|
+
return a(this, null, function* () {
|
|
977
980
|
try {
|
|
978
|
-
const
|
|
979
|
-
|
|
980
|
-
)).findIndex((
|
|
981
|
+
const s = Array.from({ length: 10 }, (i, o) => ne(t, o + e)), c = (yield Promise.all(
|
|
982
|
+
s.map((i) => this.model.exists({ shortId: i }))
|
|
983
|
+
)).findIndex((i) => !i);
|
|
981
984
|
if (c !== -1) {
|
|
982
|
-
const
|
|
983
|
-
if (
|
|
984
|
-
return { success: !0, result:
|
|
985
|
+
const i = s[c];
|
|
986
|
+
if (i)
|
|
987
|
+
return { success: !0, result: i };
|
|
985
988
|
}
|
|
986
989
|
return {
|
|
987
990
|
success: !1,
|
|
988
991
|
message: "Failed to create a unique shortId",
|
|
989
|
-
code:
|
|
992
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
990
993
|
};
|
|
991
994
|
} catch (r) {
|
|
992
995
|
return d(r);
|
|
@@ -1004,17 +1007,17 @@ class be {
|
|
|
1004
1007
|
* @param options.filter - Additional filter conditions to apply to the query.
|
|
1005
1008
|
* @returns A MongoDB query object for checking slug existence.
|
|
1006
1009
|
*/
|
|
1007
|
-
createSlugQuery({ slug: t, field: e, isObject: r, filter:
|
|
1008
|
-
const
|
|
1009
|
-
return r ?
|
|
1010
|
+
createSlugQuery({ slug: t, field: e, isObject: r, haveHistory: s = !1, filter: u }) {
|
|
1011
|
+
const c = S({}, u != null ? u : {});
|
|
1012
|
+
return r ? F(S({}, c), {
|
|
1010
1013
|
$or: [
|
|
1011
1014
|
{ [`slug.${e}`]: t },
|
|
1012
|
-
{ slugHistory: { $elemMatch: { [`slug.${e}`]: t } } }
|
|
1015
|
+
...s ? [{ slugHistory: { $elemMatch: { [`slug.${e}`]: t } } }] : []
|
|
1013
1016
|
]
|
|
1014
|
-
}) :
|
|
1017
|
+
}) : F(S({}, c), {
|
|
1015
1018
|
$or: [
|
|
1016
1019
|
{ slug: t },
|
|
1017
|
-
{ slugHistory: t }
|
|
1020
|
+
...s ? [{ slugHistory: t }] : []
|
|
1018
1021
|
]
|
|
1019
1022
|
});
|
|
1020
1023
|
}
|
|
@@ -1030,18 +1033,23 @@ class be {
|
|
|
1030
1033
|
* @returns A promise that resolves to a unique slug string.
|
|
1031
1034
|
*/
|
|
1032
1035
|
createUniqueSlug(t) {
|
|
1033
|
-
return
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1036
|
+
return a(this, arguments, function* ({ slug: e, field: r, isObject: s, haveHistory: u, filter: c }) {
|
|
1037
|
+
if (!e || typeof e != "string")
|
|
1038
|
+
throw new Error("Invalid slug provided: must be a non-empty string");
|
|
1039
|
+
const i = I(e);
|
|
1040
|
+
if (!(yield this.model.exists(
|
|
1041
|
+
this.createSlugQuery({ slug: i, field: r, isObject: s, haveHistory: u, filter: c })
|
|
1042
|
+
)))
|
|
1043
|
+
return i;
|
|
1044
|
+
for (let O = 1; O <= X; O++) {
|
|
1045
|
+
const m = `${i}-${O}`;
|
|
1046
|
+
if (!(yield this.model.exists(
|
|
1047
|
+
this.createSlugQuery({ slug: m, field: r, isObject: s, haveHistory: u, filter: c })
|
|
1048
|
+
)))
|
|
1042
1049
|
return m;
|
|
1043
1050
|
}
|
|
1044
|
-
|
|
1051
|
+
const l = Date.now(), f = Math.random().toString(36).substring(2, 8);
|
|
1052
|
+
return `${i}-${l}-${f}`;
|
|
1045
1053
|
});
|
|
1046
1054
|
}
|
|
1047
1055
|
/**
|
|
@@ -1055,29 +1063,31 @@ class be {
|
|
|
1055
1063
|
* @returns A promise that resolves to a standardized response with the created slug(s).
|
|
1056
1064
|
*/
|
|
1057
1065
|
createSlug(t) {
|
|
1058
|
-
return
|
|
1066
|
+
return a(this, arguments, function* ({ field: e, from: r, filter: s, haveHistory: u }) {
|
|
1059
1067
|
try {
|
|
1060
|
-
const
|
|
1061
|
-
return $(
|
|
1068
|
+
const c = r[e];
|
|
1069
|
+
return $(c) ? { success: !0, result: Object.fromEntries(
|
|
1062
1070
|
yield Promise.all(
|
|
1063
|
-
Object.entries(
|
|
1064
|
-
const
|
|
1071
|
+
Object.entries(c).map((f) => a(this, [f], function* ([O, m]) {
|
|
1072
|
+
const g = yield this.createUniqueSlug({
|
|
1065
1073
|
slug: m,
|
|
1066
|
-
field:
|
|
1074
|
+
field: O,
|
|
1067
1075
|
isObject: !0,
|
|
1068
|
-
|
|
1076
|
+
haveHistory: u,
|
|
1077
|
+
filter: s
|
|
1069
1078
|
});
|
|
1070
|
-
return [
|
|
1079
|
+
return [O, g];
|
|
1071
1080
|
}))
|
|
1072
1081
|
)
|
|
1073
1082
|
) } : { success: !0, result: yield this.createUniqueSlug({
|
|
1074
|
-
slug:
|
|
1083
|
+
slug: c,
|
|
1075
1084
|
field: e,
|
|
1076
1085
|
isObject: !1,
|
|
1077
|
-
|
|
1086
|
+
haveHistory: u,
|
|
1087
|
+
filter: s
|
|
1078
1088
|
}) };
|
|
1079
|
-
} catch (
|
|
1080
|
-
return d(
|
|
1089
|
+
} catch (c) {
|
|
1090
|
+
return d(c);
|
|
1081
1091
|
}
|
|
1082
1092
|
});
|
|
1083
1093
|
}
|
|
@@ -1093,31 +1103,32 @@ class be {
|
|
|
1093
1103
|
* @returns A promise that resolves to a standardized response indicating whether the slug exists.
|
|
1094
1104
|
*/
|
|
1095
1105
|
checkSlug(t) {
|
|
1096
|
-
return
|
|
1106
|
+
return a(this, arguments, function* ({ slug: e, field: r, from: s, filter: u, haveHistory: c }) {
|
|
1097
1107
|
try {
|
|
1098
|
-
const
|
|
1099
|
-
if ($(
|
|
1100
|
-
const m = Object.values(
|
|
1108
|
+
const i = s[r];
|
|
1109
|
+
if ($(i)) {
|
|
1110
|
+
const m = Object.values(i).map((v) => I(v));
|
|
1101
1111
|
return (yield Promise.all(
|
|
1102
1112
|
m.map(
|
|
1103
|
-
(
|
|
1104
|
-
slug:
|
|
1113
|
+
(v) => this.model.exists(this.createSlugQuery({
|
|
1114
|
+
slug: v,
|
|
1105
1115
|
field: r,
|
|
1106
1116
|
isObject: !0,
|
|
1117
|
+
haveHistory: c,
|
|
1107
1118
|
filter: u
|
|
1108
1119
|
}))
|
|
1109
1120
|
)
|
|
1110
|
-
)).some((
|
|
1121
|
+
)).some((v) => v) ? { success: !0, result: !0 } : { success: !0, result: !1 };
|
|
1111
1122
|
}
|
|
1112
|
-
const
|
|
1123
|
+
const l = I(e);
|
|
1113
1124
|
return { success: !0, result: (yield this.model.exists(this.createSlugQuery({
|
|
1114
|
-
slug:
|
|
1125
|
+
slug: l,
|
|
1115
1126
|
field: r,
|
|
1116
1127
|
isObject: !1,
|
|
1117
1128
|
filter: u
|
|
1118
1129
|
}))) !== null };
|
|
1119
|
-
} catch (
|
|
1120
|
-
return d(
|
|
1130
|
+
} catch (i) {
|
|
1131
|
+
return d(i);
|
|
1121
1132
|
}
|
|
1122
1133
|
});
|
|
1123
1134
|
}
|
|
@@ -1128,7 +1139,7 @@ class be {
|
|
|
1128
1139
|
* @returns A promise that resolves to a standardized response with the aggregation results.
|
|
1129
1140
|
*/
|
|
1130
1141
|
aggregate(t) {
|
|
1131
|
-
return
|
|
1142
|
+
return a(this, null, function* () {
|
|
1132
1143
|
try {
|
|
1133
1144
|
return { success: !0, result: yield this.model.aggregate(t) };
|
|
1134
1145
|
} catch (e) {
|
|
@@ -1138,7 +1149,7 @@ class be {
|
|
|
1138
1149
|
}
|
|
1139
1150
|
}
|
|
1140
1151
|
export {
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1152
|
+
Me as MongoController,
|
|
1153
|
+
we as MongooseController,
|
|
1154
|
+
x as mongo
|
|
1144
1155
|
};
|