@classytic/mongokit 2.0.0 → 2.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.
Files changed (123) hide show
  1. package/README.md +221 -7
  2. package/dist/actions/index.cjs +479 -0
  3. package/dist/actions/index.cjs.map +1 -0
  4. package/dist/actions/index.d.cts +3 -0
  5. package/dist/actions/index.d.ts +3 -0
  6. package/dist/actions/index.js +473 -0
  7. package/dist/actions/index.js.map +1 -0
  8. package/dist/index-BfVJZF-3.d.cts +337 -0
  9. package/dist/index-CgOJ2pqz.d.ts +337 -0
  10. package/dist/index.cjs +2142 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +239 -0
  13. package/dist/index.d.ts +239 -0
  14. package/dist/index.js +2108 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/memory-cache-DG2oSSbx.d.ts +142 -0
  17. package/dist/memory-cache-DqfFfKes.d.cts +142 -0
  18. package/dist/pagination/PaginationEngine.cjs +375 -0
  19. package/dist/pagination/PaginationEngine.cjs.map +1 -0
  20. package/dist/pagination/PaginationEngine.d.cts +117 -0
  21. package/dist/pagination/PaginationEngine.d.ts +117 -0
  22. package/dist/pagination/PaginationEngine.js +369 -0
  23. package/dist/pagination/PaginationEngine.js.map +1 -0
  24. package/dist/plugins/index.cjs +874 -0
  25. package/dist/plugins/index.cjs.map +1 -0
  26. package/dist/plugins/index.d.cts +275 -0
  27. package/dist/plugins/index.d.ts +275 -0
  28. package/dist/plugins/index.js +857 -0
  29. package/dist/plugins/index.js.map +1 -0
  30. package/dist/types-Nxhmi1aI.d.cts +510 -0
  31. package/dist/types-Nxhmi1aI.d.ts +510 -0
  32. package/dist/utils/index.cjs +667 -0
  33. package/dist/utils/index.cjs.map +1 -0
  34. package/dist/utils/index.d.cts +189 -0
  35. package/dist/utils/index.d.ts +189 -0
  36. package/dist/utils/index.js +643 -0
  37. package/dist/utils/index.js.map +1 -0
  38. package/package.json +44 -21
  39. package/src/Repository.js +0 -296
  40. package/src/actions/aggregate.js +0 -266
  41. package/src/actions/create.js +0 -59
  42. package/src/actions/delete.js +0 -88
  43. package/src/actions/index.js +0 -11
  44. package/src/actions/read.js +0 -188
  45. package/src/actions/update.js +0 -176
  46. package/src/hooks/lifecycle.js +0 -146
  47. package/src/index.js +0 -71
  48. package/src/pagination/PaginationEngine.js +0 -348
  49. package/src/pagination/utils/cursor.js +0 -119
  50. package/src/pagination/utils/filter.js +0 -42
  51. package/src/pagination/utils/limits.js +0 -82
  52. package/src/pagination/utils/sort.js +0 -101
  53. package/src/plugins/aggregate-helpers.plugin.js +0 -71
  54. package/src/plugins/audit-log.plugin.js +0 -60
  55. package/src/plugins/batch-operations.plugin.js +0 -66
  56. package/src/plugins/field-filter.plugin.js +0 -27
  57. package/src/plugins/index.js +0 -19
  58. package/src/plugins/method-registry.plugin.js +0 -140
  59. package/src/plugins/mongo-operations.plugin.js +0 -317
  60. package/src/plugins/soft-delete.plugin.js +0 -46
  61. package/src/plugins/subdocument.plugin.js +0 -66
  62. package/src/plugins/timestamp.plugin.js +0 -19
  63. package/src/plugins/validation-chain.plugin.js +0 -145
  64. package/src/types.d.ts +0 -87
  65. package/src/utils/error.js +0 -12
  66. package/src/utils/field-selection.js +0 -156
  67. package/src/utils/index.js +0 -12
  68. package/types/Repository.d.ts +0 -95
  69. package/types/Repository.d.ts.map +0 -1
  70. package/types/actions/aggregate.d.ts +0 -112
  71. package/types/actions/aggregate.d.ts.map +0 -1
  72. package/types/actions/create.d.ts +0 -21
  73. package/types/actions/create.d.ts.map +0 -1
  74. package/types/actions/delete.d.ts +0 -37
  75. package/types/actions/delete.d.ts.map +0 -1
  76. package/types/actions/index.d.ts +0 -6
  77. package/types/actions/index.d.ts.map +0 -1
  78. package/types/actions/read.d.ts +0 -135
  79. package/types/actions/read.d.ts.map +0 -1
  80. package/types/actions/update.d.ts +0 -58
  81. package/types/actions/update.d.ts.map +0 -1
  82. package/types/hooks/lifecycle.d.ts +0 -44
  83. package/types/hooks/lifecycle.d.ts.map +0 -1
  84. package/types/index.d.ts +0 -25
  85. package/types/index.d.ts.map +0 -1
  86. package/types/pagination/PaginationEngine.d.ts +0 -386
  87. package/types/pagination/PaginationEngine.d.ts.map +0 -1
  88. package/types/pagination/utils/cursor.d.ts +0 -40
  89. package/types/pagination/utils/cursor.d.ts.map +0 -1
  90. package/types/pagination/utils/filter.d.ts +0 -28
  91. package/types/pagination/utils/filter.d.ts.map +0 -1
  92. package/types/pagination/utils/limits.d.ts +0 -64
  93. package/types/pagination/utils/limits.d.ts.map +0 -1
  94. package/types/pagination/utils/sort.d.ts +0 -41
  95. package/types/pagination/utils/sort.d.ts.map +0 -1
  96. package/types/plugins/aggregate-helpers.plugin.d.ts +0 -6
  97. package/types/plugins/aggregate-helpers.plugin.d.ts.map +0 -1
  98. package/types/plugins/audit-log.plugin.d.ts +0 -6
  99. package/types/plugins/audit-log.plugin.d.ts.map +0 -1
  100. package/types/plugins/batch-operations.plugin.d.ts +0 -6
  101. package/types/plugins/batch-operations.plugin.d.ts.map +0 -1
  102. package/types/plugins/field-filter.plugin.d.ts +0 -6
  103. package/types/plugins/field-filter.plugin.d.ts.map +0 -1
  104. package/types/plugins/index.d.ts +0 -11
  105. package/types/plugins/index.d.ts.map +0 -1
  106. package/types/plugins/method-registry.plugin.d.ts +0 -3
  107. package/types/plugins/method-registry.plugin.d.ts.map +0 -1
  108. package/types/plugins/mongo-operations.plugin.d.ts +0 -4
  109. package/types/plugins/mongo-operations.plugin.d.ts.map +0 -1
  110. package/types/plugins/soft-delete.plugin.d.ts +0 -6
  111. package/types/plugins/soft-delete.plugin.d.ts.map +0 -1
  112. package/types/plugins/subdocument.plugin.d.ts +0 -6
  113. package/types/plugins/subdocument.plugin.d.ts.map +0 -1
  114. package/types/plugins/timestamp.plugin.d.ts +0 -6
  115. package/types/plugins/timestamp.plugin.d.ts.map +0 -1
  116. package/types/plugins/validation-chain.plugin.d.ts +0 -31
  117. package/types/plugins/validation-chain.plugin.d.ts.map +0 -1
  118. package/types/utils/error.d.ts +0 -11
  119. package/types/utils/error.d.ts.map +0 -1
  120. package/types/utils/field-selection.d.ts +0 -9
  121. package/types/utils/field-selection.d.ts.map +0 -1
  122. package/types/utils/index.d.ts +0 -2
  123. package/types/utils/index.d.ts.map +0 -1
@@ -0,0 +1,275 @@
1
+ import { F as FieldPreset, p as Plugin, L as Logger, B as SoftDeleteOptions, r as RepositoryInstance, y as ValidatorDefinition, z as ValidationChainOptions, R as RepositoryContext, Q as CacheOptions } from '../types-Nxhmi1aI.js';
2
+ import 'mongoose';
3
+
4
+ /**
5
+ * Field Filter Plugin
6
+ * Automatically filters response fields based on user roles
7
+ */
8
+
9
+ /**
10
+ * Field filter plugin that restricts fields based on user context
11
+ *
12
+ * @example
13
+ * const fieldPreset = {
14
+ * public: ['id', 'name'],
15
+ * authenticated: ['email'],
16
+ * admin: ['createdAt', 'internalNotes']
17
+ * };
18
+ *
19
+ * const repo = new Repository(Model, [fieldFilterPlugin(fieldPreset)]);
20
+ */
21
+ declare function fieldFilterPlugin(fieldPreset: FieldPreset): Plugin;
22
+
23
+ /**
24
+ * Timestamp Plugin
25
+ * Auto-injects createdAt/updatedAt timestamps on create/update
26
+ */
27
+
28
+ /**
29
+ * Timestamp plugin that auto-injects timestamps
30
+ *
31
+ * @example
32
+ * const repo = new Repository(Model, [timestampPlugin()]);
33
+ */
34
+ declare function timestampPlugin(): Plugin;
35
+
36
+ /**
37
+ * Audit Log Plugin
38
+ * Logs repository operations for auditing purposes
39
+ */
40
+
41
+ /**
42
+ * Audit log plugin that logs all repository operations
43
+ *
44
+ * @example
45
+ * const repo = new Repository(Model, [auditLogPlugin(console)]);
46
+ */
47
+ declare function auditLogPlugin(logger: Logger): Plugin;
48
+
49
+ /**
50
+ * Soft Delete Plugin
51
+ * Implements soft delete pattern - marks documents as deleted instead of removing
52
+ */
53
+
54
+ /**
55
+ * Soft delete plugin
56
+ *
57
+ * @example
58
+ * const repo = new Repository(Model, [
59
+ * softDeletePlugin({ deletedField: 'deletedAt' })
60
+ * ]);
61
+ */
62
+ declare function softDeletePlugin(options?: SoftDeleteOptions): Plugin;
63
+
64
+ /**
65
+ * Method Registry Plugin
66
+ *
67
+ * Enables plugins to dynamically add methods to repository instances.
68
+ * Foundation for extensibility - allows other plugins to extend repositories
69
+ * with custom methods while maintaining type safety and proper binding.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const repo = new Repository(User, [methodRegistryPlugin()]);
74
+ *
75
+ * // Now you can register custom methods
76
+ * repo.registerMethod('findActive', async function() {
77
+ * return this.getAll({ filters: { status: 'active' } });
78
+ * });
79
+ * ```
80
+ */
81
+
82
+ /**
83
+ * Extended repository interface with method registry
84
+ */
85
+ interface MethodRegistryRepository extends RepositoryInstance {
86
+ registerMethod(name: string, fn: Function): void;
87
+ hasMethod(name: string): boolean;
88
+ getRegisteredMethods(): string[];
89
+ }
90
+ /**
91
+ * Method registry plugin that enables dynamic method registration
92
+ */
93
+ declare function methodRegistryPlugin(): Plugin;
94
+
95
+ /**
96
+ * Validation Chain Plugin
97
+ *
98
+ * Composable validation for repository operations with customizable rules.
99
+ */
100
+
101
+ type OperationType = 'create' | 'createMany' | 'update' | 'delete';
102
+ /**
103
+ * Validation chain plugin
104
+ *
105
+ * @example
106
+ * const repo = new Repository(Model, [
107
+ * validationChainPlugin([
108
+ * requireField('email'),
109
+ * uniqueField('email', 'Email already exists'),
110
+ * blockIf('no-delete-admin', ['delete'], ctx => ctx.data?.role === 'admin', 'Cannot delete admin'),
111
+ * ])
112
+ * ]);
113
+ */
114
+ declare function validationChainPlugin(validators?: ValidatorDefinition[], options?: ValidationChainOptions): Plugin;
115
+ /**
116
+ * Block operation if condition is true
117
+ *
118
+ * @example
119
+ * blockIf('block-library', ['delete'], ctx => ctx.data?.managed, 'Cannot delete managed records')
120
+ */
121
+ declare function blockIf(name: string, operations: OperationType[], condition: (context: RepositoryContext) => boolean, errorMessage: string): ValidatorDefinition;
122
+ /**
123
+ * Require a field to be present
124
+ */
125
+ declare function requireField(field: string, operations?: OperationType[]): ValidatorDefinition;
126
+ /**
127
+ * Auto-inject a value if not present
128
+ */
129
+ declare function autoInject(field: string, getter: (context: RepositoryContext) => unknown, operations?: OperationType[]): ValidatorDefinition;
130
+ /**
131
+ * Make a field immutable (cannot be updated)
132
+ */
133
+ declare function immutableField(field: string): ValidatorDefinition;
134
+ /**
135
+ * Ensure field value is unique
136
+ */
137
+ declare function uniqueField(field: string, errorMessage?: string): ValidatorDefinition;
138
+
139
+ /**
140
+ * MongoDB Operations Plugin
141
+ *
142
+ * Adds MongoDB-specific operations to repositories.
143
+ * Requires method-registry.plugin.js to be loaded first.
144
+ */
145
+
146
+ /**
147
+ * MongoDB operations plugin
148
+ *
149
+ * @example
150
+ * const repo = new Repository(Model, [
151
+ * methodRegistryPlugin(),
152
+ * mongoOperationsPlugin(),
153
+ * ]);
154
+ *
155
+ * await repo.increment(productId, 'views', 1);
156
+ * await repo.pushToArray(productId, 'tags', 'featured');
157
+ */
158
+ declare function mongoOperationsPlugin(): Plugin;
159
+
160
+ /**
161
+ * Batch Operations Plugin
162
+ * Adds bulk update/delete operations with proper event emission
163
+ */
164
+
165
+ /**
166
+ * Batch operations plugin
167
+ *
168
+ * @example
169
+ * const repo = new Repository(Model, [
170
+ * methodRegistryPlugin(),
171
+ * batchOperationsPlugin(),
172
+ * ]);
173
+ *
174
+ * await repo.updateMany({ status: 'pending' }, { status: 'active' });
175
+ * await repo.deleteMany({ status: 'deleted' });
176
+ */
177
+ declare function batchOperationsPlugin(): Plugin;
178
+
179
+ /**
180
+ * Aggregate Helpers Plugin
181
+ * Adds common aggregation helper methods
182
+ */
183
+
184
+ /**
185
+ * Aggregate helpers plugin
186
+ *
187
+ * @example
188
+ * const repo = new Repository(Model, [
189
+ * methodRegistryPlugin(),
190
+ * aggregateHelpersPlugin(),
191
+ * ]);
192
+ *
193
+ * const groups = await repo.groupBy('category');
194
+ * const total = await repo.sum('amount', { status: 'completed' });
195
+ */
196
+ declare function aggregateHelpersPlugin(): Plugin;
197
+
198
+ /**
199
+ * Subdocument Plugin
200
+ * Adds subdocument array operations
201
+ */
202
+
203
+ /**
204
+ * Subdocument plugin for managing nested arrays
205
+ *
206
+ * @example
207
+ * const repo = new Repository(Model, [
208
+ * methodRegistryPlugin(),
209
+ * subdocumentPlugin(),
210
+ * ]);
211
+ *
212
+ * await repo.addSubdocument(parentId, 'items', { name: 'Item 1' });
213
+ * await repo.updateSubdocument(parentId, 'items', itemId, { name: 'Updated Item' });
214
+ */
215
+ declare function subdocumentPlugin(): Plugin;
216
+
217
+ /**
218
+ * Cache Plugin
219
+ *
220
+ * Optional caching layer for MongoKit with automatic invalidation.
221
+ * Bring-your-own cache adapter (Redis, Memcached, in-memory, etc.)
222
+ *
223
+ * Features:
224
+ * - Cache-aside (read-through) pattern with configurable TTLs
225
+ * - Automatic invalidation on create/update/delete
226
+ * - Collection version tags for efficient list cache invalidation
227
+ * - Manual invalidation methods for microservice scenarios
228
+ * - Skip cache per-operation with `skipCache: true`
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * import { Repository, cachePlugin } from '@classytic/mongokit';
233
+ * import Redis from 'ioredis';
234
+ *
235
+ * const redis = new Redis();
236
+ *
237
+ * const userRepo = new Repository(UserModel, [
238
+ * cachePlugin({
239
+ * adapter: {
240
+ * async get(key) { return JSON.parse(await redis.get(key) || 'null'); },
241
+ * async set(key, value, ttl) { await redis.setex(key, ttl, JSON.stringify(value)); },
242
+ * async del(key) { await redis.del(key); },
243
+ * async clear(pattern) {
244
+ * const keys = await redis.keys(pattern || '*');
245
+ * if (keys.length) await redis.del(...keys);
246
+ * }
247
+ * },
248
+ * ttl: 60, // 1 minute default
249
+ * })
250
+ * ]);
251
+ *
252
+ * // Reads check cache first
253
+ * const user = await userRepo.getById(id); // cached
254
+ *
255
+ * // Skip cache for fresh data
256
+ * const fresh = await userRepo.getById(id, { skipCache: true });
257
+ *
258
+ * // Mutations auto-invalidate
259
+ * await userRepo.update(id, { name: 'New Name' }); // invalidates cache
260
+ *
261
+ * // Manual invalidation for microservice sync
262
+ * await userRepo.invalidateCache(id); // invalidate single doc
263
+ * await userRepo.invalidateAllCache(); // invalidate all for this model
264
+ * ```
265
+ */
266
+
267
+ /**
268
+ * Cache plugin factory
269
+ *
270
+ * @param options - Cache configuration
271
+ * @returns Plugin instance
272
+ */
273
+ declare function cachePlugin(options: CacheOptions): Plugin;
274
+
275
+ export { type MethodRegistryRepository, aggregateHelpersPlugin, auditLogPlugin, autoInject, batchOperationsPlugin, blockIf, cachePlugin, fieldFilterPlugin, immutableField, methodRegistryPlugin, mongoOperationsPlugin, requireField, softDeletePlugin, subdocumentPlugin, timestampPlugin, uniqueField, validationChainPlugin };