@classytic/mongokit 3.1.0 → 3.1.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.
- package/README.md +713 -465
- package/dist/actions/index.d.ts +2 -2
- package/dist/actions/index.js +3 -515
- package/dist/chunks/chunk-2ZN65ZOP.js +93 -0
- package/dist/chunks/chunk-CF6FLC2G.js +46 -0
- package/dist/chunks/chunk-CSLJ2PL2.js +1092 -0
- package/dist/chunks/chunk-IT7DCOKR.js +299 -0
- package/dist/chunks/chunk-M2XHQGZB.js +361 -0
- package/dist/chunks/chunk-SAKSLT47.js +470 -0
- package/dist/chunks/chunk-VJXDGP3C.js +14 -0
- package/dist/{index-3Nkm_Brq.d.ts → index-BXSSv1pW.d.ts} +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +49 -2387
- package/dist/{mongooseToJsonSchema-CUQma8QK.d.ts → mongooseToJsonSchema-Cc5AwuDu.d.ts} +1 -1
- package/dist/pagination/PaginationEngine.d.ts +1 -1
- package/dist/pagination/PaginationEngine.js +2 -368
- package/dist/plugins/index.d.ts +462 -5
- package/dist/plugins/index.js +4 -1170
- package/dist/{types-CrSoCuWu.d.ts → types-B5Uv6Ak7.d.ts} +222 -5
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +3 -398
- package/package.json +8 -3
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { F as FieldPreset,
|
|
2
|
-
import 'mongoose';
|
|
1
|
+
import { F as FieldPreset, G as Plugin, a2 as Logger, a3 as SoftDeleteOptions, k as ObjectId, f as SortSpec, S as SelectSpec, e as PopulateSpec, a as OffsetPaginationResult, L as RepositoryInstance, a0 as ValidatorDefinition, a1 as ValidationChainOptions, m as RepositoryContext, a8 as CacheOptions, aa as CacheStats, ac as CascadeOptions } from '../types-B5Uv6Ak7.js';
|
|
2
|
+
import { ClientSession } from 'mongoose';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Field Filter Plugin
|
|
@@ -92,6 +92,53 @@ declare function auditLogPlugin(logger: Logger): Plugin;
|
|
|
92
92
|
* ```
|
|
93
93
|
*/
|
|
94
94
|
declare function softDeletePlugin(options?: SoftDeleteOptions): Plugin;
|
|
95
|
+
/**
|
|
96
|
+
* TypeScript interface for soft delete plugin methods
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* import type { SoftDeleteMethods } from '@classytic/mongokit';
|
|
101
|
+
*
|
|
102
|
+
* type UserRepoWithSoftDelete = UserRepo & SoftDeleteMethods<IUser>;
|
|
103
|
+
*
|
|
104
|
+
* const userRepo = new UserRepo(UserModel, [
|
|
105
|
+
* methodRegistryPlugin(),
|
|
106
|
+
* softDeletePlugin({ deletedField: 'deletedAt' }),
|
|
107
|
+
* ]) as UserRepoWithSoftDelete;
|
|
108
|
+
*
|
|
109
|
+
* // TypeScript autocomplete for soft delete methods
|
|
110
|
+
* await userRepo.restore(userId);
|
|
111
|
+
* const deleted = await userRepo.getDeleted({ page: 1, limit: 20 });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
interface SoftDeleteMethods<TDoc> {
|
|
115
|
+
/**
|
|
116
|
+
* Restore a soft-deleted document
|
|
117
|
+
* @param id - Document ID to restore
|
|
118
|
+
* @param options - Optional restore options
|
|
119
|
+
* @returns Restored document
|
|
120
|
+
*/
|
|
121
|
+
restore(id: string | ObjectId, options?: {
|
|
122
|
+
session?: ClientSession;
|
|
123
|
+
}): Promise<TDoc>;
|
|
124
|
+
/**
|
|
125
|
+
* Get paginated list of soft-deleted documents
|
|
126
|
+
* @param params - Query parameters (filters, sort, pagination)
|
|
127
|
+
* @param options - Query options (select, populate, lean, session)
|
|
128
|
+
* @returns Paginated result of deleted documents
|
|
129
|
+
*/
|
|
130
|
+
getDeleted(params?: {
|
|
131
|
+
filters?: Record<string, unknown>;
|
|
132
|
+
sort?: SortSpec | string;
|
|
133
|
+
page?: number;
|
|
134
|
+
limit?: number;
|
|
135
|
+
}, options?: {
|
|
136
|
+
select?: SelectSpec;
|
|
137
|
+
populate?: PopulateSpec;
|
|
138
|
+
lean?: boolean;
|
|
139
|
+
session?: ClientSession;
|
|
140
|
+
}): Promise<OffsetPaginationResult<TDoc>>;
|
|
141
|
+
}
|
|
95
142
|
|
|
96
143
|
/**
|
|
97
144
|
* Method Registry Plugin
|
|
@@ -178,16 +225,189 @@ declare function uniqueField(field: string, errorMessage?: string): ValidatorDef
|
|
|
178
225
|
/**
|
|
179
226
|
* MongoDB operations plugin
|
|
180
227
|
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
228
|
+
* Adds MongoDB-specific atomic operations to repositories:
|
|
229
|
+
* - upsert: Create or update document
|
|
230
|
+
* - increment/decrement: Atomic numeric operations
|
|
231
|
+
* - pushToArray/pullFromArray/addToSet: Array operations
|
|
232
|
+
* - setField/unsetField/renameField: Field operations
|
|
233
|
+
* - multiplyField: Multiply numeric field
|
|
234
|
+
* - setMin/setMax: Conditional min/max updates
|
|
235
|
+
*
|
|
236
|
+
* @example Basic usage (no TypeScript autocomplete)
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const repo = new Repository(ProductModel, [
|
|
183
239
|
* methodRegistryPlugin(),
|
|
184
240
|
* mongoOperationsPlugin(),
|
|
185
241
|
* ]);
|
|
186
242
|
*
|
|
243
|
+
* // Works at runtime but TypeScript doesn't know about these methods
|
|
244
|
+
* await (repo as any).increment(productId, 'views', 1);
|
|
245
|
+
* await (repo as any).pushToArray(productId, 'tags', 'featured');
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @example With TypeScript type safety (recommended)
|
|
249
|
+
* ```typescript
|
|
250
|
+
* import { Repository, mongoOperationsPlugin, methodRegistryPlugin } from '@classytic/mongokit';
|
|
251
|
+
* import type { MongoOperationsMethods } from '@classytic/mongokit';
|
|
252
|
+
*
|
|
253
|
+
* class ProductRepo extends Repository<IProduct> {
|
|
254
|
+
* // Add your custom methods here
|
|
255
|
+
* }
|
|
256
|
+
*
|
|
257
|
+
* // Create with type assertion to get autocomplete for plugin methods
|
|
258
|
+
* type ProductRepoWithPlugins = ProductRepo & MongoOperationsMethods<IProduct>;
|
|
259
|
+
*
|
|
260
|
+
* const repo = new ProductRepo(ProductModel, [
|
|
261
|
+
* methodRegistryPlugin(),
|
|
262
|
+
* mongoOperationsPlugin(),
|
|
263
|
+
* ]) as ProductRepoWithPlugins;
|
|
264
|
+
*
|
|
265
|
+
* // Now TypeScript provides autocomplete and type checking!
|
|
187
266
|
* await repo.increment(productId, 'views', 1);
|
|
267
|
+
* await repo.upsert({ sku: 'ABC' }, { name: 'Product', price: 99 });
|
|
188
268
|
* await repo.pushToArray(productId, 'tags', 'featured');
|
|
269
|
+
* ```
|
|
189
270
|
*/
|
|
190
271
|
declare function mongoOperationsPlugin(): Plugin;
|
|
272
|
+
/**
|
|
273
|
+
* Type interface for repositories using mongoOperationsPlugin
|
|
274
|
+
*
|
|
275
|
+
* Use this interface to get TypeScript autocomplete and type safety
|
|
276
|
+
* for the methods added by mongoOperationsPlugin.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* import { Repository, mongoOperationsPlugin, methodRegistryPlugin } from '@classytic/mongokit';
|
|
281
|
+
* import type { MongoOperationsMethods } from '@classytic/mongokit';
|
|
282
|
+
*
|
|
283
|
+
* // Without type safety (base is flexible)
|
|
284
|
+
* class ProductRepo extends Repository<IProduct> {
|
|
285
|
+
* // Can add anything - fully flexible
|
|
286
|
+
* }
|
|
287
|
+
*
|
|
288
|
+
* // With type safety for plugin methods
|
|
289
|
+
* class ProductRepo extends Repository<IProduct> implements MongoOperationsMethods<IProduct> {
|
|
290
|
+
* // TypeScript knows about upsert, increment, decrement, etc.
|
|
291
|
+
* }
|
|
292
|
+
*
|
|
293
|
+
* const repo = new ProductRepo(ProductModel, [
|
|
294
|
+
* methodRegistryPlugin(),
|
|
295
|
+
* mongoOperationsPlugin(),
|
|
296
|
+
* ]);
|
|
297
|
+
*
|
|
298
|
+
* // Now TypeScript provides autocomplete and type checking
|
|
299
|
+
* await repo.increment(productId, 'views', 1);
|
|
300
|
+
* await repo.upsert({ sku: 'ABC' }, { name: 'Product' });
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
interface MongoOperationsMethods<TDoc> {
|
|
304
|
+
/**
|
|
305
|
+
* Update existing document or insert new one
|
|
306
|
+
* @param query - Query to find document
|
|
307
|
+
* @param data - Data to update or insert
|
|
308
|
+
* @param options - Operation options (session, etc.)
|
|
309
|
+
* @returns Created or updated document
|
|
310
|
+
*/
|
|
311
|
+
upsert(query: Record<string, unknown>, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<TDoc>;
|
|
312
|
+
/**
|
|
313
|
+
* Atomically increment numeric field
|
|
314
|
+
* @param id - Document ID
|
|
315
|
+
* @param field - Field name to increment
|
|
316
|
+
* @param value - Value to increment by (default: 1)
|
|
317
|
+
* @param options - Operation options (session, etc.)
|
|
318
|
+
* @returns Updated document
|
|
319
|
+
*/
|
|
320
|
+
increment(id: string | ObjectId, field: string, value?: number, options?: Record<string, unknown>): Promise<TDoc>;
|
|
321
|
+
/**
|
|
322
|
+
* Atomically decrement numeric field
|
|
323
|
+
* @param id - Document ID
|
|
324
|
+
* @param field - Field name to decrement
|
|
325
|
+
* @param value - Value to decrement by (default: 1)
|
|
326
|
+
* @param options - Operation options (session, etc.)
|
|
327
|
+
* @returns Updated document
|
|
328
|
+
*/
|
|
329
|
+
decrement(id: string | ObjectId, field: string, value?: number, options?: Record<string, unknown>): Promise<TDoc>;
|
|
330
|
+
/**
|
|
331
|
+
* Push value to array field
|
|
332
|
+
* @param id - Document ID
|
|
333
|
+
* @param field - Array field name
|
|
334
|
+
* @param value - Value to push
|
|
335
|
+
* @param options - Operation options (session, etc.)
|
|
336
|
+
* @returns Updated document
|
|
337
|
+
*/
|
|
338
|
+
pushToArray(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
339
|
+
/**
|
|
340
|
+
* Remove value from array field
|
|
341
|
+
* @param id - Document ID
|
|
342
|
+
* @param field - Array field name
|
|
343
|
+
* @param value - Value to remove
|
|
344
|
+
* @param options - Operation options (session, etc.)
|
|
345
|
+
* @returns Updated document
|
|
346
|
+
*/
|
|
347
|
+
pullFromArray(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
348
|
+
/**
|
|
349
|
+
* Add value to array only if not already present (unique)
|
|
350
|
+
* @param id - Document ID
|
|
351
|
+
* @param field - Array field name
|
|
352
|
+
* @param value - Value to add
|
|
353
|
+
* @param options - Operation options (session, etc.)
|
|
354
|
+
* @returns Updated document
|
|
355
|
+
*/
|
|
356
|
+
addToSet(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
357
|
+
/**
|
|
358
|
+
* Set field value (alias for update with $set)
|
|
359
|
+
* @param id - Document ID
|
|
360
|
+
* @param field - Field name
|
|
361
|
+
* @param value - Value to set
|
|
362
|
+
* @param options - Operation options (session, etc.)
|
|
363
|
+
* @returns Updated document
|
|
364
|
+
*/
|
|
365
|
+
setField(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
366
|
+
/**
|
|
367
|
+
* Unset (remove) field from document
|
|
368
|
+
* @param id - Document ID
|
|
369
|
+
* @param fields - Field name or array of field names to remove
|
|
370
|
+
* @param options - Operation options (session, etc.)
|
|
371
|
+
* @returns Updated document
|
|
372
|
+
*/
|
|
373
|
+
unsetField(id: string | ObjectId, fields: string | string[], options?: Record<string, unknown>): Promise<TDoc>;
|
|
374
|
+
/**
|
|
375
|
+
* Rename field in document
|
|
376
|
+
* @param id - Document ID
|
|
377
|
+
* @param oldName - Current field name
|
|
378
|
+
* @param newName - New field name
|
|
379
|
+
* @param options - Operation options (session, etc.)
|
|
380
|
+
* @returns Updated document
|
|
381
|
+
*/
|
|
382
|
+
renameField(id: string | ObjectId, oldName: string, newName: string, options?: Record<string, unknown>): Promise<TDoc>;
|
|
383
|
+
/**
|
|
384
|
+
* Multiply numeric field by value
|
|
385
|
+
* @param id - Document ID
|
|
386
|
+
* @param field - Field name
|
|
387
|
+
* @param multiplier - Multiplier value
|
|
388
|
+
* @param options - Operation options (session, etc.)
|
|
389
|
+
* @returns Updated document
|
|
390
|
+
*/
|
|
391
|
+
multiplyField(id: string | ObjectId, field: string, multiplier: number, options?: Record<string, unknown>): Promise<TDoc>;
|
|
392
|
+
/**
|
|
393
|
+
* Set field to minimum value (only if current value is greater)
|
|
394
|
+
* @param id - Document ID
|
|
395
|
+
* @param field - Field name
|
|
396
|
+
* @param value - Minimum value
|
|
397
|
+
* @param options - Operation options (session, etc.)
|
|
398
|
+
* @returns Updated document
|
|
399
|
+
*/
|
|
400
|
+
setMin(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
401
|
+
/**
|
|
402
|
+
* Set field to maximum value (only if current value is less)
|
|
403
|
+
* @param id - Document ID
|
|
404
|
+
* @param field - Field name
|
|
405
|
+
* @param value - Maximum value
|
|
406
|
+
* @param options - Operation options (session, etc.)
|
|
407
|
+
* @returns Updated document
|
|
408
|
+
*/
|
|
409
|
+
setMax(id: string | ObjectId, field: string, value: unknown, options?: Record<string, unknown>): Promise<TDoc>;
|
|
410
|
+
}
|
|
191
411
|
|
|
192
412
|
/**
|
|
193
413
|
* Batch Operations Plugin
|
|
@@ -207,6 +427,57 @@ declare function mongoOperationsPlugin(): Plugin;
|
|
|
207
427
|
* await repo.deleteMany({ status: 'deleted' });
|
|
208
428
|
*/
|
|
209
429
|
declare function batchOperationsPlugin(): Plugin;
|
|
430
|
+
/**
|
|
431
|
+
* Type interface for repositories using batchOperationsPlugin
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* import { Repository, methodRegistryPlugin, batchOperationsPlugin } from '@classytic/mongokit';
|
|
436
|
+
* import type { BatchOperationsMethods } from '@classytic/mongokit';
|
|
437
|
+
*
|
|
438
|
+
* class ProductRepo extends Repository<IProduct> {}
|
|
439
|
+
*
|
|
440
|
+
* type ProductRepoWithBatch = ProductRepo & BatchOperationsMethods;
|
|
441
|
+
*
|
|
442
|
+
* const repo = new ProductRepo(ProductModel, [
|
|
443
|
+
* methodRegistryPlugin(),
|
|
444
|
+
* batchOperationsPlugin(),
|
|
445
|
+
* ]) as ProductRepoWithBatch;
|
|
446
|
+
*
|
|
447
|
+
* // TypeScript autocomplete works!
|
|
448
|
+
* await repo.updateMany({ status: 'pending' }, { status: 'active' });
|
|
449
|
+
* await repo.deleteMany({ status: 'archived' });
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
interface BatchOperationsMethods {
|
|
453
|
+
/**
|
|
454
|
+
* Update multiple documents matching the query
|
|
455
|
+
* @param query - Query to match documents
|
|
456
|
+
* @param data - Update data
|
|
457
|
+
* @param options - Operation options
|
|
458
|
+
* @returns Update result with matchedCount, modifiedCount, etc.
|
|
459
|
+
*/
|
|
460
|
+
updateMany(query: Record<string, unknown>, data: Record<string, unknown>, options?: {
|
|
461
|
+
session?: ClientSession;
|
|
462
|
+
updatePipeline?: boolean;
|
|
463
|
+
}): Promise<{
|
|
464
|
+
acknowledged: boolean;
|
|
465
|
+
matchedCount: number;
|
|
466
|
+
modifiedCount: number;
|
|
467
|
+
upsertedCount: number;
|
|
468
|
+
upsertedId: unknown;
|
|
469
|
+
}>;
|
|
470
|
+
/**
|
|
471
|
+
* Delete multiple documents matching the query
|
|
472
|
+
* @param query - Query to match documents
|
|
473
|
+
* @param options - Operation options
|
|
474
|
+
* @returns Delete result with deletedCount
|
|
475
|
+
*/
|
|
476
|
+
deleteMany(query: Record<string, unknown>, options?: Record<string, unknown>): Promise<{
|
|
477
|
+
acknowledged: boolean;
|
|
478
|
+
deletedCount: number;
|
|
479
|
+
}>;
|
|
480
|
+
}
|
|
210
481
|
|
|
211
482
|
/**
|
|
212
483
|
* Aggregate Helpers Plugin
|
|
@@ -226,6 +497,76 @@ declare function batchOperationsPlugin(): Plugin;
|
|
|
226
497
|
* const total = await repo.sum('amount', { status: 'completed' });
|
|
227
498
|
*/
|
|
228
499
|
declare function aggregateHelpersPlugin(): Plugin;
|
|
500
|
+
/**
|
|
501
|
+
* Type interface for repositories using aggregateHelpersPlugin
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```typescript
|
|
505
|
+
* import { Repository, methodRegistryPlugin, aggregateHelpersPlugin } from '@classytic/mongokit';
|
|
506
|
+
* import type { AggregateHelpersMethods } from '@classytic/mongokit';
|
|
507
|
+
*
|
|
508
|
+
* class OrderRepo extends Repository<IOrder> {}
|
|
509
|
+
*
|
|
510
|
+
* type OrderRepoWithAggregates = OrderRepo & AggregateHelpersMethods;
|
|
511
|
+
*
|
|
512
|
+
* const repo = new OrderRepo(OrderModel, [
|
|
513
|
+
* methodRegistryPlugin(),
|
|
514
|
+
* aggregateHelpersPlugin(),
|
|
515
|
+
* ]) as OrderRepoWithAggregates;
|
|
516
|
+
*
|
|
517
|
+
* // TypeScript autocomplete works!
|
|
518
|
+
* const groups = await repo.groupBy('status');
|
|
519
|
+
* const total = await repo.sum('amount', { status: 'completed' });
|
|
520
|
+
* const avg = await repo.average('amount');
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
interface AggregateHelpersMethods {
|
|
524
|
+
/**
|
|
525
|
+
* Group documents by field value and count occurrences
|
|
526
|
+
* @param field - Field to group by
|
|
527
|
+
* @param options - Operation options
|
|
528
|
+
* @returns Array of groups with _id and count
|
|
529
|
+
*/
|
|
530
|
+
groupBy(field: string, options?: {
|
|
531
|
+
limit?: number;
|
|
532
|
+
session?: unknown;
|
|
533
|
+
}): Promise<Array<{
|
|
534
|
+
_id: unknown;
|
|
535
|
+
count: number;
|
|
536
|
+
}>>;
|
|
537
|
+
/**
|
|
538
|
+
* Calculate sum of field values
|
|
539
|
+
* @param field - Field to sum
|
|
540
|
+
* @param query - Filter query
|
|
541
|
+
* @param options - Operation options
|
|
542
|
+
* @returns Sum of field values
|
|
543
|
+
*/
|
|
544
|
+
sum(field: string, query?: Record<string, unknown>, options?: Record<string, unknown>): Promise<number>;
|
|
545
|
+
/**
|
|
546
|
+
* Calculate average of field values
|
|
547
|
+
* @param field - Field to average
|
|
548
|
+
* @param query - Filter query
|
|
549
|
+
* @param options - Operation options
|
|
550
|
+
* @returns Average of field values
|
|
551
|
+
*/
|
|
552
|
+
average(field: string, query?: Record<string, unknown>, options?: Record<string, unknown>): Promise<number>;
|
|
553
|
+
/**
|
|
554
|
+
* Get minimum field value
|
|
555
|
+
* @param field - Field to get minimum from
|
|
556
|
+
* @param query - Filter query
|
|
557
|
+
* @param options - Operation options
|
|
558
|
+
* @returns Minimum field value
|
|
559
|
+
*/
|
|
560
|
+
min(field: string, query?: Record<string, unknown>, options?: Record<string, unknown>): Promise<number>;
|
|
561
|
+
/**
|
|
562
|
+
* Get maximum field value
|
|
563
|
+
* @param field - Field to get maximum from
|
|
564
|
+
* @param query - Filter query
|
|
565
|
+
* @param options - Operation options
|
|
566
|
+
* @returns Maximum field value
|
|
567
|
+
*/
|
|
568
|
+
max(field: string, query?: Record<string, unknown>, options?: Record<string, unknown>): Promise<number>;
|
|
569
|
+
}
|
|
229
570
|
|
|
230
571
|
/**
|
|
231
572
|
* Subdocument Plugin
|
|
@@ -245,6 +586,73 @@ declare function aggregateHelpersPlugin(): Plugin;
|
|
|
245
586
|
* await repo.updateSubdocument(parentId, 'items', itemId, { name: 'Updated Item' });
|
|
246
587
|
*/
|
|
247
588
|
declare function subdocumentPlugin(): Plugin;
|
|
589
|
+
/**
|
|
590
|
+
* Type interface for repositories using subdocumentPlugin
|
|
591
|
+
*
|
|
592
|
+
* @example
|
|
593
|
+
* ```typescript
|
|
594
|
+
* import { Repository, methodRegistryPlugin, subdocumentPlugin } from '@classytic/mongokit';
|
|
595
|
+
* import type { SubdocumentMethods } from '@classytic/mongokit';
|
|
596
|
+
*
|
|
597
|
+
* class OrderRepo extends Repository<IOrder> {}
|
|
598
|
+
*
|
|
599
|
+
* type OrderRepoWithSubdocs = OrderRepo & SubdocumentMethods<IOrder>;
|
|
600
|
+
*
|
|
601
|
+
* const repo = new OrderRepo(OrderModel, [
|
|
602
|
+
* methodRegistryPlugin(),
|
|
603
|
+
* subdocumentPlugin(),
|
|
604
|
+
* ]) as OrderRepoWithSubdocs;
|
|
605
|
+
*
|
|
606
|
+
* // TypeScript autocomplete works!
|
|
607
|
+
* await repo.addSubdocument(orderId, 'items', { productId: '123', quantity: 2 });
|
|
608
|
+
* await repo.updateSubdocument(orderId, 'items', itemId, { quantity: 5 });
|
|
609
|
+
* await repo.deleteSubdocument(orderId, 'items', itemId);
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
interface SubdocumentMethods<TDoc> {
|
|
613
|
+
/**
|
|
614
|
+
* Add subdocument to array field
|
|
615
|
+
* @param parentId - Parent document ID
|
|
616
|
+
* @param arrayPath - Path to array field (e.g., 'items', 'addresses')
|
|
617
|
+
* @param subData - Subdocument data
|
|
618
|
+
* @param options - Operation options
|
|
619
|
+
* @returns Updated parent document
|
|
620
|
+
*/
|
|
621
|
+
addSubdocument(parentId: string | ObjectId, arrayPath: string, subData: Record<string, unknown>, options?: Record<string, unknown>): Promise<TDoc>;
|
|
622
|
+
/**
|
|
623
|
+
* Get subdocument from array field
|
|
624
|
+
* @param parentId - Parent document ID
|
|
625
|
+
* @param arrayPath - Path to array field
|
|
626
|
+
* @param subId - Subdocument ID
|
|
627
|
+
* @param options - Operation options
|
|
628
|
+
* @returns Subdocument
|
|
629
|
+
*/
|
|
630
|
+
getSubdocument(parentId: string | ObjectId, arrayPath: string, subId: string | ObjectId, options?: {
|
|
631
|
+
lean?: boolean;
|
|
632
|
+
session?: unknown;
|
|
633
|
+
}): Promise<Record<string, unknown>>;
|
|
634
|
+
/**
|
|
635
|
+
* Update subdocument in array field
|
|
636
|
+
* @param parentId - Parent document ID
|
|
637
|
+
* @param arrayPath - Path to array field
|
|
638
|
+
* @param subId - Subdocument ID
|
|
639
|
+
* @param updateData - Update data
|
|
640
|
+
* @param options - Operation options
|
|
641
|
+
* @returns Updated parent document
|
|
642
|
+
*/
|
|
643
|
+
updateSubdocument(parentId: string | ObjectId, arrayPath: string, subId: string | ObjectId, updateData: Record<string, unknown>, options?: {
|
|
644
|
+
session?: unknown;
|
|
645
|
+
}): Promise<TDoc>;
|
|
646
|
+
/**
|
|
647
|
+
* Delete subdocument from array field
|
|
648
|
+
* @param parentId - Parent document ID
|
|
649
|
+
* @param arrayPath - Path to array field
|
|
650
|
+
* @param subId - Subdocument ID
|
|
651
|
+
* @param options - Operation options
|
|
652
|
+
* @returns Updated parent document
|
|
653
|
+
*/
|
|
654
|
+
deleteSubdocument(parentId: string | ObjectId, arrayPath: string, subId: string | ObjectId, options?: Record<string, unknown>): Promise<TDoc>;
|
|
655
|
+
}
|
|
248
656
|
|
|
249
657
|
/**
|
|
250
658
|
* Cache Plugin
|
|
@@ -303,6 +711,55 @@ declare function subdocumentPlugin(): Plugin;
|
|
|
303
711
|
* @returns Plugin instance
|
|
304
712
|
*/
|
|
305
713
|
declare function cachePlugin(options: CacheOptions): Plugin;
|
|
714
|
+
/**
|
|
715
|
+
* TypeScript interface for cache plugin methods
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* import type { CacheMethods } from '@classytic/mongokit';
|
|
720
|
+
*
|
|
721
|
+
* type ProductRepoWithCache = ProductRepo & CacheMethods;
|
|
722
|
+
*
|
|
723
|
+
* const productRepo = new ProductRepo(ProductModel, [
|
|
724
|
+
* methodRegistryPlugin(),
|
|
725
|
+
* cachePlugin({ adapter: redisAdapter, ttl: 60 }),
|
|
726
|
+
* ]) as ProductRepoWithCache;
|
|
727
|
+
*
|
|
728
|
+
* // TypeScript autocomplete for cache methods
|
|
729
|
+
* await productRepo.invalidateCache(productId);
|
|
730
|
+
* await productRepo.invalidateListCache();
|
|
731
|
+
* await productRepo.invalidateAllCache();
|
|
732
|
+
* const stats = productRepo.getCacheStats();
|
|
733
|
+
* productRepo.resetCacheStats();
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
interface CacheMethods {
|
|
737
|
+
/**
|
|
738
|
+
* Invalidate cache for a specific document
|
|
739
|
+
* Use when document was updated outside this service
|
|
740
|
+
* @param id - Document ID to invalidate
|
|
741
|
+
*/
|
|
742
|
+
invalidateCache(id: string): Promise<void>;
|
|
743
|
+
/**
|
|
744
|
+
* Invalidate all list caches for this model
|
|
745
|
+
* Use when bulk changes happened outside this service
|
|
746
|
+
*/
|
|
747
|
+
invalidateListCache(): Promise<void>;
|
|
748
|
+
/**
|
|
749
|
+
* Invalidate ALL cache entries for this model
|
|
750
|
+
* Nuclear option - use sparingly
|
|
751
|
+
*/
|
|
752
|
+
invalidateAllCache(): Promise<void>;
|
|
753
|
+
/**
|
|
754
|
+
* Get cache statistics for monitoring
|
|
755
|
+
* @returns Cache statistics (hits, misses, sets, invalidations)
|
|
756
|
+
*/
|
|
757
|
+
getCacheStats(): CacheStats;
|
|
758
|
+
/**
|
|
759
|
+
* Reset cache statistics
|
|
760
|
+
*/
|
|
761
|
+
resetCacheStats(): void;
|
|
762
|
+
}
|
|
306
763
|
|
|
307
764
|
/**
|
|
308
765
|
* Cascade Delete Plugin
|
|
@@ -339,4 +796,4 @@ declare function cachePlugin(options: CacheOptions): Plugin;
|
|
|
339
796
|
*/
|
|
340
797
|
declare function cascadePlugin(options: CascadeOptions): Plugin;
|
|
341
798
|
|
|
342
|
-
export { type MethodRegistryRepository, aggregateHelpersPlugin, auditLogPlugin, autoInject, batchOperationsPlugin, blockIf, cachePlugin, cascadePlugin, fieldFilterPlugin, immutableField, methodRegistryPlugin, mongoOperationsPlugin, requireField, softDeletePlugin, subdocumentPlugin, timestampPlugin, uniqueField, validationChainPlugin };
|
|
799
|
+
export { type AggregateHelpersMethods, type BatchOperationsMethods, type CacheMethods, type MethodRegistryRepository, type MongoOperationsMethods, type SoftDeleteMethods, type SubdocumentMethods, aggregateHelpersPlugin, auditLogPlugin, autoInject, batchOperationsPlugin, blockIf, cachePlugin, cascadePlugin, fieldFilterPlugin, immutableField, methodRegistryPlugin, mongoOperationsPlugin, requireField, softDeletePlugin, subdocumentPlugin, timestampPlugin, uniqueField, validationChainPlugin };
|