@classytic/mongokit 3.0.6 → 3.1.1

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.
@@ -16,14 +16,70 @@ 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[];
23
23
  /** Select specification */
24
24
  type SelectSpec = string | string[] | Record<string, 0 | 1>;
25
25
  /** Filter query type for MongoDB queries (compatible with Mongoose 8 & 9) */
26
- type FilterQuery<T> = Record<string, unknown>;
26
+ type FilterQuery<_T = unknown> = Record<string, unknown>;
27
+ /**
28
+ * Infer document type from a Mongoose Model
29
+ * @example
30
+ * type UserDoc = InferDocument<typeof UserModel>;
31
+ */
32
+ type InferDocument<TModel> = TModel extends Model<infer TDoc> ? TDoc : never;
33
+ /**
34
+ * Infer raw document shape (without Mongoose Document methods)
35
+ * @example
36
+ * type User = InferRawDoc<typeof UserModel>;
37
+ */
38
+ type InferRawDoc<TModel> = TModel extends Model<infer TDoc> ? TDoc extends Document ? Omit<TDoc, keyof Document> : TDoc : never;
39
+ /**
40
+ * Make specific fields optional
41
+ * @example
42
+ * type CreateUser = PartialBy<User, 'createdAt' | 'updatedAt'>;
43
+ */
44
+ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
45
+ /**
46
+ * Make specific fields required
47
+ * @example
48
+ * type UserWithId = RequiredBy<User, '_id'>;
49
+ */
50
+ type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
51
+ /**
52
+ * Extract keys of type T that have values of type V
53
+ * @example
54
+ * type StringFields = KeysOfType<User, string>; // 'name' | 'email'
55
+ */
56
+ type KeysOfType<T, V> = {
57
+ [K in keyof T]: T[K] extends V ? K : never;
58
+ }[keyof T];
59
+ /**
60
+ * Deep partial - makes all nested properties optional
61
+ */
62
+ type DeepPartial<T> = T extends object ? {
63
+ [P in keyof T]?: DeepPartial<T[P]>;
64
+ } : T;
65
+ /**
66
+ * Strict object type - prevents excess properties
67
+ * Use with `satisfies` for compile-time validation
68
+ */
69
+ type Strict<T> = T & {
70
+ [K in Exclude<string, keyof T>]?: never;
71
+ };
72
+ /**
73
+ * NonNullable fields extractor
74
+ */
75
+ type NonNullableFields<T> = {
76
+ [K in keyof T]: NonNullable<T[K]>;
77
+ };
78
+ /**
79
+ * Create/Update input types from document
80
+ */
81
+ type CreateInput<TDoc> = Omit<TDoc, '_id' | 'createdAt' | 'updatedAt' | '__v'>;
82
+ type UpdateInput<TDoc> = Partial<Omit<TDoc, '_id' | 'createdAt' | '__v'>>;
27
83
  /** Hook execution mode */
28
84
  type HookMode = 'sync' | 'async';
29
85
  /** Repository options */
@@ -242,8 +298,30 @@ interface RepositoryContext {
242
298
  lean?: boolean;
243
299
  /** MongoDB session */
244
300
  session?: ClientSession;
245
- /** Include soft-deleted documents */
301
+ /** Pagination filters */
302
+ filters?: Record<string, unknown>;
303
+ /** Sort specification */
304
+ sort?: SortSpec;
305
+ /** Page number (offset pagination) */
306
+ page?: number;
307
+ /** Items per page */
308
+ limit?: number;
309
+ /** Cursor for next page (keyset pagination) */
310
+ after?: string;
311
+ /** Whether this is a soft delete operation (set by softDeletePlugin) */
312
+ softDeleted?: boolean;
313
+ /** Include soft-deleted documents in queries */
246
314
  includeDeleted?: boolean;
315
+ /** Skip cache for this operation */
316
+ skipCache?: boolean;
317
+ /** Custom TTL for this operation (seconds) */
318
+ cacheTtl?: number;
319
+ /** Whether result was served from cache (internal) */
320
+ _cacheHit?: boolean;
321
+ /** Cached result (internal) */
322
+ _cachedResult?: unknown;
323
+ /** IDs to cascade delete (internal) */
324
+ _cascadeIds?: unknown[];
247
325
  /** Custom context data from plugins */
248
326
  [key: string]: unknown;
249
327
  }
@@ -272,8 +350,24 @@ interface RepositoryInstance {
272
350
  hasMethod?(name: string): boolean;
273
351
  [key: string]: unknown;
274
352
  }
275
- /** Repository event names */
276
- type RepositoryEvent = 'before:create' | 'after:create' | 'error:create' | 'before:createMany' | 'after:createMany' | 'error:createMany' | 'before:update' | 'after:update' | 'error:update' | 'before:updateMany' | 'after:updateMany' | 'error:updateMany' | 'before:delete' | 'after:delete' | 'error:delete' | 'before:deleteMany' | 'after:deleteMany' | 'error:deleteMany' | 'before:getById' | 'after:getById' | 'before:getByQuery' | 'after:getByQuery' | 'before:getAll' | 'after:getAll' | 'before:aggregatePaginate' | 'method:registered' | 'error:hook';
353
+ /** Repository operation names */
354
+ type RepositoryOperation = 'create' | 'createMany' | 'update' | 'updateMany' | 'delete' | 'deleteMany' | 'getById' | 'getByQuery' | 'getAll' | 'aggregatePaginate' | 'lookupPopulate';
355
+ /** Event lifecycle phases */
356
+ type EventPhase = 'before' | 'after' | 'error';
357
+ /** Repository event names (generated from template literals) */
358
+ type RepositoryEvent = `${EventPhase}:${RepositoryOperation}` | 'method:registered' | 'error:hook';
359
+ /** Type-safe event handler map */
360
+ type EventHandlers<TDoc = unknown> = {
361
+ [K in RepositoryEvent]?: K extends `after:${string}` ? (payload: {
362
+ context: RepositoryContext;
363
+ result: TDoc | TDoc[];
364
+ }) => void | Promise<void> : K extends `error:${string}` ? (payload: {
365
+ context: RepositoryContext;
366
+ error: Error;
367
+ }) => void | Promise<void> : (payload: {
368
+ context: RepositoryContext;
369
+ }) => void | Promise<void>;
370
+ };
277
371
  /** Event payload */
278
372
  interface EventPayload {
279
373
  context: RepositoryContext;
@@ -289,16 +383,6 @@ interface FieldPreset {
289
383
  /** Additional fields for admins */
290
384
  admin?: string[];
291
385
  }
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
386
  /** Field rules for schema building */
303
387
  interface FieldRules {
304
388
  [fieldName: string]: {
@@ -335,6 +419,8 @@ interface SchemaBuilderOptions {
335
419
  update?: {
336
420
  /** Fields to omit from update schema */
337
421
  omitFields?: string[];
422
+ /** Require at least one field to be provided (default: false) */
423
+ requireAtLeastOne?: boolean;
338
424
  };
339
425
  /** Query schema options */
340
426
  query?: {
@@ -354,6 +440,12 @@ interface JsonSchema {
354
440
  enum?: string[];
355
441
  format?: string;
356
442
  pattern?: string;
443
+ minProperties?: number;
444
+ maxProperties?: number;
445
+ minLength?: number;
446
+ maxLength?: number;
447
+ minimum?: number;
448
+ maximum?: number;
357
449
  }
358
450
  /** CRUD schemas result - framework-agnostic JSON schemas */
359
451
  interface CrudSchemas {
@@ -454,25 +546,7 @@ interface SoftDeleteRepository {
454
546
  session?: ClientSession;
455
547
  }): Promise<OffsetPaginationResult<unknown>>;
456
548
  }
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
- }
549
+
476
550
  /** Group result */
477
551
  interface GroupResult {
478
552
  _id: unknown;
@@ -573,4 +647,4 @@ interface HttpError extends Error {
573
647
  }>;
574
648
  }
575
649
 
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 };
650
+ export type { DecodedCursor as $, AnyDocument as A, UpdateManyResult as B, CacheAdapter as C, DeepPartial as D, UpdateWithValidationResult as E, FieldPreset as F, Plugin as G, HttpError as H, InferDocument as I, PluginFunction as J, KeysetPaginationOptions as K, RepositoryInstance as L, RepositoryOperation as M, NonNullableFields as N, OffsetPaginationOptions as O, PaginationConfig as P, EventPhase as Q, RepositoryOptions as R, SelectSpec as S, RepositoryEvent as T, UserContext as U, ValidationResult as V, WithTransactionOptions as W, EventHandlers as X, EventPayload as Y, FieldRules as Z, JsonSchema as _, OffsetPaginationResult as a, ValidatorDefinition as a0, ValidationChainOptions as a1, Logger as a2, SoftDeleteOptions as a3, SoftDeleteFilterMode as a4, SoftDeleteRepository as a5, GroupResult as a6, MinMaxResult as a7, CacheOptions as a8, CacheOperationOptions as a9, CacheStats as aa, CascadeRelation as ab, CascadeOptions as ac, 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, InferRawDoc as q, PartialBy as r, RequiredBy as s, KeysOfType as t, Strict as u, CreateInput as v, UpdateInput as w, OperationOptions as x, CreateOptions as y, DeleteResult 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-BKMxPbPp.js';
2
+ import { S as SelectSpec, e as PopulateSpec, f as SortSpec } from '../types-DA0rs2Jh.js';
3
3
  import 'mongoose';
4
4
 
5
5
  /**