@classytic/mongokit 3.2.0 → 3.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +470 -193
  2. package/dist/actions/index.d.mts +9 -0
  3. package/dist/actions/index.mjs +15 -0
  4. package/dist/aggregate-BAi4Do-X.mjs +767 -0
  5. package/dist/aggregate-CCHI7F51.d.mts +269 -0
  6. package/dist/ai/index.d.mts +125 -0
  7. package/dist/ai/index.mjs +203 -0
  8. package/dist/cache-keys-C8Z9B5sw.mjs +204 -0
  9. package/dist/chunk-DQk6qfdC.mjs +18 -0
  10. package/dist/create-BuO6xt0v.mjs +55 -0
  11. package/dist/custom-id.plugin-B_zIs6gE.mjs +1818 -0
  12. package/dist/custom-id.plugin-BzZI4gnE.d.mts +893 -0
  13. package/dist/index.d.mts +1012 -0
  14. package/dist/index.mjs +1906 -0
  15. package/dist/limits-DsNeCx4D.mjs +299 -0
  16. package/dist/logger-D8ily-PP.mjs +51 -0
  17. package/dist/mongooseToJsonSchema-COdDEkIJ.mjs +317 -0
  18. package/dist/{mongooseToJsonSchema-CaRF_bCN.d.ts → mongooseToJsonSchema-Wbvjfwkn.d.mts} +16 -89
  19. package/dist/pagination/PaginationEngine.d.mts +93 -0
  20. package/dist/pagination/PaginationEngine.mjs +196 -0
  21. package/dist/plugins/index.d.mts +3 -0
  22. package/dist/plugins/index.mjs +3 -0
  23. package/dist/types-D-gploPr.d.mts +1241 -0
  24. package/dist/utils/{index.d.ts → index.d.mts} +14 -21
  25. package/dist/utils/index.mjs +5 -0
  26. package/package.json +21 -21
  27. package/dist/actions/index.d.ts +0 -3
  28. package/dist/actions/index.js +0 -5
  29. package/dist/ai/index.d.ts +0 -175
  30. package/dist/ai/index.js +0 -206
  31. package/dist/chunks/chunk-2ZN65ZOP.js +0 -93
  32. package/dist/chunks/chunk-44KXLGPO.js +0 -388
  33. package/dist/chunks/chunk-DEVXDBRL.js +0 -1226
  34. package/dist/chunks/chunk-I7CWNAJB.js +0 -46
  35. package/dist/chunks/chunk-JWUAVZ3L.js +0 -8
  36. package/dist/chunks/chunk-UE2IEXZJ.js +0 -306
  37. package/dist/chunks/chunk-URLJFIR7.js +0 -22
  38. package/dist/chunks/chunk-VWKIKZYF.js +0 -737
  39. package/dist/chunks/chunk-WSFCRVEQ.js +0 -7
  40. package/dist/index-BDn5fSTE.d.ts +0 -516
  41. package/dist/index.d.ts +0 -1422
  42. package/dist/index.js +0 -1893
  43. package/dist/pagination/PaginationEngine.d.ts +0 -117
  44. package/dist/pagination/PaginationEngine.js +0 -3
  45. package/dist/plugins/index.d.ts +0 -922
  46. package/dist/plugins/index.js +0 -6
  47. package/dist/types-Jni1KgkP.d.ts +0 -780
  48. package/dist/utils/index.js +0 -5
@@ -1,46 +0,0 @@
1
- import { __export } from './chunk-WSFCRVEQ.js';
2
-
3
- // src/actions/create.ts
4
- var create_exports = {};
5
- __export(create_exports, {
6
- create: () => create,
7
- createDefault: () => createDefault,
8
- createMany: () => createMany,
9
- upsert: () => upsert
10
- });
11
- async function create(Model, data, options = {}) {
12
- const document = new Model(data);
13
- await document.save({ session: options.session });
14
- return document;
15
- }
16
- async function createMany(Model, dataArray, options = {}) {
17
- return Model.insertMany(dataArray, {
18
- session: options.session,
19
- ordered: options.ordered !== false
20
- });
21
- }
22
- async function createDefault(Model, overrides = {}, options = {}) {
23
- const defaults = {};
24
- Model.schema.eachPath((path, schemaType) => {
25
- const schemaOptions = schemaType.options;
26
- if (schemaOptions.default !== void 0 && path !== "_id") {
27
- defaults[path] = typeof schemaOptions.default === "function" ? schemaOptions.default() : schemaOptions.default;
28
- }
29
- });
30
- return create(Model, { ...defaults, ...overrides }, options);
31
- }
32
- async function upsert(Model, query, data, options = {}) {
33
- return Model.findOneAndUpdate(
34
- query,
35
- { $setOnInsert: data },
36
- {
37
- upsert: true,
38
- new: true,
39
- runValidators: true,
40
- session: options.session,
41
- ...options.updatePipeline !== void 0 ? { updatePipeline: options.updatePipeline } : {}
42
- }
43
- );
44
- }
45
-
46
- export { create, createMany, create_exports, upsert };
@@ -1,8 +0,0 @@
1
- // src/utils/error.ts
2
- function createError(status, message) {
3
- const error = new Error(message);
4
- error.status = status;
5
- return error;
6
- }
7
-
8
- export { createError };
@@ -1,306 +0,0 @@
1
- import 'mongoose';
2
-
3
- // src/utils/memory-cache.ts
4
- function createMemoryCache(maxEntries = 1e3) {
5
- const cache = /* @__PURE__ */ new Map();
6
- let lastCleanup = Date.now();
7
- const CLEANUP_INTERVAL_MS = 6e4;
8
- function cleanupIfNeeded() {
9
- const now = Date.now();
10
- if (now - lastCleanup < CLEANUP_INTERVAL_MS) return;
11
- lastCleanup = now;
12
- for (const [key, entry] of cache) {
13
- if (entry.expiresAt < now) cache.delete(key);
14
- }
15
- }
16
- function evictOldest() {
17
- while (cache.size >= maxEntries) {
18
- const firstKey = cache.keys().next().value;
19
- if (firstKey) cache.delete(firstKey);
20
- else break;
21
- }
22
- }
23
- return {
24
- async get(key) {
25
- const entry = cache.get(key);
26
- if (!entry) return null;
27
- if (entry.expiresAt < Date.now()) {
28
- cache.delete(key);
29
- return null;
30
- }
31
- cache.delete(key);
32
- cache.set(key, entry);
33
- return entry.value;
34
- },
35
- async set(key, value, ttl) {
36
- cache.delete(key);
37
- if (cache.size >= maxEntries) {
38
- cleanupIfNeeded();
39
- evictOldest();
40
- }
41
- cache.set(key, {
42
- value,
43
- expiresAt: Date.now() + ttl * 1e3
44
- });
45
- },
46
- async del(key) {
47
- cache.delete(key);
48
- },
49
- async clear(pattern) {
50
- if (!pattern) {
51
- cache.clear();
52
- return;
53
- }
54
- const regex = new RegExp(
55
- "^" + pattern.replace(/\*/g, ".*").replace(/\?/g, ".") + "$"
56
- );
57
- for (const key of cache.keys()) {
58
- if (regex.test(key)) {
59
- cache.delete(key);
60
- }
61
- }
62
- }
63
- };
64
- }
65
- function buildCrudSchemasFromMongooseSchema(mongooseSchema, options = {}) {
66
- const jsonCreate = buildJsonSchemaFromPaths(mongooseSchema, options);
67
- const jsonUpdate = buildJsonSchemaForUpdate(jsonCreate, options);
68
- const jsonParams = {
69
- type: "object",
70
- properties: { id: { type: "string", pattern: "^[0-9a-fA-F]{24}$" } },
71
- required: ["id"]
72
- };
73
- const tree = mongooseSchema?.obj || {};
74
- const jsonQuery = buildJsonSchemaForQuery(tree, options);
75
- return { createBody: jsonCreate, updateBody: jsonUpdate, params: jsonParams, listQuery: jsonQuery };
76
- }
77
- function buildCrudSchemasFromModel(mongooseModel, options = {}) {
78
- if (!mongooseModel || !mongooseModel.schema) {
79
- throw new Error("Invalid mongoose model");
80
- }
81
- return buildCrudSchemasFromMongooseSchema(mongooseModel.schema, options);
82
- }
83
- function getImmutableFields(options = {}) {
84
- const immutable = [];
85
- const fieldRules = options?.fieldRules || {};
86
- Object.entries(fieldRules).forEach(([field, rules]) => {
87
- if (rules.immutable || rules.immutableAfterCreate) {
88
- immutable.push(field);
89
- }
90
- });
91
- (options?.update?.omitFields || []).forEach((f) => {
92
- if (!immutable.includes(f)) immutable.push(f);
93
- });
94
- return immutable;
95
- }
96
- function getSystemManagedFields(options = {}) {
97
- const systemManaged = [];
98
- const fieldRules = options?.fieldRules || {};
99
- Object.entries(fieldRules).forEach(([field, rules]) => {
100
- if (rules.systemManaged) {
101
- systemManaged.push(field);
102
- }
103
- });
104
- return systemManaged;
105
- }
106
- function isFieldUpdateAllowed(fieldName, options = {}) {
107
- const immutableFields = getImmutableFields(options);
108
- const systemManagedFields = getSystemManagedFields(options);
109
- return !immutableFields.includes(fieldName) && !systemManagedFields.includes(fieldName);
110
- }
111
- function validateUpdateBody(body = {}, options = {}) {
112
- const violations = [];
113
- const immutableFields = getImmutableFields(options);
114
- const systemManagedFields = getSystemManagedFields(options);
115
- Object.keys(body).forEach((field) => {
116
- if (immutableFields.includes(field)) {
117
- violations.push({ field, reason: "Field is immutable" });
118
- } else if (systemManagedFields.includes(field)) {
119
- violations.push({ field, reason: "Field is system-managed" });
120
- }
121
- });
122
- return {
123
- valid: violations.length === 0,
124
- violations
125
- };
126
- }
127
- function buildJsonSchemaFromPaths(mongooseSchema, options) {
128
- const properties = {};
129
- const required = [];
130
- const paths = mongooseSchema.paths;
131
- const rootFields = /* @__PURE__ */ new Map();
132
- for (const [path, schemaType] of Object.entries(paths)) {
133
- if (path === "_id" || path === "__v") continue;
134
- const parts = path.split(".");
135
- const rootField = parts[0];
136
- if (!rootFields.has(rootField)) {
137
- rootFields.set(rootField, []);
138
- }
139
- rootFields.get(rootField).push({ path, schemaType });
140
- }
141
- for (const [rootField, fieldPaths] of rootFields.entries()) {
142
- if (fieldPaths.length === 1 && fieldPaths[0].path === rootField) {
143
- const schemaType = fieldPaths[0].schemaType;
144
- properties[rootField] = schemaTypeToJsonSchema(schemaType);
145
- if (schemaType.isRequired) {
146
- required.push(rootField);
147
- }
148
- } else {
149
- const nestedSchema = buildNestedJsonSchema(fieldPaths, rootField);
150
- properties[rootField] = nestedSchema.schema;
151
- if (nestedSchema.required) {
152
- required.push(rootField);
153
- }
154
- }
155
- }
156
- const schema = { type: "object", properties };
157
- if (required.length) schema.required = required;
158
- const fieldsToOmit = /* @__PURE__ */ new Set(["createdAt", "updatedAt", "__v"]);
159
- (options?.create?.omitFields || []).forEach((f) => fieldsToOmit.add(f));
160
- const fieldRules = options?.fieldRules || {};
161
- Object.entries(fieldRules).forEach(([field, rules]) => {
162
- if (rules.systemManaged) {
163
- fieldsToOmit.add(field);
164
- }
165
- });
166
- fieldsToOmit.forEach((field) => {
167
- if (schema.properties?.[field]) {
168
- delete schema.properties[field];
169
- }
170
- if (schema.required) {
171
- schema.required = schema.required.filter((k) => k !== field);
172
- }
173
- });
174
- const reqOv = options?.create?.requiredOverrides || {};
175
- const optOv = options?.create?.optionalOverrides || {};
176
- schema.required = schema.required || [];
177
- for (const [k, v] of Object.entries(reqOv)) {
178
- if (v && !schema.required.includes(k)) schema.required.push(k);
179
- }
180
- for (const [k, v] of Object.entries(optOv)) {
181
- if (v && schema.required) schema.required = schema.required.filter((x) => x !== k);
182
- }
183
- Object.entries(fieldRules).forEach(([field, rules]) => {
184
- if (rules.optional && schema.required) {
185
- schema.required = schema.required.filter((x) => x !== field);
186
- }
187
- });
188
- const schemaOverrides = options?.create?.schemaOverrides || {};
189
- for (const [k, override] of Object.entries(schemaOverrides)) {
190
- if (schema.properties?.[k]) {
191
- schema.properties[k] = override;
192
- }
193
- }
194
- if (options?.strictAdditionalProperties === true) {
195
- schema.additionalProperties = false;
196
- }
197
- return schema;
198
- }
199
- function buildNestedJsonSchema(fieldPaths, rootField) {
200
- const properties = {};
201
- const required = [];
202
- let hasRequiredFields = false;
203
- for (const { path, schemaType } of fieldPaths) {
204
- const relativePath = path.substring(rootField.length + 1);
205
- const parts = relativePath.split(".");
206
- if (parts.length === 1) {
207
- properties[parts[0]] = schemaTypeToJsonSchema(schemaType);
208
- if (schemaType.isRequired) {
209
- required.push(parts[0]);
210
- hasRequiredFields = true;
211
- }
212
- } else {
213
- const fieldName = parts[0];
214
- if (!properties[fieldName]) {
215
- properties[fieldName] = { type: "object", properties: {} };
216
- }
217
- const nestedObj = properties[fieldName];
218
- if (!nestedObj.properties) nestedObj.properties = {};
219
- const deepPath = parts.slice(1).join(".");
220
- nestedObj.properties[deepPath] = schemaTypeToJsonSchema(schemaType);
221
- }
222
- }
223
- const schema = { type: "object", properties };
224
- if (required.length) schema.required = required;
225
- return { schema, required: hasRequiredFields };
226
- }
227
- function schemaTypeToJsonSchema(schemaType) {
228
- const result = {};
229
- const instance = schemaType.instance;
230
- const options = schemaType.options || {};
231
- if (instance === "String") {
232
- result.type = "string";
233
- if (typeof options.minlength === "number") result.minLength = options.minlength;
234
- if (typeof options.maxlength === "number") result.maxLength = options.maxlength;
235
- if (options.match instanceof RegExp) result.pattern = options.match.source;
236
- if (options.enum && Array.isArray(options.enum)) result.enum = options.enum;
237
- } else if (instance === "Number") {
238
- result.type = "number";
239
- if (typeof options.min === "number") result.minimum = options.min;
240
- if (typeof options.max === "number") result.maximum = options.max;
241
- } else if (instance === "Boolean") {
242
- result.type = "boolean";
243
- } else if (instance === "Date") {
244
- result.type = "string";
245
- result.format = "date-time";
246
- } else if (instance === "ObjectId" || instance === "ObjectID") {
247
- result.type = "string";
248
- result.pattern = "^[0-9a-fA-F]{24}$";
249
- } else if (instance === "Array") {
250
- result.type = "array";
251
- result.items = { type: "string" };
252
- } else {
253
- result.type = "object";
254
- result.additionalProperties = true;
255
- }
256
- return result;
257
- }
258
- function buildJsonSchemaForUpdate(createJson, options) {
259
- const clone = JSON.parse(JSON.stringify(createJson));
260
- delete clone.required;
261
- const fieldsToOmit = /* @__PURE__ */ new Set();
262
- (options?.update?.omitFields || []).forEach((f) => fieldsToOmit.add(f));
263
- const fieldRules = options?.fieldRules || {};
264
- Object.entries(fieldRules).forEach(([field, rules]) => {
265
- if (rules.immutable || rules.immutableAfterCreate) {
266
- fieldsToOmit.add(field);
267
- }
268
- });
269
- fieldsToOmit.forEach((field) => {
270
- if (clone.properties?.[field]) {
271
- delete clone.properties[field];
272
- }
273
- });
274
- if (options?.strictAdditionalProperties === true) {
275
- clone.additionalProperties = false;
276
- }
277
- if (options?.update?.requireAtLeastOne === true) {
278
- clone.minProperties = 1;
279
- }
280
- return clone;
281
- }
282
- function buildJsonSchemaForQuery(_tree, options) {
283
- const basePagination = {
284
- type: "object",
285
- properties: {
286
- page: { type: "string" },
287
- limit: { type: "string" },
288
- sort: { type: "string" },
289
- populate: { type: "string" },
290
- search: { type: "string" },
291
- select: { type: "string" },
292
- lean: { type: "string" },
293
- includeDeleted: { type: "string" }
294
- },
295
- additionalProperties: true
296
- };
297
- const filterable = options?.query?.filterableFields || {};
298
- for (const [k, v] of Object.entries(filterable)) {
299
- if (basePagination.properties) {
300
- basePagination.properties[k] = v && typeof v === "object" && "type" in v ? v : { type: "string" };
301
- }
302
- }
303
- return basePagination;
304
- }
305
-
306
- export { buildCrudSchemasFromModel, buildCrudSchemasFromMongooseSchema, createMemoryCache, getImmutableFields, getSystemManagedFields, isFieldUpdateAllowed, validateUpdateBody };
@@ -1,22 +0,0 @@
1
- // src/utils/logger.ts
2
- var noop = () => {
3
- };
4
- var current = {
5
- warn: console.warn.bind(console),
6
- debug: noop
7
- };
8
- function configureLogger(config) {
9
- if (config === false) {
10
- current = { warn: noop, debug: noop };
11
- } else {
12
- current = { ...current, ...config };
13
- }
14
- }
15
- function warn(message, ...args) {
16
- current.warn(message, ...args);
17
- }
18
- function debug(message, ...args) {
19
- current.debug(message, ...args);
20
- }
21
-
22
- export { configureLogger, debug, warn };