@classytic/mongokit 3.0.5 → 3.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.
@@ -1,4 +1,4 @@
1
- import { q as UserContext, F as FieldPreset, H as HttpError, Y as CacheAdapter, y as SchemaBuilderOptions, z as CrudSchemas, V as ValidationResult, v as ParsedQuery } from './types-CHIDluaP.js';
1
+ import { U as UserContext, F as FieldPreset, H as HttpError, C as CacheAdapter, g as SchemaBuilderOptions, h as CrudSchemas, V as ValidationResult } from './types-CrSoCuWu.js';
2
2
  import mongoose__default, { Schema } from 'mongoose';
3
3
 
4
4
  /**
@@ -154,6 +154,8 @@ declare function createMemoryCache(maxEntries?: number): CacheAdapter;
154
154
  * Additional Options:
155
155
  * - strictAdditionalProperties: Set to true to add "additionalProperties: false" to schemas
156
156
  * This makes Fastify reject unknown fields at validation level (default: false for backward compatibility)
157
+ * - update.requireAtLeastOne: Set to true to add "minProperties: 1" to update schema
158
+ * This prevents empty update payloads (default: false)
157
159
  *
158
160
  * @example
159
161
  * buildCrudSchemasFromModel(Model, {
@@ -163,7 +165,10 @@ declare function createMemoryCache(maxEntries?: number): CacheAdapter;
163
165
  * status: { systemManaged: true },
164
166
  * },
165
167
  * create: { omitFields: ['verifiedAt'] },
166
- * update: { omitFields: ['customerId'] }
168
+ * update: {
169
+ * omitFields: ['customerId'],
170
+ * requireAtLeastOne: true // Reject empty updates
171
+ * }
167
172
  * })
168
173
  */
169
174
 
@@ -192,112 +197,4 @@ declare function isFieldUpdateAllowed(fieldName: string, options?: SchemaBuilder
192
197
  */
193
198
  declare function validateUpdateBody(body?: Record<string, unknown>, options?: SchemaBuilderOptions): ValidationResult;
194
199
 
195
- /**
196
- * Query Parser
197
- *
198
- * Parses HTTP query parameters into MongoDB-compatible query objects.
199
- * Supports operators, pagination, sorting, and filtering.
200
- */
201
-
202
- /** Operator mapping from query syntax to MongoDB operators */
203
- type OperatorMap = Record<string, string>;
204
- /** Possible values in filter parameters */
205
- type FilterValue = string | number | boolean | null | undefined | Record<string, unknown> | unknown[];
206
- /** Configuration options for QueryParser */
207
- interface QueryParserOptions {
208
- /** Maximum allowed regex pattern length (default: 500) */
209
- maxRegexLength?: number;
210
- /** Maximum allowed text search query length (default: 200) */
211
- maxSearchLength?: number;
212
- /** Maximum allowed filter depth (default: 10) */
213
- maxFilterDepth?: number;
214
- /** Additional operators to block */
215
- additionalDangerousOperators?: string[];
216
- }
217
- /**
218
- * Query Parser Class
219
- *
220
- * Parses HTTP query parameters into MongoDB-compatible query objects.
221
- * Includes security measures against NoSQL injection and ReDoS attacks.
222
- *
223
- * @example
224
- * ```typescript
225
- * import { QueryParser } from '@classytic/mongokit';
226
- *
227
- * const parser = new QueryParser({ maxRegexLength: 100 });
228
- * const query = parser.parseQuery(req.query);
229
- * ```
230
- */
231
- declare class QueryParser {
232
- private readonly options;
233
- private readonly operators;
234
- /**
235
- * Dangerous MongoDB operators that should never be accepted from user input
236
- * Security: Prevent NoSQL injection attacks
237
- */
238
- private readonly dangerousOperators;
239
- /**
240
- * Regex pattern characters that can cause catastrophic backtracking (ReDoS)
241
- */
242
- private readonly dangerousRegexPatterns;
243
- constructor(options?: QueryParserOptions);
244
- /**
245
- * Parse query parameters into MongoDB query format
246
- */
247
- parseQuery(query: Record<string, unknown> | null | undefined): ParsedQuery;
248
- /**
249
- * Parse sort parameter
250
- * Converts string like '-createdAt' to { createdAt: -1 }
251
- * Handles multiple sorts: '-createdAt,name' → { createdAt: -1, name: 1 }
252
- */
253
- private _parseSort;
254
- /**
255
- * Parse standard filter parameter (filter[field]=value)
256
- */
257
- private _parseFilters;
258
- /**
259
- * Handle operator syntax: field[operator]=value
260
- */
261
- private _handleOperatorSyntax;
262
- /**
263
- * Handle bracket syntax with object value
264
- */
265
- private _handleBracketSyntax;
266
- /**
267
- * Convert operator to MongoDB format
268
- */
269
- private _toMongoOperator;
270
- /**
271
- * Create a safe regex pattern with protection against ReDoS attacks
272
- * @param pattern - The pattern string from user input
273
- * @param flags - Regex flags (default: 'i' for case-insensitive)
274
- * @returns A safe RegExp or null if pattern is invalid/dangerous
275
- */
276
- private _createSafeRegex;
277
- /**
278
- * Escape special regex characters for literal matching
279
- */
280
- private _escapeRegex;
281
- /**
282
- * Sanitize text search query for MongoDB $text search
283
- * @param search - Raw search input from user
284
- * @returns Sanitized search string or undefined
285
- */
286
- private _sanitizeSearch;
287
- /**
288
- * Convert values based on operator type
289
- */
290
- private _convertValue;
291
- /**
292
- * Parse $or conditions
293
- */
294
- private _parseOr;
295
- /**
296
- * Enhance filters with between operator
297
- */
298
- private _enhanceWithBetween;
299
- }
300
- /** Default query parser instance with standard options */
301
- declare const defaultQueryParser: QueryParser;
302
-
303
- export { type FilterValue as F, type OperatorMap as O, QueryParser as Q, getMongooseProjection as a, type QueryParserOptions as b, createFieldPreset as c, defaultQueryParser as d, buildCrudSchemasFromMongooseSchema as e, filterResponseData as f, getFieldsForUser as g, buildCrudSchemasFromModel as h, getImmutableFields as i, getSystemManagedFields as j, isFieldUpdateAllowed as k, createError as l, createMemoryCache as m, validateUpdateBody as v };
200
+ export { getMongooseProjection as a, buildCrudSchemasFromMongooseSchema as b, createFieldPreset as c, buildCrudSchemasFromModel as d, getImmutableFields as e, filterResponseData as f, getFieldsForUser as g, getSystemManagedFields as h, isFieldUpdateAllowed as i, createError as j, createMemoryCache as k, validateUpdateBody as v };
@@ -1,5 +1,5 @@
1
1
  import { Model } from 'mongoose';
2
- import { A as AnyDocument, P as PaginationConfig, O as OffsetPaginationOptions, a as OffsetPaginationResult, K as KeysetPaginationOptions, b as KeysetPaginationResult, c as AggregatePaginationOptions, d as AggregatePaginationResult } from '../types-CHIDluaP.js';
2
+ import { A as AnyDocument, P as PaginationConfig, O as OffsetPaginationOptions, a as OffsetPaginationResult, K as KeysetPaginationOptions, b as KeysetPaginationResult, c as AggregatePaginationOptions, d as AggregatePaginationResult } from '../types-CrSoCuWu.js';
3
3
 
4
4
  /**
5
5
  * Pagination Engine
@@ -1,4 +1,4 @@
1
- import { F as FieldPreset, r as Plugin, L as Logger, M as SoftDeleteOptions, t as RepositoryInstance, G as ValidatorDefinition, I as ValidationChainOptions, i as RepositoryContext, Z as CacheOptions, a1 as CascadeOptions } from '../types-CHIDluaP.js';
1
+ import { F as FieldPreset, u as Plugin, L as Logger, I as SoftDeleteOptions, w as RepositoryInstance, B as ValidatorDefinition, G as ValidationChainOptions, m as RepositoryContext, X as CacheOptions, $ as CascadeOptions } from '../types-CrSoCuWu.js';
2
2
  import 'mongoose';
3
3
 
4
4
  /**
@@ -16,7 +16,7 @@ type AnyDocument = Document & Record<string, unknown>;
16
16
  type AnyModel = Model<AnyDocument>;
17
17
  /** Sort direction */
18
18
  type SortDirection = 1 | -1;
19
- /** Sort specification */
19
+ /** Sort specification for MongoDB queries */
20
20
  type SortSpec = Record<string, SortDirection>;
21
21
  /** Populate specification */
22
22
  type PopulateSpec = string | string[] | PopulateOptions | PopulateOptions[];
@@ -158,6 +158,13 @@ interface OperationOptions {
158
158
  /** Additional query filters (e.g., for soft delete) */
159
159
  query?: Record<string, unknown>;
160
160
  }
161
+ /** withTransaction options */
162
+ interface WithTransactionOptions {
163
+ /** Allow non-transactional fallback when transactions are unsupported */
164
+ allowFallback?: boolean;
165
+ /** Optional hook to observe fallback triggers */
166
+ onFallback?: (error: Error) => void;
167
+ }
161
168
  /** Create operation options */
162
169
  interface CreateOptions {
163
170
  /** MongoDB session for transactions */
@@ -282,16 +289,6 @@ interface FieldPreset {
282
289
  /** Additional fields for admins */
283
290
  admin?: string[];
284
291
  }
285
- /** Parsed query result */
286
- interface ParsedQuery {
287
- filters: FilterQuery<AnyDocument>;
288
- limit: number;
289
- sort: SortSpec | undefined;
290
- populate: string | undefined;
291
- search: string | undefined;
292
- page?: number;
293
- after?: string;
294
- }
295
292
  /** Field rules for schema building */
296
293
  interface FieldRules {
297
294
  [fieldName: string]: {
@@ -328,6 +325,8 @@ interface SchemaBuilderOptions {
328
325
  update?: {
329
326
  /** Fields to omit from update schema */
330
327
  omitFields?: string[];
328
+ /** Require at least one field to be provided (default: false) */
329
+ requireAtLeastOne?: boolean;
331
330
  };
332
331
  /** Query schema options */
333
332
  query?: {
@@ -347,6 +346,12 @@ interface JsonSchema {
347
346
  enum?: string[];
348
347
  format?: string;
349
348
  pattern?: string;
349
+ minProperties?: number;
350
+ maxProperties?: number;
351
+ minLength?: number;
352
+ maxLength?: number;
353
+ minimum?: number;
354
+ maximum?: number;
350
355
  }
351
356
  /** CRUD schemas result - framework-agnostic JSON schemas */
352
357
  interface CrudSchemas {
@@ -447,25 +452,7 @@ interface SoftDeleteRepository {
447
452
  session?: ClientSession;
448
453
  }): Promise<OffsetPaginationResult<unknown>>;
449
454
  }
450
- /** Lookup options for aggregate */
451
- interface LookupOptions {
452
- /** Collection to join */
453
- from: string;
454
- /** Local field to match */
455
- localField: string;
456
- /** Foreign field to match */
457
- foreignField: string;
458
- /** Output array field name */
459
- as: string;
460
- /** Additional pipeline stages */
461
- pipeline?: PipelineStage[];
462
- /** Initial match query */
463
- query?: FilterQuery<AnyDocument>;
464
- /** Operation options */
465
- options?: {
466
- session?: ClientSession;
467
- };
468
- }
455
+
469
456
  /** Group result */
470
457
  interface GroupResult {
471
458
  _id: unknown;
@@ -566,4 +553,4 @@ interface HttpError extends Error {
566
553
  }>;
567
554
  }
568
555
 
569
- export type { CacheStats as $, AnyDocument as A, DecodedCursor as B, CreateOptions as C, DeleteResult as D, EventPayload as E, FieldPreset as F, ValidatorDefinition as G, HttpError as H, ValidationChainOptions as I, JsonSchema as J, KeysetPaginationOptions as K, Logger as L, SoftDeleteOptions as M, SoftDeleteFilterMode as N, OffsetPaginationOptions as O, PaginationConfig as P, SoftDeleteRepository as Q, RepositoryOptions as R, SelectSpec as S, LookupOptions as T, UpdateOptions as U, ValidationResult as V, GroupResult as W, MinMaxResult as X, CacheAdapter as Y, CacheOptions as Z, CacheOperationOptions as _, OffsetPaginationResult as a, CascadeRelation as a0, CascadeOptions as a1, KeysetPaginationResult as b, AggregatePaginationOptions as c, AggregatePaginationResult as d, PopulateSpec as e, SortSpec as f, PluginType as g, ObjectId as h, RepositoryContext as i, AnyModel as j, SortDirection as k, HookMode as l, PaginationResult as m, OperationOptions as n, UpdateManyResult as o, UpdateWithValidationResult as p, UserContext as q, Plugin as r, PluginFunction as s, RepositoryInstance as t, RepositoryEvent as u, ParsedQuery as v, FilterQuery as w, FieldRules as x, SchemaBuilderOptions as y, CrudSchemas as z };
556
+ export type { CascadeOptions as $, AnyDocument as A, ValidatorDefinition as B, CacheAdapter as C, DeleteResult as D, EventPayload as E, FieldPreset as F, ValidationChainOptions as G, HttpError as H, SoftDeleteOptions as I, JsonSchema as J, KeysetPaginationOptions as K, Logger as L, SoftDeleteFilterMode as M, SoftDeleteRepository as N, OffsetPaginationOptions as O, PaginationConfig as P, GroupResult as Q, RepositoryOptions as R, SelectSpec as S, MinMaxResult as T, UserContext as U, ValidationResult as V, WithTransactionOptions as W, CacheOptions as X, CacheOperationOptions as Y, CacheStats as Z, CascadeRelation as _, OffsetPaginationResult as a, KeysetPaginationResult as b, AggregatePaginationOptions as c, AggregatePaginationResult as d, PopulateSpec as e, SortSpec as f, SchemaBuilderOptions as g, CrudSchemas as h, PaginationResult as i, PluginType as j, ObjectId as k, UpdateOptions as l, RepositoryContext as m, AnyModel as n, SortDirection as o, HookMode as p, OperationOptions as q, CreateOptions as r, UpdateManyResult as s, UpdateWithValidationResult as t, Plugin as u, PluginFunction as v, RepositoryInstance as w, RepositoryEvent as x, FieldRules as y, DecodedCursor as z };
@@ -1,5 +1,5 @@
1
- export { F as FilterValue, O as OperatorMap, Q as QueryParser, b as QueryParserOptions, h as buildCrudSchemasFromModel, e as buildCrudSchemasFromMongooseSchema, l as createError, c as createFieldPreset, m as createMemoryCache, f as filterResponseData, g as getFieldsForUser, i as getImmutableFields, a as getMongooseProjection, j as getSystemManagedFields, k as isFieldUpdateAllowed, d as queryParser, v as validateUpdateBody } from '../queryParser-CxzCjzXd.js';
2
- import { S as SelectSpec, e as PopulateSpec, f as SortSpec } from '../types-CHIDluaP.js';
1
+ export { d as buildCrudSchemasFromModel, b as buildCrudSchemasFromMongooseSchema, j as createError, c as createFieldPreset, k as createMemoryCache, f as filterResponseData, g as getFieldsForUser, e as getImmutableFields, a as getMongooseProjection, h as getSystemManagedFields, i as isFieldUpdateAllowed, v as validateUpdateBody } from '../mongooseToJsonSchema-CUQma8QK.js';
2
+ import { S as SelectSpec, e as PopulateSpec, f as SortSpec } from '../types-CrSoCuWu.js';
3
3
  import 'mongoose';
4
4
 
5
5
  /**