@decaf-ts/db-decorators 0.7.12 → 0.8.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 (50) hide show
  1. package/README.md +1 -1
  2. package/dist/db-decorators.cjs +1 -1
  3. package/dist/db-decorators.cjs.map +1 -1
  4. package/dist/db-decorators.js +1 -1
  5. package/dist/db-decorators.js.map +1 -1
  6. package/lib/esm/index.d.ts +1 -1
  7. package/lib/esm/index.js +1 -1
  8. package/lib/esm/overrides/Metadata.d.ts +1 -0
  9. package/lib/esm/overrides/Metadata.js +1 -1
  10. package/lib/esm/overrides/Metadata.js.map +1 -1
  11. package/lib/esm/repository/Repository.d.ts +303 -48
  12. package/lib/esm/repository/Repository.js +370 -61
  13. package/lib/esm/repository/Repository.js.map +1 -1
  14. package/lib/esm/repository/constants.js +3 -0
  15. package/lib/esm/repository/constants.js.map +1 -1
  16. package/lib/esm/repository/index.d.ts +1 -2
  17. package/lib/esm/repository/index.js +1 -2
  18. package/lib/esm/repository/index.js.map +1 -1
  19. package/lib/esm/repository/types.d.ts +3 -0
  20. package/lib/esm/repository/utils.js +3 -3
  21. package/lib/esm/repository/utils.js.map +1 -1
  22. package/lib/esm/repository/wrappers.d.ts +1 -0
  23. package/lib/esm/repository/wrappers.js +28 -0
  24. package/lib/esm/repository/wrappers.js.map +1 -1
  25. package/lib/index.cjs +1 -1
  26. package/lib/index.d.ts +1 -1
  27. package/lib/overrides/Metadata.cjs +1 -0
  28. package/lib/overrides/Metadata.d.ts +1 -0
  29. package/lib/overrides/Metadata.js.map +1 -1
  30. package/lib/repository/Repository.cjs +370 -61
  31. package/lib/repository/Repository.d.ts +303 -48
  32. package/lib/repository/Repository.js.map +1 -1
  33. package/lib/repository/constants.cjs +3 -0
  34. package/lib/repository/constants.js.map +1 -1
  35. package/lib/repository/index.cjs +1 -2
  36. package/lib/repository/index.d.ts +1 -2
  37. package/lib/repository/index.js.map +1 -1
  38. package/lib/repository/types.d.ts +3 -0
  39. package/lib/repository/utils.cjs +2 -2
  40. package/lib/repository/utils.js.map +1 -1
  41. package/lib/repository/wrappers.cjs +29 -0
  42. package/lib/repository/wrappers.d.ts +1 -0
  43. package/lib/repository/wrappers.js.map +1 -1
  44. package/package.json +1 -1
  45. package/lib/esm/repository/BaseRepository.d.ts +0 -352
  46. package/lib/esm/repository/BaseRepository.js +0 -423
  47. package/lib/esm/repository/BaseRepository.js.map +0 -1
  48. package/lib/repository/BaseRepository.cjs +0 -427
  49. package/lib/repository/BaseRepository.d.ts +0 -352
  50. package/lib/repository/BaseRepository.js.map +0 -1
@@ -1,427 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseRepository = void 0;
4
- require("./../overrides/index.cjs");
5
- const decorator_validation_1 = require("@decaf-ts/decorator-validation");
6
- const utils_1 = require("./utils.cjs");
7
- const constants_1 = require("./../operations/constants.cjs");
8
- const errors_1 = require("./errors.cjs");
9
- const wrappers_1 = require("./wrappers.cjs");
10
- const Context_1 = require("./Context.cjs");
11
- /**
12
- * @description Base repository implementation providing CRUD operations for models.
13
- * @summary The BaseRepository class serves as a foundation for repository implementations, providing
14
- * abstract and concrete methods for creating, reading, updating, and deleting model instances.
15
- * It handles operation lifecycles including prefix and suffix operations, and enforces decorators.
16
- * @template M - The model type extending Model
17
- * @template F - The repository flags type, defaults to RepositoryFlags
18
- * @template C - The context type, defaults to Context<F>
19
- * @param {Constructor<M>} clazz - The constructor for the model class
20
- * @class BaseRepository
21
- * @example
22
- * class UserModel extends Model {
23
- * @id()
24
- * id: string;
25
- *
26
- * @required()
27
- * name: string;
28
- * }
29
- *
30
- * class UserRepository extends BaseRepository<UserModel> {
31
- * constructor() {
32
- * super(UserModel);
33
- * }
34
- *
35
- * async create(model: UserModel): Promise<UserModel> {
36
- * // Implementation
37
- * return model;
38
- * }
39
- *
40
- * async read(key: string): Promise<UserModel> {
41
- * // Implementation
42
- * return new UserModel({ id: key, name: 'User' });
43
- * }
44
- *
45
- * async update(model: UserModel): Promise<UserModel> {
46
- * // Implementation
47
- * return model;
48
- * }
49
- *
50
- * async delete(key: string): Promise<UserModel> {
51
- * // Implementation
52
- * const model = await this.read(key);
53
- * return model;
54
- * }
55
- * }
56
- *
57
- * @mermaid
58
- * sequenceDiagram
59
- * participant C as Client
60
- * participant R as Repository
61
- * participant P as Prefix Methods
62
- * participant D as Database
63
- * participant S as Suffix Methods
64
- * participant V as Validators/Decorators
65
- *
66
- * Note over C,V: Create Operation
67
- * C->>R: create(model)
68
- * R->>P: createPrefix(model)
69
- * P->>V: enforceDBDecorators(ON)
70
- * P->>D: Database operation
71
- * D->>S: createSuffix(model)
72
- * S->>V: enforceDBDecorators(AFTER)
73
- * S->>C: Return model
74
- *
75
- * Note over C,V: Read Operation
76
- * C->>R: read(key)
77
- * R->>P: readPrefix(key)
78
- * P->>V: enforceDBDecorators(ON)
79
- * P->>D: Database operation
80
- * D->>S: readSuffix(model)
81
- * S->>V: enforceDBDecorators(AFTER)
82
- * S->>C: Return model
83
- *
84
- * Note over C,V: Update Operation
85
- * C->>R: update(model)
86
- * R->>P: updatePrefix(model)
87
- * P->>V: enforceDBDecorators(ON)
88
- * P->>D: Database operation
89
- * D->>S: updateSuffix(model)
90
- * S->>V: enforceDBDecorators(AFTER)
91
- * S->>C: Return model
92
- *
93
- * Note over C,V: Delete Operation
94
- * C->>R: delete(key)
95
- * R->>P: deletePrefix(key)
96
- * P->>V: enforceDBDecorators(ON)
97
- * P->>D: Database operation
98
- * D->>S: deleteSuffix(model)
99
- * S->>V: enforceDBDecorators(AFTER)
100
- * S->>C: Return model
101
- */
102
- class BaseRepository {
103
- /**
104
- * @description Gets the model class constructor.
105
- * @summary Retrieves the constructor for the model class associated with this repository.
106
- * Throws an error if no class definition is found.
107
- * @return {Constructor<M>} The constructor for the model class
108
- */
109
- get class() {
110
- if (!this._class)
111
- throw new errors_1.InternalError(`No class definition found for this repository`);
112
- return this._class;
113
- }
114
- /**
115
- * @description Gets the primary key property name of the model.
116
- * @summary Retrieves the name of the property that serves as the primary key for the model.
117
- * If not already determined, it finds the primary key using the model's decorators.
118
- * @return The name of the primary key property
119
- */
120
- get pk() {
121
- return decorator_validation_1.Model.pk(this.class);
122
- }
123
- /**
124
- * @description Gets the primary key properties.
125
- * @summary Retrieves the properties associated with the primary key of the model.
126
- * If not already determined, it triggers the pk getter to find the primary key properties.
127
- * @return {any} The properties of the primary key
128
- */
129
- get pkProps() {
130
- return decorator_validation_1.Model.pkProps(this.class);
131
- }
132
- constructor(clazz) {
133
- if (clazz)
134
- this._class = clazz;
135
- // eslint-disable-next-line @typescript-eslint/no-this-alias
136
- const self = this;
137
- [this.create, this.read, this.update, this.delete].forEach((m) => {
138
- const name = m.name;
139
- (0, wrappers_1.wrapMethodWithContext)(self, self[name + "Prefix"], m, self[name + "Suffix"]);
140
- });
141
- }
142
- /**
143
- * @description Creates multiple model instances in the repository.
144
- * @summary Persists multiple model instances to the underlying data store by calling
145
- * the create method for each model in the array.
146
- * @param {M[]} models - The array of model instances to create
147
- * @param {any[]} args - Additional arguments for the create operation
148
- * @return {Promise<M[]>} A promise that resolves to an array of created model instances
149
- */
150
- async createAll(models, ...args) {
151
- return Promise.all(models.map((m) => this.create(m, ...args)));
152
- }
153
- /**
154
- * @description Prepares a model for creation and executes pre-creation operations.
155
- * @summary Processes a model before it is created in the data store. This includes
156
- * creating a context, instantiating a new model instance, and enforcing any decorators
157
- * that should be applied before creation.
158
- * @param {M} model - The model instance to prepare for creation
159
- * @param {any[]} args - Additional arguments for the create operation
160
- * @return A promise that resolves to an array containing the prepared model and context arguments
161
- */
162
- async createPrefix(model, ...args) {
163
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.CREATE, this.class, args);
164
- model = new this.class(model);
165
- await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.ON);
166
- return [model, ...contextArgs.args];
167
- }
168
- /**
169
- * @description Processes a model after creation and executes post-creation operations.
170
- * @summary Finalizes a model after it has been created in the data store. This includes
171
- * enforcing any decorators that should be applied after creation.
172
- * @param {M} model - The model instance that was created
173
- * @param {C} context - The context for the operation
174
- * @return {Promise<M>} A promise that resolves to the processed model instance
175
- */
176
- async createSuffix(model, context) {
177
- await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.AFTER);
178
- return model;
179
- }
180
- /**
181
- * @description Prepares multiple models for creation and executes pre-creation operations.
182
- * @summary Processes multiple models before they are created in the data store. This includes
183
- * creating a context, instantiating new model instances, and enforcing any decorators
184
- * that should be applied before creation for each model.
185
- * @param {M[]} models - The array of model instances to prepare for creation
186
- * @param {any[]} args - Additional arguments for the create operation
187
- * @return A promise that resolves to an array containing the prepared models and context arguments
188
- */
189
- async createAllPrefix(models, ...args) {
190
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.CREATE, this.class, args);
191
- await Promise.all(models.map(async (m) => {
192
- m = new this.class(m);
193
- await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.ON);
194
- return m;
195
- }));
196
- return [models, ...contextArgs.args];
197
- }
198
- /**
199
- * @description Processes multiple models after creation and executes post-creation operations.
200
- * @summary Finalizes multiple models after they have been created in the data store. This includes
201
- * enforcing any decorators that should be applied after creation for each model.
202
- * @param {M[]} models - The array of model instances that were created
203
- * @param {C} context - The context for the operation
204
- * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
205
- */
206
- async createAllSuffix(models, context) {
207
- await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.AFTER)));
208
- return models;
209
- }
210
- /**
211
- * @description Retrieves multiple model instances from the repository by their primary keys.
212
- * @summary Fetches multiple model instances from the underlying data store using their primary keys
213
- * by calling the read method for each key in the array.
214
- * @param {string[] | number[]} keys - The array of primary keys of the models to retrieve
215
- * @param {any[]} args - Additional arguments for the read operation
216
- * @return {Promise<M[]>} A promise that resolves to an array of retrieved model instances
217
- */
218
- async readAll(keys, ...args) {
219
- return await Promise.all(keys.map((id) => this.read(id, ...args)));
220
- }
221
- /**
222
- * @description Processes a model after retrieval and executes post-read operations.
223
- * @summary Finalizes a model after it has been retrieved from the data store. This includes
224
- * enforcing any decorators that should be applied after reading.
225
- * @param {M} model - The model instance that was retrieved
226
- * @param {C} context - The context for the operation
227
- * @return {Promise<M>} A promise that resolves to the processed model instance
228
- */
229
- async readSuffix(model, context) {
230
- await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.READ, constants_1.OperationKeys.AFTER);
231
- return model;
232
- }
233
- /**
234
- * @description Prepares for reading a model and executes pre-read operations.
235
- * @summary Processes a key before a model is read from the data store. This includes
236
- * creating a context, instantiating a new model instance with the key, and enforcing any decorators
237
- * that should be applied before reading.
238
- * @param {string} key - The primary key of the model to read
239
- * @param {any[]} args - Additional arguments for the read operation
240
- * @return A promise that resolves to an array containing the key and context arguments
241
- */
242
- async readPrefix(key, ...args) {
243
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.READ, this.class, args);
244
- const model = new this.class();
245
- model[this.pk] = key;
246
- await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.READ, constants_1.OperationKeys.ON);
247
- return [key, ...contextArgs.args];
248
- }
249
- /**
250
- * @description Prepares for reading multiple models and executes pre-read operations.
251
- * @summary Processes multiple keys before models are read from the data store. This includes
252
- * creating a context, instantiating new model instances with the keys, and enforcing any decorators
253
- * that should be applied before reading for each key.
254
- * @param {string[] | number[]} keys - The array of primary keys of the models to read
255
- * @param {any[]} args - Additional arguments for the read operation
256
- * @return A promise that resolves to an array containing the keys and context arguments
257
- */
258
- async readAllPrefix(keys, ...args) {
259
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.READ, this.class, args);
260
- await Promise.all(keys.map(async (k) => {
261
- const m = new this.class();
262
- m[this.pk] = k;
263
- return (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.READ, constants_1.OperationKeys.ON);
264
- }));
265
- return [keys, ...contextArgs.args];
266
- }
267
- /**
268
- * @description Processes multiple models after retrieval and executes post-read operations.
269
- * @summary Finalizes multiple models after they have been retrieved from the data store. This includes
270
- * enforcing any decorators that should be applied after reading for each model.
271
- * @param {M[]} models - The array of model instances that were retrieved
272
- * @param {C} context - The context for the operation
273
- * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
274
- */
275
- async readAllSuffix(models, context) {
276
- await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.READ, constants_1.OperationKeys.AFTER)));
277
- return models;
278
- }
279
- /**
280
- * @description Updates multiple model instances in the repository.
281
- * @summary Updates multiple model instances in the underlying data store by calling
282
- * the update method for each model in the array.
283
- * @param {M[]} models - The array of model instances to update
284
- * @param {any[]} args - Additional arguments for the update operation
285
- * @return {Promise<M[]>} A promise that resolves to an array of updated model instances
286
- */
287
- async updateAll(models, ...args) {
288
- return Promise.all(models.map((m) => this.update(m, ...args)));
289
- }
290
- /**
291
- * @description Processes a model after update and executes post-update operations.
292
- * @summary Finalizes a model after it has been updated in the data store. This includes
293
- * enforcing any decorators that should be applied after updating.
294
- * @param {M} model - The model instance that was updated
295
- * @param {C} context - The context for the operation
296
- * @return {Promise<M>} A promise that resolves to the processed model instance
297
- */
298
- async updateSuffix(model, context) {
299
- await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.AFTER);
300
- return model;
301
- }
302
- /**
303
- * @description Prepares a model for update and executes pre-update operations.
304
- * @summary Processes a model before it is updated in the data store. This includes
305
- * creating a context, validating the primary key, retrieving the existing model,
306
- * and enforcing any decorators that should be applied before updating.
307
- * @param {M} model - The model instance to prepare for update
308
- * @param {any[]} args - Additional arguments for the update operation
309
- * @return A promise that resolves to an array containing the prepared model and context arguments
310
- */
311
- async updatePrefix(model, ...args) {
312
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.UPDATE, this.class, args);
313
- const id = model[this.pk];
314
- if (!id)
315
- throw new errors_1.InternalError(`No value for the Id is defined under the property ${this.pk}`);
316
- const oldModel = await this.read(id);
317
- await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.ON, oldModel);
318
- return [model, ...contextArgs.args];
319
- }
320
- /**
321
- * @description Prepares multiple models for update and executes pre-update operations.
322
- * @summary Processes multiple models before they are updated in the data store. This includes
323
- * creating a context, instantiating new model instances, and enforcing any decorators
324
- * that should be applied before updating for each model.
325
- * @param {M[]} models - The array of model instances to prepare for update
326
- * @param {any[]} args - Additional arguments for the update operation
327
- * @return A promise that resolves to an array containing the prepared models and context arguments
328
- */
329
- async updateAllPrefix(models, ...args) {
330
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.UPDATE, this.class, args);
331
- await Promise.all(models.map((m) => {
332
- m = new this.class(m);
333
- (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.ON);
334
- return m;
335
- }));
336
- return [models, ...contextArgs.args];
337
- }
338
- /**
339
- * @description Processes multiple models after update and executes post-update operations.
340
- * @summary Finalizes multiple models after they have been updated in the data store. This includes
341
- * enforcing any decorators that should be applied after updating for each model.
342
- * @param {M[]} models - The array of model instances that were updated
343
- * @param {C} context - The context for the operation
344
- * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
345
- */
346
- async updateAllSuffix(models, context) {
347
- await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.AFTER)));
348
- return models;
349
- }
350
- /**
351
- * @description Deletes multiple model instances from the repository by their primary keys.
352
- * @summary Removes multiple model instances from the underlying data store using their primary keys
353
- * by calling the delete method for each key in the array.
354
- * @param {string[] | number[]} keys - The array of primary keys of the models to delete
355
- * @param {any[]} args - Additional arguments for the delete operation
356
- * @return {Promise<M[]>} A promise that resolves to an array of deleted model instances
357
- */
358
- async deleteAll(keys, ...args) {
359
- return Promise.all(keys.map((k) => this.delete(k, ...args)));
360
- }
361
- /**
362
- * @description Processes a model after deletion and executes post-delete operations.
363
- * @summary Finalizes a model after it has been deleted from the data store. This includes
364
- * enforcing any decorators that should be applied after deletion.
365
- * @param {M} model - The model instance that was deleted
366
- * @param {C} context - The context for the operation
367
- * @return {Promise<M>} A promise that resolves to the processed model instance
368
- */
369
- async deleteSuffix(model, context) {
370
- await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.AFTER);
371
- return model;
372
- }
373
- /**
374
- * @description Prepares for deleting a model and executes pre-delete operations.
375
- * @summary Processes a key before a model is deleted from the data store. This includes
376
- * creating a context, retrieving the model to be deleted, and enforcing any decorators
377
- * that should be applied before deletion.
378
- * @param {any} key - The primary key of the model to delete
379
- * @param {any[]} args - Additional arguments for the delete operation
380
- * @return A promise that resolves to an array containing the key and context arguments
381
- */
382
- async deletePrefix(key, ...args) {
383
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.DELETE, this.class, args);
384
- const model = await this.read(key, ...contextArgs.args);
385
- await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.ON);
386
- return [key, ...contextArgs.args];
387
- }
388
- /**
389
- * @description Prepares for deleting multiple models and executes pre-delete operations.
390
- * @summary Processes multiple keys before models are deleted from the data store. This includes
391
- * creating a context, retrieving the models to be deleted, and enforcing any decorators
392
- * that should be applied before deletion for each model.
393
- * @param {string[] | number[]} keys - The array of primary keys of the models to delete
394
- * @param {any[]} args - Additional arguments for the delete operation
395
- * @return A promise that resolves to an array containing the keys and context arguments
396
- */
397
- async deleteAllPrefix(keys, ...args) {
398
- const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.DELETE, this.class, args);
399
- const models = await this.readAll(keys, ...contextArgs.args);
400
- await Promise.all(models.map(async (m) => {
401
- return (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.ON);
402
- }));
403
- return [keys, ...contextArgs.args];
404
- }
405
- /**
406
- * @description Processes multiple models after deletion and executes post-delete operations.
407
- * @summary Finalizes multiple models after they have been deleted from the data store. This includes
408
- * enforcing any decorators that should be applied after deletion for each model.
409
- * @param {M[]} models - The array of model instances that were deleted
410
- * @param {C} context - The context for the operation
411
- * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
412
- */
413
- async deleteAllSuffix(models, context) {
414
- await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.AFTER)));
415
- return models;
416
- }
417
- /**
418
- * @description Returns a string representation of the repository.
419
- * @summary Creates a string that identifies this repository by the name of its model class.
420
- * @return {string} A string representation of the repository
421
- */
422
- toString() {
423
- return `${this.class.name} Repository`;
424
- }
425
- }
426
- exports.BaseRepository = BaseRepository;
427
- //# sourceMappingURL=BaseRepository.js.map