@classytic/mongokit 3.0.6 → 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, Z as CacheAdapter, y as SchemaBuilderOptions, z as CrudSchemas, V as ValidationResult, v as ParsedQuery } from './types-DDDYo18H.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-DDDYo18H.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, _ as CacheOptions, a2 as CascadeOptions } from '../types-DDDYo18H.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[];
@@ -289,16 +289,6 @@ interface FieldPreset {
289
289
  /** Additional fields for admins */
290
290
  admin?: string[];
291
291
  }
292
- /** Parsed query result */
293
- interface ParsedQuery {
294
- filters: FilterQuery<AnyDocument>;
295
- limit: number;
296
- sort: SortSpec | undefined;
297
- populate: string | undefined;
298
- search: string | undefined;
299
- page?: number;
300
- after?: string;
301
- }
302
292
  /** Field rules for schema building */
303
293
  interface FieldRules {
304
294
  [fieldName: string]: {
@@ -335,6 +325,8 @@ interface SchemaBuilderOptions {
335
325
  update?: {
336
326
  /** Fields to omit from update schema */
337
327
  omitFields?: string[];
328
+ /** Require at least one field to be provided (default: false) */
329
+ requireAtLeastOne?: boolean;
338
330
  };
339
331
  /** Query schema options */
340
332
  query?: {
@@ -354,6 +346,12 @@ interface JsonSchema {
354
346
  enum?: string[];
355
347
  format?: string;
356
348
  pattern?: string;
349
+ minProperties?: number;
350
+ maxProperties?: number;
351
+ minLength?: number;
352
+ maxLength?: number;
353
+ minimum?: number;
354
+ maximum?: number;
357
355
  }
358
356
  /** CRUD schemas result - framework-agnostic JSON schemas */
359
357
  interface CrudSchemas {
@@ -454,25 +452,7 @@ interface SoftDeleteRepository {
454
452
  session?: ClientSession;
455
453
  }): Promise<OffsetPaginationResult<unknown>>;
456
454
  }
457
- /** Lookup options for aggregate */
458
- interface LookupOptions {
459
- /** Collection to join */
460
- from: string;
461
- /** Local field to match */
462
- localField: string;
463
- /** Foreign field to match */
464
- foreignField: string;
465
- /** Output array field name */
466
- as: string;
467
- /** Additional pipeline stages */
468
- pipeline?: PipelineStage[];
469
- /** Initial match query */
470
- query?: FilterQuery<AnyDocument>;
471
- /** Operation options */
472
- options?: {
473
- session?: ClientSession;
474
- };
475
- }
455
+
476
456
  /** Group result */
477
457
  interface GroupResult {
478
458
  _id: unknown;
@@ -573,4 +553,4 @@ interface HttpError extends Error {
573
553
  }>;
574
554
  }
575
555
 
576
- export type { CacheOperationOptions 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, WithTransactionOptions as W, GroupResult as X, MinMaxResult as Y, CacheAdapter as Z, CacheOptions as _, OffsetPaginationResult as a, CacheStats as a0, CascadeRelation as a1, CascadeOptions as a2, 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-Do3SgsyJ.js';
2
- import { S as SelectSpec, e as PopulateSpec, f as SortSpec } from '../types-DDDYo18H.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
  /**