@classytic/mongokit 1.0.2 → 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.
- package/README.md +772 -151
- package/dist/actions/index.cjs +479 -0
- package/dist/actions/index.cjs.map +1 -0
- package/dist/actions/index.d.cts +3 -0
- package/dist/actions/index.d.ts +3 -0
- package/dist/actions/index.js +473 -0
- package/dist/actions/index.js.map +1 -0
- package/dist/index-BfVJZF-3.d.cts +337 -0
- package/dist/index-CgOJ2pqz.d.ts +337 -0
- package/dist/index.cjs +2142 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +239 -0
- package/dist/index.d.ts +239 -0
- package/dist/index.js +2108 -0
- package/dist/index.js.map +1 -0
- package/dist/memory-cache-DG2oSSbx.d.ts +142 -0
- package/dist/memory-cache-DqfFfKes.d.cts +142 -0
- package/dist/pagination/PaginationEngine.cjs +375 -0
- package/dist/pagination/PaginationEngine.cjs.map +1 -0
- package/dist/pagination/PaginationEngine.d.cts +117 -0
- package/dist/pagination/PaginationEngine.d.ts +117 -0
- package/dist/pagination/PaginationEngine.js +369 -0
- package/dist/pagination/PaginationEngine.js.map +1 -0
- package/dist/plugins/index.cjs +874 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.cts +275 -0
- package/dist/plugins/index.d.ts +275 -0
- package/dist/plugins/index.js +857 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/types-Nxhmi1aI.d.cts +510 -0
- package/dist/types-Nxhmi1aI.d.ts +510 -0
- package/dist/utils/index.cjs +667 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +189 -0
- package/dist/utils/index.d.ts +189 -0
- package/dist/utils/index.js +643 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +54 -24
- package/src/Repository.js +0 -225
- package/src/actions/aggregate.js +0 -191
- package/src/actions/create.js +0 -59
- package/src/actions/delete.js +0 -88
- package/src/actions/index.js +0 -11
- package/src/actions/read.js +0 -156
- package/src/actions/update.js +0 -176
- package/src/hooks/lifecycle.js +0 -146
- package/src/index.js +0 -60
- package/src/plugins/aggregate-helpers.plugin.js +0 -71
- package/src/plugins/audit-log.plugin.js +0 -60
- package/src/plugins/batch-operations.plugin.js +0 -66
- package/src/plugins/field-filter.plugin.js +0 -27
- package/src/plugins/index.js +0 -19
- package/src/plugins/method-registry.plugin.js +0 -140
- package/src/plugins/mongo-operations.plugin.js +0 -313
- package/src/plugins/soft-delete.plugin.js +0 -46
- package/src/plugins/subdocument.plugin.js +0 -66
- package/src/plugins/timestamp.plugin.js +0 -19
- package/src/plugins/validation-chain.plugin.js +0 -145
- package/src/utils/field-selection.js +0 -156
- package/src/utils/index.js +0 -12
- package/types/actions/index.d.ts +0 -121
- package/types/index.d.ts +0 -104
- package/types/plugins/index.d.ts +0 -88
- package/types/utils/index.d.ts +0 -24
|
@@ -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 };
|