@cyberskill/shared 1.217.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -223
- package/dist/config/config.util.d.ts +17 -0
- package/dist/config/env/env.util.d.ts +26 -0
- package/dist/config/eslint/index.d.ts +4 -0
- package/dist/config/graphql-codegen/graphql-codegen.util.d.ts +23 -0
- package/dist/config/vitest/vitest.e2e.cjs +1 -1
- package/dist/config/vitest/vitest.e2e.d.ts +16 -0
- package/dist/config/vitest/vitest.e2e.js +1 -1
- package/dist/config/vitest/vitest.unit.cjs +1 -1
- package/dist/config/vitest/vitest.unit.d.ts +18 -0
- package/dist/config/vitest/vitest.unit.js +1 -1
- package/dist/constant/common.d.ts +4 -0
- package/dist/constant/index.d.ts +3 -0
- package/dist/node/apollo-server/apollo-server.util.d.ts +14 -0
- package/dist/node/command/command.type.d.ts +26 -6
- package/dist/node/command/command.util.d.ts +50 -0
- package/dist/node/express/express.util.d.ts +47 -0
- package/dist/node/fs/fs.type.d.ts +6 -0
- package/dist/node/fs/fs.util.d.ts +48 -1
- package/dist/node/log/log.type.d.ts +6 -1
- package/dist/node/log/log.util.cjs +1 -1
- package/dist/node/log/log.util.d.ts +30 -2
- package/dist/node/log/log.util.js +33 -20
- package/dist/node/mongo/index.cjs +1 -1
- package/dist/node/mongo/index.js +11 -15
- package/dist/node/mongo/mongo.constant.d.ts +5 -0
- package/dist/node/mongo/mongo.type.d.ts +30 -10
- package/dist/node/mongo/mongo.util.cjs +5 -5
- package/dist/node/mongo/mongo.util.d.ts +355 -12
- package/dist/node/mongo/mongo.util.js +686 -190
- package/dist/node/package/package.type.d.ts +2 -2
- package/dist/node/package/package.util.cjs +1 -1
- package/dist/node/package/package.util.d.ts +52 -1
- package/dist/node/package/package.util.js +97 -92
- package/dist/node/path/path.constant.d.ts +9 -0
- package/dist/node/path/path.util.d.ts +24 -0
- package/dist/node/storage/storage.util.d.ts +44 -0
- package/dist/node/storage/storage.util.js +39 -0
- package/dist/node/upload/upload.util.d.ts +21 -0
- package/dist/node/ws/ts.util.d.ts +16 -0
- package/dist/react/apollo-client/apollo-client.component.d.ts +19 -1
- package/dist/react/apollo-client/apollo-client.component.js +1 -4
- package/dist/react/apollo-client/apollo-client.constant.d.ts +5 -0
- package/dist/react/apollo-client/apollo-client.context.d.ts +15 -0
- package/dist/react/apollo-client/apollo-client.hook.d.ts +12 -0
- package/dist/react/apollo-client/apollo-client.util.d.ts +48 -0
- package/dist/react/apollo-client/apollo-client.util.js +9 -0
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.d.ts +19 -1
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.js +1 -4
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.d.ts +10 -0
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.d.ts +10 -0
- package/dist/react/apollo-error/apollo-error.component.cjs +1 -1
- package/dist/react/apollo-error/apollo-error.component.d.ts +17 -0
- package/dist/react/apollo-error/apollo-error.component.js +10 -10
- package/dist/react/apollo-error/apollo-error.hook.d.ts +12 -0
- package/dist/react/apollo-error/apollo-error.provider.d.ts +15 -0
- package/dist/react/apollo-error/apollo-error.util.d.ts +17 -0
- package/dist/react/i18next/i18next.hook.d.ts +9 -0
- package/dist/react/i18next/i18next.util.d.ts +10 -0
- package/dist/react/loading/loading.component.d.ts +21 -0
- package/dist/react/loading/loading.hook.d.ts +12 -0
- package/dist/react/loading/loading.provider.d.ts +15 -0
- package/dist/react/log/log.util.d.ts +17 -0
- package/dist/react/next-intl/next-intl.constant.d.ts +9 -0
- package/dist/react/next-intl/next-intl.hoc.d.ts +16 -0
- package/dist/react/next-intl/next-intl.hook.d.ts +21 -0
- package/dist/react/next-intl/next-intl.provider.d.ts +21 -0
- package/dist/react/storage/storage.hook.d.ts +19 -0
- package/dist/react/storage/storage.hook.js +28 -30
- package/dist/react/storage/storage.util.d.ts +39 -0
- package/dist/react/storage/storage.util.js +33 -0
- package/dist/react/userback/userback.component.d.ts +18 -0
- package/dist/typescript/common.type.d.ts +6 -0
- package/dist/typescript/index.d.ts +3 -0
- package/dist/util/common/common.util.d.ts +19 -7
- package/dist/util/common/index.d.ts +3 -0
- package/dist/util/index.d.ts +3 -0
- package/dist/util/object/object.util.d.ts +25 -12
- package/dist/util/serializer/serializer.type.d.ts +1 -1
- package/dist/util/serializer/serializer.util.d.ts +8 -0
- package/dist/util/serializer/serializer.util.js +4 -4
- package/dist/util/string/string.util.cjs +1 -1
- package/dist/util/string/string.util.d.ts +30 -17
- package/dist/util/string/string.util.js +14 -16
- package/dist/util/validate/validate.util.d.ts +9 -3
- package/dist/util/validate/validate.util.js +5 -3
- package/package.json +12 -10
- /package/dist/node_modules/.pnpm/{vitest@3.2.4_@types_debug@4.1.12_@types_node@24.0.14_@vitest_browser@3.2.4_jiti@2.4.2_j_5b4f3b6d03b5e1745ce439b75ce4a42f → vitest@3.2.4_@types_debug@4.1.12_@types_node@24.1.0_@vitest_browser@3.2.4_jiti@2.5.0_js_54cc41e7146dfcdf3e05180c4f422c31}/node_modules/vitest/dist/config.cjs +0 -0
- /package/dist/node_modules/.pnpm/{vitest@3.2.4_@types_debug@4.1.12_@types_node@24.0.14_@vitest_browser@3.2.4_jiti@2.4.2_j_5b4f3b6d03b5e1745ce439b75ce4a42f → vitest@3.2.4_@types_debug@4.1.12_@types_node@24.1.0_@vitest_browser@3.2.4_jiti@2.5.0_js_54cc41e7146dfcdf3e05180c4f422c31}/node_modules/vitest/dist/config.js +0 -0
|
@@ -1,68 +1,111 @@
|
|
|
1
|
-
import { cloneDeep as
|
|
2
|
-
import
|
|
3
|
-
import { Document as
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import { catchError as i } from "../log/log.util.js";
|
|
17
|
-
var V = Object.defineProperty, U = Object.defineProperties, L = Object.getOwnPropertyDescriptors, b = Object.getOwnPropertySymbols, Q = Object.prototype.hasOwnProperty, H = Object.prototype.propertyIsEnumerable, E = (n, t, e) => t in n ? V(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, f = (n, t) => {
|
|
1
|
+
import { cloneDeep as U, isObject as $ } from "lodash-es";
|
|
2
|
+
import k from "migrate-mongo";
|
|
3
|
+
import { Document as K } from "mongoose";
|
|
4
|
+
import W from "mongoose-aggregate-paginate-v2";
|
|
5
|
+
import Q from "mongoose-paginate-v2";
|
|
6
|
+
import { v4 as F } from "uuid";
|
|
7
|
+
import { getNestedValue as H, setNestedValue as z } from "../../util/object/object.util.js";
|
|
8
|
+
import { regexSearchMapper as J } from "../../util/common/common.util.js";
|
|
9
|
+
import { writeFileSync as G, pathExistsSync as X, readFileSync as Y, appendFileSync as Z } from "../fs/fs.util.js";
|
|
10
|
+
import { PATH as x, MIGRATE_MONGO_CONFIG as q } from "../path/path.constant.js";
|
|
11
|
+
import { validate as ee } from "../../util/validate/validate.util.js";
|
|
12
|
+
import { generateShortId as te, generateSlug as j } from "../../util/string/string.util.js";
|
|
13
|
+
import { RESPONSE_STATUS as R } from "../../constant/response-status.js";
|
|
14
|
+
import { catchError as f } from "../log/log.util.js";
|
|
15
|
+
var re = Object.defineProperty, se = Object.defineProperties, ne = Object.getOwnPropertyDescriptors, L = Object.getOwnPropertySymbols, ce = Object.prototype.hasOwnProperty, ue = Object.prototype.propertyIsEnumerable, T = (n, t, e) => t in n ? re(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, E = (n, t) => {
|
|
18
16
|
for (var e in t || (t = {}))
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
for (var e of
|
|
22
|
-
|
|
17
|
+
ce.call(t, e) && T(n, e, t[e]);
|
|
18
|
+
if (L)
|
|
19
|
+
for (var e of L(t))
|
|
20
|
+
ue.call(t, e) && T(n, e, t[e]);
|
|
23
21
|
return n;
|
|
24
|
-
},
|
|
25
|
-
var
|
|
22
|
+
}, w = (n, t) => se(n, ne(t)), oe = (n, t, e) => T(n, typeof t != "symbol" ? t + "" : t, e), a = (n, t, e) => new Promise((r, s) => {
|
|
23
|
+
var c = (i) => {
|
|
26
24
|
try {
|
|
27
|
-
|
|
28
|
-
} catch (
|
|
29
|
-
s(
|
|
25
|
+
o(e.next(i));
|
|
26
|
+
} catch (l) {
|
|
27
|
+
s(l);
|
|
30
28
|
}
|
|
31
|
-
},
|
|
29
|
+
}, u = (i) => {
|
|
32
30
|
try {
|
|
33
|
-
|
|
34
|
-
} catch (
|
|
35
|
-
s(
|
|
31
|
+
o(e.throw(i));
|
|
32
|
+
} catch (l) {
|
|
33
|
+
s(l);
|
|
36
34
|
}
|
|
37
|
-
},
|
|
38
|
-
|
|
35
|
+
}, o = (i) => i.done ? r(i.value) : Promise.resolve(i.value).then(c, u);
|
|
36
|
+
o((e = e.apply(n, t)).next());
|
|
39
37
|
});
|
|
40
|
-
|
|
38
|
+
function ie(n) {
|
|
39
|
+
return n === n.toUpperCase() ? n.charAt(0).toUpperCase() + n.slice(1).toLowerCase() : n;
|
|
40
|
+
}
|
|
41
|
+
const b = {
|
|
42
|
+
/**
|
|
43
|
+
* Creates generic fields that are commonly used across MongoDB documents.
|
|
44
|
+
* This function generates standard fields including a UUID, deletion flag, and timestamps
|
|
45
|
+
* that can be applied to any document schema.
|
|
46
|
+
*
|
|
47
|
+
* @returns An object containing generic document fields (id, isDel, createdAt, updatedAt).
|
|
48
|
+
*/
|
|
41
49
|
createGenericFields() {
|
|
42
50
|
return {
|
|
43
|
-
id:
|
|
51
|
+
id: F(),
|
|
44
52
|
isDel: !1,
|
|
45
53
|
createdAt: /* @__PURE__ */ new Date(),
|
|
46
54
|
updatedAt: /* @__PURE__ */ new Date()
|
|
47
55
|
};
|
|
48
56
|
},
|
|
57
|
+
/**
|
|
58
|
+
* Applies plugins to a Mongoose schema.
|
|
59
|
+
* This function filters out falsy plugins and applies the remaining valid plugins
|
|
60
|
+
* to the provided schema.
|
|
61
|
+
*
|
|
62
|
+
* @param schema - The Mongoose schema to apply plugins to.
|
|
63
|
+
* @param plugins - An array of plugin functions or false values to filter and apply.
|
|
64
|
+
*/
|
|
49
65
|
applyPlugins(n, t) {
|
|
50
66
|
t.filter((e) => typeof e == "function").forEach((e) => n.plugin(e));
|
|
51
67
|
},
|
|
68
|
+
/**
|
|
69
|
+
* Applies middleware functions to a Mongoose schema.
|
|
70
|
+
* This function configures pre and post middleware for specified methods on the schema.
|
|
71
|
+
*
|
|
72
|
+
* @param schema - The Mongoose schema to apply middleware to.
|
|
73
|
+
* @param middlewares - An array of middleware configurations with method, pre, and post functions.
|
|
74
|
+
*/
|
|
52
75
|
applyMiddlewares(n, t) {
|
|
53
76
|
t.forEach(({ method: e, pre: r, post: s }) => {
|
|
54
77
|
e && r && n.pre(e, r), e && s && n.post(e, s);
|
|
55
78
|
});
|
|
56
79
|
},
|
|
80
|
+
/**
|
|
81
|
+
* Creates a generic Mongoose schema with common fields.
|
|
82
|
+
* This function creates a base schema with UUID field and deletion flag,
|
|
83
|
+
* configured with automatic timestamps.
|
|
84
|
+
*
|
|
85
|
+
* @param mongoose - The Mongoose instance to create the schema with.
|
|
86
|
+
* @returns A Mongoose schema with generic document fields.
|
|
87
|
+
*/
|
|
57
88
|
createGenericSchema(n) {
|
|
58
89
|
return new n.Schema(
|
|
59
90
|
{
|
|
60
|
-
id: { type: String, default:
|
|
91
|
+
id: { type: String, default: F, unique: !0 },
|
|
61
92
|
isDel: { type: Boolean, default: !1 }
|
|
62
93
|
},
|
|
63
94
|
{ timestamps: !0 }
|
|
64
95
|
);
|
|
65
96
|
},
|
|
97
|
+
/**
|
|
98
|
+
* Creates a Mongoose schema with optional virtual fields and generic fields.
|
|
99
|
+
* This function creates a new Mongoose schema from the provided schema definition,
|
|
100
|
+
* optionally adds virtual fields, and includes generic fields (unless standalone is true).
|
|
101
|
+
*
|
|
102
|
+
* @param options - Configuration options including mongoose instance, schema definition, virtuals, and standalone flag.
|
|
103
|
+
* @param options.mongoose - The Mongoose instance to use for schema creation.
|
|
104
|
+
* @param options.schema - The schema definition object.
|
|
105
|
+
* @param options.virtuals - Optional array of virtual field configurations.
|
|
106
|
+
* @param options.standalone - Whether to exclude generic fields (default: false).
|
|
107
|
+
* @returns A configured Mongoose schema.
|
|
108
|
+
*/
|
|
66
109
|
createSchema({
|
|
67
110
|
mongoose: n,
|
|
68
111
|
schema: t,
|
|
@@ -70,41 +113,90 @@ const h = {
|
|
|
70
113
|
standalone: r = !1
|
|
71
114
|
}) {
|
|
72
115
|
const s = new n.Schema(t);
|
|
73
|
-
return e.forEach(({ name:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
116
|
+
return e.forEach(({ name: c, options: u, get: o }) => {
|
|
117
|
+
if (b.isDynamicVirtual(u)) {
|
|
118
|
+
const i = s.statics;
|
|
119
|
+
i._dynamicVirtuals || (i._dynamicVirtuals = []), i._dynamicVirtuals.push({
|
|
120
|
+
name: c,
|
|
121
|
+
options: u
|
|
122
|
+
});
|
|
123
|
+
const l = s.virtual(c);
|
|
124
|
+
o ? l.get(o) : l.get(function() {
|
|
125
|
+
var d;
|
|
126
|
+
return ((d = this._populated) == null ? void 0 : d[c]) || (u != null && u.count ? 0 : u != null && u.justOne ? null : []);
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
const i = s.virtual(c, u);
|
|
130
|
+
o && i.get(o);
|
|
131
|
+
}
|
|
132
|
+
}), r || s.add(b.createGenericSchema(n)), s;
|
|
77
133
|
},
|
|
134
|
+
/**
|
|
135
|
+
* Creates a Mongoose model with plugins, middleware, and pagination support.
|
|
136
|
+
* This function creates a model from a schema with optional pagination and aggregation plugins,
|
|
137
|
+
* and applies any specified middleware. If a model with the same name already exists, it returns the existing model.
|
|
138
|
+
*
|
|
139
|
+
* @param options - Configuration options including mongoose instance, model name, schema, and feature flags.
|
|
140
|
+
* @param options.mongoose - The Mongoose instance to use for model creation.
|
|
141
|
+
* @param options.name - The name of the model to create.
|
|
142
|
+
* @param options.schema - The schema definition for the model.
|
|
143
|
+
* @param options.pagination - Whether to enable pagination plugin (default: false).
|
|
144
|
+
* @param options.aggregate - Whether to enable aggregation pagination plugin (default: false).
|
|
145
|
+
* @param options.virtuals - Optional array of virtual field configurations.
|
|
146
|
+
* @param options.middlewares - Optional array of middleware configurations.
|
|
147
|
+
* @returns A configured Mongoose model with extended functionality.
|
|
148
|
+
* @throws {Error} When the model name is not provided.
|
|
149
|
+
*/
|
|
78
150
|
createModel({
|
|
79
151
|
mongoose: n,
|
|
80
152
|
name: t,
|
|
81
153
|
schema: e,
|
|
82
154
|
pagination: r = !1,
|
|
83
155
|
aggregate: s = !1,
|
|
84
|
-
virtuals:
|
|
85
|
-
middlewares:
|
|
156
|
+
virtuals: c = [],
|
|
157
|
+
middlewares: u = []
|
|
86
158
|
}) {
|
|
87
159
|
if (!t)
|
|
88
160
|
throw new Error("Model name is required.");
|
|
89
161
|
if (n.models[t])
|
|
90
162
|
return n.models[t];
|
|
91
|
-
const
|
|
92
|
-
return
|
|
93
|
-
r &&
|
|
94
|
-
s &&
|
|
95
|
-
]),
|
|
163
|
+
const o = b.createSchema({ mongoose: n, schema: e, virtuals: c });
|
|
164
|
+
return b.applyPlugins(o, [
|
|
165
|
+
r && Q,
|
|
166
|
+
s && W
|
|
167
|
+
]), b.applyMiddlewares(o, u), n.model(t, o);
|
|
96
168
|
},
|
|
169
|
+
/**
|
|
170
|
+
* Validation utilities for Mongoose schemas.
|
|
171
|
+
* This object provides common validation functions that can be used in Mongoose schema definitions.
|
|
172
|
+
*/
|
|
97
173
|
validator: {
|
|
174
|
+
/**
|
|
175
|
+
* Creates a required field validator.
|
|
176
|
+
* This function returns a validator that checks if a field value is not empty
|
|
177
|
+
* using the validate.isEmpty utility.
|
|
178
|
+
*
|
|
179
|
+
* @returns A validation function that returns true if the field is not empty.
|
|
180
|
+
*/
|
|
98
181
|
isRequired() {
|
|
99
182
|
return function(n) {
|
|
100
|
-
return
|
|
101
|
-
return
|
|
183
|
+
return a(this, null, function* () {
|
|
184
|
+
return !ee.isEmpty(n);
|
|
102
185
|
});
|
|
103
186
|
};
|
|
104
187
|
},
|
|
188
|
+
/**
|
|
189
|
+
* Creates a unique field validator.
|
|
190
|
+
* This function returns a validator that checks if a field value is unique
|
|
191
|
+
* across the specified fields in the collection.
|
|
192
|
+
*
|
|
193
|
+
* @param fields - An array of field names to check for uniqueness.
|
|
194
|
+
* @returns A validation function that returns true if the value is unique across the specified fields.
|
|
195
|
+
* @throws {Error} When fields is not a non-empty array of strings.
|
|
196
|
+
*/
|
|
105
197
|
isUnique(n) {
|
|
106
198
|
return function(t) {
|
|
107
|
-
return
|
|
199
|
+
return a(this, null, function* () {
|
|
108
200
|
if (!Array.isArray(n) || n.length === 0)
|
|
109
201
|
throw new Error("Fields must be a non-empty array of strings.");
|
|
110
202
|
const e = { $or: n.map((s) => ({ [s]: t })) };
|
|
@@ -112,9 +204,18 @@ const h = {
|
|
|
112
204
|
});
|
|
113
205
|
};
|
|
114
206
|
},
|
|
207
|
+
/**
|
|
208
|
+
* Creates a regex pattern validator.
|
|
209
|
+
* This function returns a validator that checks if a string value matches
|
|
210
|
+
* all provided regular expressions.
|
|
211
|
+
*
|
|
212
|
+
* @param regexArray - An array of regular expressions to test against the value.
|
|
213
|
+
* @returns A validation function that returns true if the value matches all regex patterns.
|
|
214
|
+
* @throws {Error} When regexArray is not an array of valid RegExp objects.
|
|
215
|
+
*/
|
|
115
216
|
matchesRegex(n) {
|
|
116
217
|
return function(t) {
|
|
117
|
-
return
|
|
218
|
+
return a(this, null, function* () {
|
|
118
219
|
if (!Array.isArray(n) || n.some((e) => !(e instanceof RegExp)))
|
|
119
220
|
throw new Error("regexArray must be an array of valid RegExp objects.");
|
|
120
221
|
return n.every((e) => e.test(t));
|
|
@@ -122,45 +223,213 @@ const h = {
|
|
|
122
223
|
};
|
|
123
224
|
}
|
|
124
225
|
},
|
|
125
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Migration utilities for MongoDB.
|
|
228
|
+
* This object extends the migrate-mongo library with additional configuration utilities.
|
|
229
|
+
*/
|
|
230
|
+
migrate: w(E({}, k), {
|
|
231
|
+
/**
|
|
232
|
+
* Sets the migration configuration and updates .gitignore.
|
|
233
|
+
* This function creates a migration configuration file and ensures it's properly
|
|
234
|
+
* excluded from version control.
|
|
235
|
+
*
|
|
236
|
+
* @param options - Migration configuration options to write to the config file.
|
|
237
|
+
*/
|
|
126
238
|
setConfig: (n) => {
|
|
127
239
|
const t = `// This file is automatically generated by the Cyberskill CLI.
|
|
128
240
|
module.exports = ${JSON.stringify(n, null, 4)}`;
|
|
129
|
-
|
|
241
|
+
G(x.MIGRATE_MONGO_CONFIG, t);
|
|
130
242
|
const e = `
|
|
131
|
-
${
|
|
243
|
+
${q}
|
|
132
244
|
`;
|
|
133
|
-
|
|
134
|
-
`).includes(
|
|
245
|
+
X(x.GIT_IGNORE) ? Y(x.GIT_IGNORE, "utf-8").split(`
|
|
246
|
+
`).includes(q) || Z(x.GIT_IGNORE, e) : G(x.GIT_IGNORE, e);
|
|
135
247
|
}
|
|
136
248
|
}),
|
|
249
|
+
/**
|
|
250
|
+
* Converts string values in a filter to regex patterns for case-insensitive search.
|
|
251
|
+
* This function recursively processes a filter object and converts string values in specified fields
|
|
252
|
+
* to MongoDB regex patterns that support accented character matching.
|
|
253
|
+
*
|
|
254
|
+
* @param filter - The filter object to process.
|
|
255
|
+
* @param fields - An array of field names to convert to regex patterns.
|
|
256
|
+
* @returns A new filter object with string values converted to regex patterns.
|
|
257
|
+
*/
|
|
137
258
|
regexify(n, t) {
|
|
138
259
|
if (!n)
|
|
139
260
|
return {};
|
|
140
|
-
let e =
|
|
261
|
+
let e = U(n);
|
|
141
262
|
if (!t || t.length === 0)
|
|
142
263
|
return e;
|
|
143
264
|
for (const r of t) {
|
|
144
|
-
const s = r.toString().split("."),
|
|
145
|
-
if (typeof
|
|
146
|
-
const
|
|
147
|
-
$regex: `.*${
|
|
265
|
+
const s = r.toString().split("."), c = H(e, s);
|
|
266
|
+
if (typeof c == "string" && c.length > 0) {
|
|
267
|
+
const u = {
|
|
268
|
+
$regex: `.*${J(c)}.*`,
|
|
148
269
|
$options: "i"
|
|
149
270
|
};
|
|
150
|
-
e =
|
|
271
|
+
e = z(e, s, u);
|
|
151
272
|
}
|
|
152
273
|
}
|
|
153
274
|
return e;
|
|
275
|
+
},
|
|
276
|
+
/**
|
|
277
|
+
* Checks if a virtual options object has a dynamic ref function.
|
|
278
|
+
*
|
|
279
|
+
* @param options - The virtual options to check.
|
|
280
|
+
* @returns True if the options contain a dynamic ref function.
|
|
281
|
+
*/
|
|
282
|
+
isDynamicVirtual(n) {
|
|
283
|
+
return !!(n && typeof n.ref == "function");
|
|
154
284
|
}
|
|
155
285
|
};
|
|
156
|
-
|
|
286
|
+
function le(n) {
|
|
287
|
+
return n !== null && typeof n == "object" && "toObject" in n && typeof n.toObject == "function";
|
|
288
|
+
}
|
|
289
|
+
function ae(n, t, e) {
|
|
290
|
+
if (!n.length || !t || !(e != null && e.ref))
|
|
291
|
+
return [];
|
|
292
|
+
const r = /* @__PURE__ */ new Map();
|
|
293
|
+
return n.forEach((s) => {
|
|
294
|
+
try {
|
|
295
|
+
const c = e.ref(s);
|
|
296
|
+
if (c == null)
|
|
297
|
+
return;
|
|
298
|
+
const u = typeof c == "string" ? c : String(c);
|
|
299
|
+
if (u && u.trim() !== "") {
|
|
300
|
+
const o = ie(u);
|
|
301
|
+
r.has(o) || r.set(o, []), r.get(o).push(s);
|
|
302
|
+
}
|
|
303
|
+
} catch (c) {
|
|
304
|
+
f(new Error(`Dynamic ref function failed for virtual "${t}": ${c instanceof Error ? c.message : String(c)}`));
|
|
305
|
+
}
|
|
306
|
+
}), Array.from(r.entries()).map(([s, c]) => ({ model: s, docs: c }));
|
|
307
|
+
}
|
|
308
|
+
function P(n, t, e, r, s) {
|
|
309
|
+
return a(this, null, function* () {
|
|
310
|
+
if (!t.length || !e.length || !r)
|
|
311
|
+
return t;
|
|
312
|
+
const c = e.filter((i) => {
|
|
313
|
+
if (Array.isArray(r))
|
|
314
|
+
return r.length > 0 && r.some((l) => typeof l == "string" ? l === i.name : l.path === i.name);
|
|
315
|
+
if (typeof r == "string")
|
|
316
|
+
return r === i.name;
|
|
317
|
+
if (typeof r == "object" && r !== null) {
|
|
318
|
+
const l = r;
|
|
319
|
+
return l.path && l.path === i.name || l.populate && l.populate === i.name;
|
|
320
|
+
}
|
|
321
|
+
return !1;
|
|
322
|
+
});
|
|
323
|
+
if (c.length === 0)
|
|
324
|
+
return t;
|
|
325
|
+
const u = U(t.map(
|
|
326
|
+
(i) => le(i) ? i.toObject() : i
|
|
327
|
+
));
|
|
328
|
+
u.forEach((i) => {
|
|
329
|
+
c.forEach(({ name: l, options: d }) => {
|
|
330
|
+
if (!(l in i)) {
|
|
331
|
+
const m = i;
|
|
332
|
+
m[l] = d.count ? 0 : d.justOne ? null : [];
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
const o = /* @__PURE__ */ new Map();
|
|
337
|
+
for (const i of c) {
|
|
338
|
+
const { name: l, options: d } = i, m = ae(u, l, d);
|
|
339
|
+
for (const p of m) {
|
|
340
|
+
o.has(p.model) || o.set(p.model, {
|
|
341
|
+
virtuals: [],
|
|
342
|
+
localValueSets: /* @__PURE__ */ new Map(),
|
|
343
|
+
docsByLocalValue: /* @__PURE__ */ new Map()
|
|
344
|
+
});
|
|
345
|
+
const h = o.get(p.model);
|
|
346
|
+
h.virtuals.find((N) => N.name === l) || (h.virtuals.push(i), h.localValueSets.set(l, /* @__PURE__ */ new Set()));
|
|
347
|
+
const M = h.localValueSets.get(l);
|
|
348
|
+
p.docs.forEach((N) => {
|
|
349
|
+
const C = N[d.localField];
|
|
350
|
+
if (C != null) {
|
|
351
|
+
const _ = String(C);
|
|
352
|
+
M.add(_);
|
|
353
|
+
let v = -1;
|
|
354
|
+
const D = N;
|
|
355
|
+
D.id !== void 0 ? v = u.findIndex((V) => V.id === D.id) : D._id !== void 0 && (v = u.findIndex((V) => {
|
|
356
|
+
var O, g, S, y;
|
|
357
|
+
return ((g = (O = V._id) == null ? void 0 : O.toString) == null ? void 0 : g.call(O)) === ((y = (S = D._id) == null ? void 0 : S.toString) == null ? void 0 : y.call(S));
|
|
358
|
+
})), v !== -1 && (h.docsByLocalValue.has(_) || h.docsByLocalValue.set(_, []), h.docsByLocalValue.get(_).push(v));
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return yield Promise.all(Array.from(o.entries()).map((i) => a(null, [i], function* ([l, d]) {
|
|
364
|
+
const m = n.models[l];
|
|
365
|
+
if (!m)
|
|
366
|
+
return;
|
|
367
|
+
const p = /* @__PURE__ */ new Set();
|
|
368
|
+
if (d.localValueSets.forEach((_) => {
|
|
369
|
+
_.forEach((v) => p.add(v));
|
|
370
|
+
}), p.size === 0)
|
|
371
|
+
return;
|
|
372
|
+
const h = [...new Set(d.virtuals.map((_) => _.options.foreignField))], M = Array.from(p);
|
|
373
|
+
let N;
|
|
374
|
+
h.length === 1 ? N = { [String(h[0])]: { $in: M } } : N = { $or: h.map((_) => ({ [_]: { $in: M } })) };
|
|
375
|
+
const C = yield m.find(N, s).lean();
|
|
376
|
+
for (const _ of d.virtuals) {
|
|
377
|
+
const { name: v, options: D } = _, V = C.filter((O) => {
|
|
378
|
+
const g = O[D.foreignField];
|
|
379
|
+
return g != null && p.has(String(g));
|
|
380
|
+
});
|
|
381
|
+
if (D.count) {
|
|
382
|
+
const O = /* @__PURE__ */ new Map();
|
|
383
|
+
V.forEach((g) => {
|
|
384
|
+
var S;
|
|
385
|
+
const y = (S = g[D.foreignField]) == null ? void 0 : S.toString();
|
|
386
|
+
y && O.set(y, (O.get(y) || 0) + 1);
|
|
387
|
+
}), d.localValueSets.get(v).forEach((g) => {
|
|
388
|
+
const S = d.docsByLocalValue.get(g) || [], y = O.get(g) || 0;
|
|
389
|
+
S.forEach((A) => {
|
|
390
|
+
const I = u[A];
|
|
391
|
+
I[v] === void 0 && (I[v] = y);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
} else {
|
|
395
|
+
const O = /* @__PURE__ */ new Map();
|
|
396
|
+
V.forEach((g) => {
|
|
397
|
+
var S;
|
|
398
|
+
const y = (S = g[D.foreignField]) == null ? void 0 : S.toString();
|
|
399
|
+
y && (O.has(y) || O.set(y, []), O.get(y).push(g));
|
|
400
|
+
}), d.localValueSets.get(v).forEach((g) => {
|
|
401
|
+
const S = d.docsByLocalValue.get(g) || [], y = O.get(g) || [], A = D.justOne ? y[0] || null : y;
|
|
402
|
+
S.forEach((I) => {
|
|
403
|
+
const B = u[I];
|
|
404
|
+
B[v] = A;
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}))), u;
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
class Ne {
|
|
413
|
+
/**
|
|
414
|
+
* Creates a new MongoDB controller instance.
|
|
415
|
+
*
|
|
416
|
+
* @param db - The MongoDB database instance.
|
|
417
|
+
* @param collectionName - The name of the collection to operate on.
|
|
418
|
+
*/
|
|
157
419
|
constructor(t, e) {
|
|
158
|
-
|
|
420
|
+
oe(this, "collection"), this.collection = t.collection(e);
|
|
159
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* Creates a single document in the collection.
|
|
424
|
+
* This method adds generic fields (id, isDel, timestamps) to the document before insertion.
|
|
425
|
+
*
|
|
426
|
+
* @param document - The document to create, with or without generic fields.
|
|
427
|
+
* @returns A promise that resolves to a standardized response with the created document.
|
|
428
|
+
*/
|
|
160
429
|
createOne(t) {
|
|
161
|
-
return
|
|
430
|
+
return a(this, null, function* () {
|
|
162
431
|
try {
|
|
163
|
-
const e =
|
|
432
|
+
const e = E(E({}, b.createGenericFields()), t);
|
|
164
433
|
return (yield this.collection.insertOne(e)).acknowledged ? {
|
|
165
434
|
success: !0,
|
|
166
435
|
message: "Document created successfully",
|
|
@@ -168,43 +437,62 @@ class ce {
|
|
|
168
437
|
} : {
|
|
169
438
|
success: !1,
|
|
170
439
|
message: "Document creation failed",
|
|
171
|
-
code:
|
|
440
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
172
441
|
};
|
|
173
442
|
} catch (e) {
|
|
174
|
-
return
|
|
443
|
+
return f(e);
|
|
175
444
|
}
|
|
176
445
|
});
|
|
177
446
|
}
|
|
447
|
+
/**
|
|
448
|
+
* Creates multiple documents in the collection.
|
|
449
|
+
* This method adds generic fields to each document before bulk insertion.
|
|
450
|
+
*
|
|
451
|
+
* @param documents - An array of documents to create.
|
|
452
|
+
* @returns A promise that resolves to a standardized response with the created documents.
|
|
453
|
+
*/
|
|
178
454
|
createMany(t) {
|
|
179
|
-
return
|
|
455
|
+
return a(this, null, function* () {
|
|
180
456
|
try {
|
|
181
|
-
const e = t.map((s) =>
|
|
457
|
+
const e = t.map((s) => E(E({}, b.createGenericFields()), s)), r = yield this.collection.insertMany(e);
|
|
182
458
|
return r.insertedCount === 0 ? {
|
|
183
459
|
success: !1,
|
|
184
460
|
message: "No documents were inserted",
|
|
185
|
-
code:
|
|
461
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
186
462
|
} : {
|
|
187
463
|
success: !0,
|
|
188
464
|
message: `${r.insertedCount} documents created successfully`,
|
|
189
465
|
result: e
|
|
190
466
|
};
|
|
191
467
|
} catch (e) {
|
|
192
|
-
return
|
|
468
|
+
return f(e);
|
|
193
469
|
}
|
|
194
470
|
});
|
|
195
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* Finds a single document by filter criteria.
|
|
474
|
+
*
|
|
475
|
+
* @param filter - The filter criteria to find the document.
|
|
476
|
+
* @returns A promise that resolves to a standardized response with the found document.
|
|
477
|
+
*/
|
|
196
478
|
findOne(t) {
|
|
197
|
-
return
|
|
479
|
+
return a(this, null, function* () {
|
|
198
480
|
try {
|
|
199
481
|
const e = yield this.collection.findOne(t);
|
|
200
|
-
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code:
|
|
482
|
+
return e ? { success: !0, message: "Document found", result: e } : { success: !1, message: "Document not found", code: R.NOT_FOUND.CODE };
|
|
201
483
|
} catch (e) {
|
|
202
|
-
return
|
|
484
|
+
return f(e);
|
|
203
485
|
}
|
|
204
486
|
});
|
|
205
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* Finds all documents matching the filter criteria.
|
|
490
|
+
*
|
|
491
|
+
* @param filter - The filter criteria to find documents (defaults to empty object for all documents).
|
|
492
|
+
* @returns A promise that resolves to a standardized response with the found documents.
|
|
493
|
+
*/
|
|
206
494
|
findAll() {
|
|
207
|
-
return
|
|
495
|
+
return a(this, arguments, function* (t = {}) {
|
|
208
496
|
try {
|
|
209
497
|
return {
|
|
210
498
|
success: !0,
|
|
@@ -212,25 +500,39 @@ class ce {
|
|
|
212
500
|
result: yield this.collection.find(t).toArray()
|
|
213
501
|
};
|
|
214
502
|
} catch (e) {
|
|
215
|
-
return
|
|
503
|
+
return f(e);
|
|
216
504
|
}
|
|
217
505
|
});
|
|
218
506
|
}
|
|
507
|
+
/**
|
|
508
|
+
* Counts documents matching the filter criteria.
|
|
509
|
+
*
|
|
510
|
+
* @param filter - The filter criteria to count documents (defaults to empty object for all documents).
|
|
511
|
+
* @returns A promise that resolves to a standardized response with the document count.
|
|
512
|
+
*/
|
|
219
513
|
count() {
|
|
220
|
-
return
|
|
514
|
+
return a(this, arguments, function* (t = {}) {
|
|
221
515
|
try {
|
|
516
|
+
const e = yield this.collection.countDocuments(t);
|
|
222
517
|
return {
|
|
223
518
|
success: !0,
|
|
224
|
-
message:
|
|
225
|
-
result:
|
|
519
|
+
message: `${e} documents counted successfully`,
|
|
520
|
+
result: e
|
|
226
521
|
};
|
|
227
522
|
} catch (e) {
|
|
228
|
-
return
|
|
523
|
+
return f(e);
|
|
229
524
|
}
|
|
230
525
|
});
|
|
231
526
|
}
|
|
527
|
+
/**
|
|
528
|
+
* Updates a single document matching the filter criteria.
|
|
529
|
+
*
|
|
530
|
+
* @param filter - The filter criteria to find the document to update.
|
|
531
|
+
* @param update - The update data to apply to the document.
|
|
532
|
+
* @returns A promise that resolves to a standardized response with the update result.
|
|
533
|
+
*/
|
|
232
534
|
updateOne(t, e) {
|
|
233
|
-
return
|
|
535
|
+
return a(this, null, function* () {
|
|
234
536
|
try {
|
|
235
537
|
const r = yield this.collection.updateOne(t, {
|
|
236
538
|
$set: e
|
|
@@ -238,19 +540,26 @@ class ce {
|
|
|
238
540
|
return r.matchedCount === 0 ? {
|
|
239
541
|
success: !1,
|
|
240
542
|
message: "No documents matched the filter",
|
|
241
|
-
code:
|
|
543
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
242
544
|
} : {
|
|
243
545
|
success: !0,
|
|
244
546
|
message: "Document updated successfully",
|
|
245
547
|
result: r
|
|
246
548
|
};
|
|
247
549
|
} catch (r) {
|
|
248
|
-
return
|
|
550
|
+
return f(r);
|
|
249
551
|
}
|
|
250
552
|
});
|
|
251
553
|
}
|
|
554
|
+
/**
|
|
555
|
+
* Updates multiple documents matching the filter criteria.
|
|
556
|
+
*
|
|
557
|
+
* @param filter - The filter criteria to find documents to update.
|
|
558
|
+
* @param update - The update data to apply to the documents.
|
|
559
|
+
* @returns A promise that resolves to a standardized response with the update result.
|
|
560
|
+
*/
|
|
252
561
|
updateMany(t, e) {
|
|
253
|
-
return
|
|
562
|
+
return a(this, null, function* () {
|
|
254
563
|
try {
|
|
255
564
|
const r = yield this.collection.updateMany(t, {
|
|
256
565
|
$set: e
|
|
@@ -258,300 +567,487 @@ class ce {
|
|
|
258
567
|
return r.matchedCount === 0 ? {
|
|
259
568
|
success: !1,
|
|
260
569
|
message: "No documents matched the filter",
|
|
261
|
-
code:
|
|
570
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
262
571
|
} : {
|
|
263
572
|
success: !0,
|
|
264
573
|
message: "Documents updated successfully",
|
|
265
574
|
result: r
|
|
266
575
|
};
|
|
267
576
|
} catch (r) {
|
|
268
|
-
return
|
|
577
|
+
return f(r);
|
|
269
578
|
}
|
|
270
579
|
});
|
|
271
580
|
}
|
|
581
|
+
/**
|
|
582
|
+
* Deletes a single document matching the filter criteria.
|
|
583
|
+
*
|
|
584
|
+
* @param filter - The filter criteria to find the document to delete.
|
|
585
|
+
* @returns A promise that resolves to a standardized response with the delete result.
|
|
586
|
+
*/
|
|
272
587
|
deleteOne(t) {
|
|
273
|
-
return
|
|
588
|
+
return a(this, null, function* () {
|
|
274
589
|
try {
|
|
275
590
|
const e = yield this.collection.deleteOne(t);
|
|
276
591
|
return e.deletedCount === 0 ? {
|
|
277
592
|
success: !1,
|
|
278
593
|
message: "No documents matched the filter",
|
|
279
|
-
code:
|
|
594
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
280
595
|
} : {
|
|
281
596
|
success: !0,
|
|
282
597
|
message: "Document deleted successfully",
|
|
283
598
|
result: e
|
|
284
599
|
};
|
|
285
600
|
} catch (e) {
|
|
286
|
-
return
|
|
601
|
+
return f(e);
|
|
287
602
|
}
|
|
288
603
|
});
|
|
289
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* Deletes multiple documents matching the filter criteria.
|
|
607
|
+
*
|
|
608
|
+
* @param filter - The filter criteria to find documents to delete.
|
|
609
|
+
* @returns A promise that resolves to a standardized response with the delete result.
|
|
610
|
+
*/
|
|
290
611
|
deleteMany(t) {
|
|
291
|
-
return
|
|
612
|
+
return a(this, null, function* () {
|
|
292
613
|
try {
|
|
293
614
|
const e = yield this.collection.deleteMany(t);
|
|
294
615
|
return e.deletedCount === 0 ? {
|
|
295
616
|
success: !1,
|
|
296
617
|
message: "No documents matched the filter",
|
|
297
|
-
code:
|
|
618
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
298
619
|
} : {
|
|
299
620
|
success: !0,
|
|
300
621
|
message: "Documents deleted successfully",
|
|
301
622
|
result: e
|
|
302
623
|
};
|
|
303
624
|
} catch (e) {
|
|
304
|
-
return
|
|
625
|
+
return f(e);
|
|
305
626
|
}
|
|
306
627
|
});
|
|
307
628
|
}
|
|
308
629
|
}
|
|
309
|
-
class
|
|
630
|
+
class be {
|
|
631
|
+
/**
|
|
632
|
+
* Creates a new Mongoose controller instance.
|
|
633
|
+
*
|
|
634
|
+
* @param model - The Mongoose model to operate on.
|
|
635
|
+
*/
|
|
310
636
|
constructor(t) {
|
|
311
637
|
this.model = t;
|
|
312
638
|
}
|
|
639
|
+
/**
|
|
640
|
+
* Gets the model name for logging and error messages.
|
|
641
|
+
*
|
|
642
|
+
* @returns The name of the model.
|
|
643
|
+
*/
|
|
313
644
|
getModelName() {
|
|
314
645
|
return this.model.modelName;
|
|
315
646
|
}
|
|
647
|
+
/**
|
|
648
|
+
* Finds a single document with optional population and projection.
|
|
649
|
+
* Automatically handles dynamic virtual population if configured.
|
|
650
|
+
*
|
|
651
|
+
* @param filter - The filter criteria to find the document.
|
|
652
|
+
* @param projection - The fields to include/exclude in the result.
|
|
653
|
+
* @param options - Query options for the operation.
|
|
654
|
+
* @param populate - Population configuration for related documents.
|
|
655
|
+
* @returns A promise that resolves to a standardized response with the found document.
|
|
656
|
+
*/
|
|
316
657
|
findOne() {
|
|
317
|
-
return
|
|
658
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, s) {
|
|
318
659
|
try {
|
|
319
|
-
const
|
|
320
|
-
s &&
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
660
|
+
const c = this.model.findOne(t, e, r), o = this.model.schema.statics._dynamicVirtuals;
|
|
661
|
+
s && (!o || o.length === 0) && c.populate(s);
|
|
662
|
+
const i = yield c.exec();
|
|
663
|
+
if (!i)
|
|
664
|
+
return {
|
|
665
|
+
success: !1,
|
|
666
|
+
message: `No ${this.getModelName()} found.`,
|
|
667
|
+
code: R.NOT_FOUND.CODE
|
|
668
|
+
};
|
|
669
|
+
let l = i;
|
|
670
|
+
if (o && o.length > 0) {
|
|
671
|
+
const d = yield P(this.model.base, [i], o, s);
|
|
672
|
+
l = d && d[0] ? d[0] : i;
|
|
673
|
+
}
|
|
674
|
+
return { success: !0, result: l };
|
|
675
|
+
} catch (c) {
|
|
676
|
+
return f(c);
|
|
329
677
|
}
|
|
330
678
|
});
|
|
331
679
|
}
|
|
680
|
+
/**
|
|
681
|
+
* Finds all documents with optional population and projection.
|
|
682
|
+
* Automatically handles dynamic virtual population if configured.
|
|
683
|
+
*
|
|
684
|
+
* @param filter - The filter criteria to find documents.
|
|
685
|
+
* @param projection - The fields to include/exclude in the result.
|
|
686
|
+
* @param options - Query options for the operation.
|
|
687
|
+
* @param populate - Population configuration for related documents.
|
|
688
|
+
* @returns A promise that resolves to a standardized response with the found documents.
|
|
689
|
+
*/
|
|
332
690
|
findAll() {
|
|
333
|
-
return
|
|
691
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, s) {
|
|
334
692
|
try {
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
693
|
+
const c = this.model.find(t, e, r), o = this.model.schema.statics._dynamicVirtuals;
|
|
694
|
+
s && (!o || o.length === 0) && c.populate(s);
|
|
695
|
+
const i = yield c.exec();
|
|
696
|
+
let l = i;
|
|
697
|
+
return o && o.length > 0 && i.length > 0 && (l = yield P(this.model.base, i, o, s)), { success: !0, result: l };
|
|
698
|
+
} catch (c) {
|
|
699
|
+
return f(c);
|
|
339
700
|
}
|
|
340
701
|
});
|
|
341
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* Finds documents with pagination support.
|
|
705
|
+
* Automatically handles dynamic virtual population if configured.
|
|
706
|
+
*
|
|
707
|
+
* @param filter - The filter criteria to find documents.
|
|
708
|
+
* @param options - Pagination options including page, limit, and population.
|
|
709
|
+
* @returns A promise that resolves to a standardized response with paginated results.
|
|
710
|
+
*/
|
|
342
711
|
findPaging() {
|
|
343
|
-
return
|
|
712
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
344
713
|
try {
|
|
345
|
-
|
|
714
|
+
const s = this.model.schema.statics._dynamicVirtuals, c = E({}, e);
|
|
715
|
+
s && s.length > 0 && (c.populate = void 0);
|
|
716
|
+
const u = yield this.model.paginate(t, c);
|
|
717
|
+
let o = u.docs;
|
|
718
|
+
return s && s.length > 0 && u.docs.length > 0 && (o = yield P(this.model.base, u.docs, s, e.populate)), { success: !0, result: w(E({}, u), { docs: o }) };
|
|
346
719
|
} catch (r) {
|
|
347
|
-
return
|
|
720
|
+
return f(r);
|
|
348
721
|
}
|
|
349
722
|
});
|
|
350
723
|
}
|
|
724
|
+
/**
|
|
725
|
+
* Performs aggregation with pagination support.
|
|
726
|
+
*
|
|
727
|
+
* @param pipeline - The aggregation pipeline stages.
|
|
728
|
+
* @param options - Pagination options for the aggregation result.
|
|
729
|
+
* @returns A promise that resolves to a standardized response with paginated aggregation results.
|
|
730
|
+
*/
|
|
351
731
|
findPagingAggregate(t) {
|
|
352
|
-
return
|
|
732
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
353
733
|
try {
|
|
354
|
-
|
|
734
|
+
const c = this.model.schema.statics._dynamicVirtuals, u = E({}, r);
|
|
735
|
+
c && c.length > 0 && (u.populate = void 0);
|
|
736
|
+
const o = yield this.model.aggregatePaginate(
|
|
355
737
|
this.model.aggregate(e),
|
|
356
|
-
|
|
357
|
-
)
|
|
738
|
+
u
|
|
739
|
+
);
|
|
740
|
+
let i = o.docs;
|
|
741
|
+
return c && c.length > 0 && o.docs.length > 0 && (i = yield P(this.model.base, o.docs, c, r.populate)), { success: !0, result: w(E({}, o), { docs: i }) };
|
|
358
742
|
} catch (s) {
|
|
359
|
-
return
|
|
743
|
+
return f(s);
|
|
360
744
|
}
|
|
361
745
|
});
|
|
362
746
|
}
|
|
747
|
+
/**
|
|
748
|
+
* Counts documents matching the filter criteria.
|
|
749
|
+
*
|
|
750
|
+
* @param filter - The filter criteria to count documents.
|
|
751
|
+
* @returns A promise that resolves to a standardized response with the document count.
|
|
752
|
+
*/
|
|
363
753
|
count() {
|
|
364
|
-
return
|
|
754
|
+
return a(this, arguments, function* (t = {}) {
|
|
365
755
|
try {
|
|
366
756
|
return { success: !0, result: yield this.model.countDocuments(t) };
|
|
367
757
|
} catch (e) {
|
|
368
|
-
return
|
|
758
|
+
return f(e);
|
|
369
759
|
}
|
|
370
760
|
});
|
|
371
761
|
}
|
|
762
|
+
/**
|
|
763
|
+
* Creates a single document.
|
|
764
|
+
*
|
|
765
|
+
* @param doc - The document to create.
|
|
766
|
+
* @returns A promise that resolves to a standardized response with the created document.
|
|
767
|
+
*/
|
|
372
768
|
createOne(t) {
|
|
373
|
-
return
|
|
769
|
+
return a(this, null, function* () {
|
|
374
770
|
try {
|
|
375
771
|
return { success: !0, result: yield this.model.create(t) };
|
|
376
772
|
} catch (e) {
|
|
377
|
-
return
|
|
773
|
+
return f(e);
|
|
378
774
|
}
|
|
379
775
|
});
|
|
380
776
|
}
|
|
777
|
+
/**
|
|
778
|
+
* Creates multiple documents with bulk insertion.
|
|
779
|
+
*
|
|
780
|
+
* @param docs - An array of documents to create.
|
|
781
|
+
* @param options - Options for the bulk insertion operation.
|
|
782
|
+
* @returns A promise that resolves to a standardized response with the created documents.
|
|
783
|
+
*/
|
|
381
784
|
createMany(t) {
|
|
382
|
-
return
|
|
785
|
+
return a(this, arguments, function* (e, r = {}) {
|
|
383
786
|
try {
|
|
384
|
-
return { success: !0, result: (yield this.model.insertMany(e, r)).map((
|
|
787
|
+
return { success: !0, result: (yield this.model.insertMany(e, r)).map((u) => u instanceof K ? u.toObject() : null).filter((u) => u !== null) };
|
|
385
788
|
} catch (s) {
|
|
386
|
-
return
|
|
789
|
+
return f(s);
|
|
387
790
|
}
|
|
388
791
|
});
|
|
389
792
|
}
|
|
793
|
+
/**
|
|
794
|
+
* Updates a single document and returns the updated version.
|
|
795
|
+
*
|
|
796
|
+
* @param filter - The filter criteria to find the document to update.
|
|
797
|
+
* @param update - The update data to apply.
|
|
798
|
+
* @param options - Options for the update operation.
|
|
799
|
+
* @returns A promise that resolves to a standardized response with the updated document.
|
|
800
|
+
*/
|
|
390
801
|
updateOne() {
|
|
391
|
-
return
|
|
802
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
392
803
|
try {
|
|
393
|
-
const s = yield this.model.findOneAndUpdate(t, e,
|
|
804
|
+
const s = yield this.model.findOneAndUpdate(t, e, E({
|
|
394
805
|
new: !0
|
|
395
806
|
}, r)).exec();
|
|
396
807
|
return s ? { success: !0, result: s } : {
|
|
397
808
|
success: !1,
|
|
398
809
|
message: `Failed to update ${this.getModelName()}.`,
|
|
399
|
-
code:
|
|
810
|
+
code: R.NOT_FOUND.CODE
|
|
400
811
|
};
|
|
401
812
|
} catch (s) {
|
|
402
|
-
return
|
|
813
|
+
return f(s);
|
|
403
814
|
}
|
|
404
815
|
});
|
|
405
816
|
}
|
|
817
|
+
/**
|
|
818
|
+
* Updates multiple documents matching the filter criteria.
|
|
819
|
+
*
|
|
820
|
+
* @param filter - The filter criteria to find documents to update.
|
|
821
|
+
* @param update - The update data to apply.
|
|
822
|
+
* @param options - Options for the update operation.
|
|
823
|
+
* @returns A promise that resolves to a standardized response with the update result.
|
|
824
|
+
*/
|
|
406
825
|
updateMany() {
|
|
407
|
-
return
|
|
826
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
408
827
|
try {
|
|
409
828
|
return { success: !0, result: yield this.model.updateMany(t, e, r).exec() };
|
|
410
829
|
} catch (s) {
|
|
411
|
-
return
|
|
830
|
+
return f(s);
|
|
412
831
|
}
|
|
413
832
|
});
|
|
414
833
|
}
|
|
834
|
+
/**
|
|
835
|
+
* Deletes a single document and returns the deleted version.
|
|
836
|
+
*
|
|
837
|
+
* @param filter - The filter criteria to find the document to delete.
|
|
838
|
+
* @param options - Options for the delete operation.
|
|
839
|
+
* @returns A promise that resolves to a standardized response with the deleted document.
|
|
840
|
+
*/
|
|
415
841
|
deleteOne() {
|
|
416
|
-
return
|
|
842
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
417
843
|
try {
|
|
418
844
|
const r = yield this.model.findOneAndDelete(t, e).exec();
|
|
419
845
|
return r ? { success: !0, result: r } : {
|
|
420
846
|
success: !1,
|
|
421
847
|
message: `No ${this.getModelName()} found to delete.`,
|
|
422
|
-
code:
|
|
848
|
+
code: R.NOT_FOUND.CODE
|
|
423
849
|
};
|
|
424
850
|
} catch (r) {
|
|
425
|
-
return
|
|
851
|
+
return f(r);
|
|
426
852
|
}
|
|
427
853
|
});
|
|
428
854
|
}
|
|
855
|
+
/**
|
|
856
|
+
* Deletes multiple documents matching the filter criteria.
|
|
857
|
+
*
|
|
858
|
+
* @param filter - The filter criteria to find documents to delete.
|
|
859
|
+
* @param options - Options for the delete operation.
|
|
860
|
+
* @returns A promise that resolves to a standardized response with the delete result.
|
|
861
|
+
*/
|
|
429
862
|
deleteMany() {
|
|
430
|
-
return
|
|
863
|
+
return a(this, arguments, function* (t = {}, e = {}) {
|
|
431
864
|
try {
|
|
432
865
|
const r = yield this.model.deleteMany(t, e).exec();
|
|
433
866
|
return r.deletedCount === 0 ? {
|
|
434
867
|
success: !1,
|
|
435
868
|
message: "No documents found to delete.",
|
|
436
|
-
code:
|
|
869
|
+
code: R.NOT_FOUND.CODE
|
|
437
870
|
} : { success: !0, result: r };
|
|
438
871
|
} catch (r) {
|
|
439
|
-
return
|
|
872
|
+
return f(r);
|
|
440
873
|
}
|
|
441
874
|
});
|
|
442
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Creates a unique short ID based on a given ID.
|
|
878
|
+
* This method generates multiple short IDs with increasing lengths and finds the first available one.
|
|
879
|
+
*
|
|
880
|
+
* @param id - The base ID to generate short IDs from.
|
|
881
|
+
* @param length - The initial length for short ID generation (default: 4).
|
|
882
|
+
* @returns A promise that resolves to a standardized response with the unique short ID.
|
|
883
|
+
*/
|
|
443
884
|
createShortId(t, e = 4) {
|
|
444
|
-
return
|
|
885
|
+
return a(this, null, function* () {
|
|
445
886
|
try {
|
|
446
|
-
const s =
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
887
|
+
const s = Array.from({ length: 10 }, (o, i) => te(t, i + e)), u = (yield Promise.all(
|
|
888
|
+
s.map((o) => this.model.exists({ shortId: o }))
|
|
889
|
+
)).findIndex((o) => !o);
|
|
890
|
+
if (u !== -1) {
|
|
891
|
+
const o = s[u];
|
|
892
|
+
if (o)
|
|
893
|
+
return { success: !0, result: o };
|
|
451
894
|
}
|
|
452
895
|
return {
|
|
453
896
|
success: !1,
|
|
454
897
|
message: "Failed to create a unique shortId",
|
|
455
|
-
code:
|
|
898
|
+
code: R.INTERNAL_SERVER_ERROR.CODE
|
|
456
899
|
};
|
|
457
900
|
} catch (r) {
|
|
458
|
-
return
|
|
901
|
+
return f(r);
|
|
459
902
|
}
|
|
460
903
|
});
|
|
461
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* Creates a query for slug existence checking.
|
|
907
|
+
* This method generates a query that checks for slug existence in both current and historical slug fields.
|
|
908
|
+
*
|
|
909
|
+
* @param options - Configuration for slug query generation including slug, field, and filter.
|
|
910
|
+
* @param options.slug - The slug string to check for existence.
|
|
911
|
+
* @param options.field - The field name for object-based slug checking.
|
|
912
|
+
* @param options.isObject - Whether the slug is stored as an object with nested fields.
|
|
913
|
+
* @param options.filter - Additional filter conditions to apply to the query.
|
|
914
|
+
* @returns A MongoDB query object for checking slug existence.
|
|
915
|
+
*/
|
|
462
916
|
createSlugQuery({ slug: t, field: e, isObject: r, filter: s }) {
|
|
463
|
-
const
|
|
464
|
-
return r ?
|
|
917
|
+
const c = E({}, s != null ? s : {});
|
|
918
|
+
return r ? w(E({}, c), {
|
|
465
919
|
$or: [
|
|
466
920
|
{ [`slug.${e}`]: t },
|
|
467
921
|
{ slugHistory: { $elemMatch: { [`slug.${e}`]: t } } }
|
|
468
922
|
]
|
|
469
|
-
}) :
|
|
923
|
+
}) : w(E({}, c), {
|
|
470
924
|
$or: [
|
|
471
925
|
{ slug: t },
|
|
472
926
|
{ slugHistory: t }
|
|
473
927
|
]
|
|
474
928
|
});
|
|
475
929
|
}
|
|
930
|
+
/**
|
|
931
|
+
* Creates a unique slug based on a given string.
|
|
932
|
+
* This method generates multiple slug variations and finds the first available one.
|
|
933
|
+
*
|
|
934
|
+
* @param options - Configuration for slug generation including slug, field, and filter.
|
|
935
|
+
* @param options.slug - The base slug string to make unique.
|
|
936
|
+
* @param options.field - The field name for object-based slug checking.
|
|
937
|
+
* @param options.isObject - Whether the slug is stored as an object with nested fields.
|
|
938
|
+
* @param options.filter - Additional filter conditions to apply when checking slug existence.
|
|
939
|
+
* @returns A promise that resolves to a unique slug string.
|
|
940
|
+
*/
|
|
476
941
|
createUniqueSlug(t) {
|
|
477
|
-
return
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
942
|
+
return a(this, arguments, function* ({ slug: e, field: r, isObject: s, filter: c }) {
|
|
943
|
+
const u = j(e), i = Array.from({ length: 100 }, (m, p) => p === 0 ? u : `${u}-${p}`), d = (yield Promise.all(
|
|
944
|
+
i.map(
|
|
945
|
+
(m) => this.model.exists(this.createSlugQuery({ slug: m, field: r, isObject: s, filter: c }))
|
|
946
|
+
)
|
|
947
|
+
)).findIndex((m) => !m);
|
|
948
|
+
if (d !== -1) {
|
|
949
|
+
const m = i[d];
|
|
950
|
+
if (m)
|
|
951
|
+
return m;
|
|
952
|
+
}
|
|
953
|
+
return `${u}-${Date.now()}`;
|
|
483
954
|
});
|
|
484
955
|
}
|
|
956
|
+
/**
|
|
957
|
+
* Creates a slug for a document field.
|
|
958
|
+
* This method handles both simple string fields and object fields with nested slug generation.
|
|
959
|
+
*
|
|
960
|
+
* @param options - Configuration for slug creation including field, source document, and filter.
|
|
961
|
+
* @param options.field - The field name to create a slug for.
|
|
962
|
+
* @param options.from - The source document containing the field value.
|
|
963
|
+
* @param options.filter - Additional filter conditions to apply when checking slug existence.
|
|
964
|
+
* @returns A promise that resolves to a standardized response with the created slug(s).
|
|
965
|
+
*/
|
|
485
966
|
createSlug(t) {
|
|
486
|
-
return
|
|
967
|
+
return a(this, arguments, function* ({ field: e, from: r, filter: s }) {
|
|
487
968
|
try {
|
|
488
|
-
const
|
|
489
|
-
return
|
|
969
|
+
const c = r[e];
|
|
970
|
+
return $(c) ? { success: !0, result: Object.fromEntries(
|
|
490
971
|
yield Promise.all(
|
|
491
|
-
Object.entries(
|
|
492
|
-
const
|
|
493
|
-
slug:
|
|
494
|
-
field:
|
|
972
|
+
Object.entries(c).map((l) => a(this, [l], function* ([d, m]) {
|
|
973
|
+
const p = yield this.createUniqueSlug({
|
|
974
|
+
slug: m,
|
|
975
|
+
field: d,
|
|
495
976
|
isObject: !0,
|
|
496
977
|
filter: s
|
|
497
978
|
});
|
|
498
|
-
return [
|
|
979
|
+
return [d, p];
|
|
499
980
|
}))
|
|
500
981
|
)
|
|
501
982
|
) } : { success: !0, result: yield this.createUniqueSlug({
|
|
502
|
-
slug:
|
|
983
|
+
slug: c,
|
|
503
984
|
field: e,
|
|
504
985
|
isObject: !1,
|
|
505
986
|
filter: s
|
|
506
987
|
}) };
|
|
507
|
-
} catch (
|
|
508
|
-
return
|
|
988
|
+
} catch (c) {
|
|
989
|
+
return f(c);
|
|
509
990
|
}
|
|
510
991
|
});
|
|
511
992
|
}
|
|
993
|
+
/**
|
|
994
|
+
* Checks if a slug already exists in the collection.
|
|
995
|
+
* This method verifies slug existence in both current and historical slug fields.
|
|
996
|
+
*
|
|
997
|
+
* @param options - Configuration for slug checking including slug, field, source document, and filter.
|
|
998
|
+
* @param options.slug - The slug string to check for existence.
|
|
999
|
+
* @param options.field - The field name for object-based slug checking.
|
|
1000
|
+
* @param options.from - The source document containing the field value.
|
|
1001
|
+
* @param options.filter - Additional filter conditions to apply to the query.
|
|
1002
|
+
* @returns A promise that resolves to a standardized response indicating whether the slug exists.
|
|
1003
|
+
*/
|
|
512
1004
|
checkSlug(t) {
|
|
513
|
-
return
|
|
1005
|
+
return a(this, arguments, function* ({ slug: e, field: r, from: s, filter: c }) {
|
|
514
1006
|
try {
|
|
515
|
-
const
|
|
516
|
-
if (
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
1007
|
+
const u = s[r];
|
|
1008
|
+
if ($(u)) {
|
|
1009
|
+
const m = Object.values(u).map((h) => j(h));
|
|
1010
|
+
return (yield Promise.all(
|
|
1011
|
+
m.map(
|
|
1012
|
+
(h) => this.model.exists(this.createSlugQuery({
|
|
1013
|
+
slug: h,
|
|
1014
|
+
field: r,
|
|
1015
|
+
isObject: !0,
|
|
1016
|
+
filter: c
|
|
1017
|
+
}))
|
|
1018
|
+
)
|
|
1019
|
+
)).some((h) => h) ? { success: !0, result: !0 } : { success: !0, result: !1 };
|
|
528
1020
|
}
|
|
529
|
-
const
|
|
1021
|
+
const i = j(e);
|
|
530
1022
|
return { success: !0, result: (yield this.model.exists(this.createSlugQuery({
|
|
531
|
-
slug:
|
|
1023
|
+
slug: i,
|
|
532
1024
|
field: r,
|
|
533
1025
|
isObject: !1,
|
|
534
|
-
filter:
|
|
1026
|
+
filter: c
|
|
535
1027
|
}))) !== null };
|
|
536
|
-
} catch (
|
|
537
|
-
return
|
|
1028
|
+
} catch (u) {
|
|
1029
|
+
return f(u);
|
|
538
1030
|
}
|
|
539
1031
|
});
|
|
540
1032
|
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Performs aggregation operations on the collection.
|
|
1035
|
+
*
|
|
1036
|
+
* @param pipeline - The aggregation pipeline stages to execute.
|
|
1037
|
+
* @returns A promise that resolves to a standardized response with the aggregation results.
|
|
1038
|
+
*/
|
|
541
1039
|
aggregate(t) {
|
|
542
|
-
return
|
|
1040
|
+
return a(this, null, function* () {
|
|
543
1041
|
try {
|
|
544
1042
|
return { success: !0, result: yield this.model.aggregate(t) };
|
|
545
1043
|
} catch (e) {
|
|
546
|
-
return
|
|
1044
|
+
return f(e);
|
|
547
1045
|
}
|
|
548
1046
|
});
|
|
549
1047
|
}
|
|
550
1048
|
}
|
|
551
1049
|
export {
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
h as mongo,
|
|
556
|
-
fe as mongoosePaginate
|
|
1050
|
+
Ne as MongoController,
|
|
1051
|
+
be as MongooseController,
|
|
1052
|
+
b as mongo
|
|
557
1053
|
};
|