@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 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/repository/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,qCAAgC;AACzE,OAAO,EAAE,aAAa,EAAE,oBAAiB;AAGzC,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,cAAc,GACf,sCAAiC;AAElC,OAAO,EAAe,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAQ7D,MAAM,UAAU,mBAAmB,CACjC,MAA4C;IAE5C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAyB,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE;QACvD,IAAI,CAAC;YACH,KAAK;gBACH,OAAO,KAAK,KAAK,QAAQ;oBACvB,CAAC,CAAC,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACtC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC;IACf,CAAC,EAAE,SAAS,CAAC,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAKvC,IAAO,EACP,OAA+B,EAC/B,KAAQ,EACR,SAAiB,EACjB,MAAc,EACd,QAAY;IAEZ,MAAM,U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,GAAG,WAAW,EAAE,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAEhD,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAC7B,KAAK,CAAC,WAA6B,EACnC,eAAe,CAAC,UAAU,CAC3B,CAAC;IAEF,IAAI,CAAC,UAAU;QAAE,OAAO;IACxB,OAAO,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,CACnC,CAAC,KAAsD,EAAE,SAAS,EAAE,EAAE;QACpE,MAAM,GAAG,GAAG,MAAM;YAChB,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE;YACrC,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;QAC1B,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QACjE,MAAM,IAAI,GAAG,EAAE,CAAC;QAChB,KAAK,MAAM,CAAC,IAAI,GAAG;YAAE,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAE1D,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACxB,IAAI,CAAC,KAAK;gBAAE,KAAK,GAAG,EAAE,CAAC;YACvB,KAAK,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;QAC1B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,EACD,SAAS,CACV,CAAC;AACJ,CAAC"}
@@ -36,3 +36,4 @@ export declare function suffixMethod(obj: any, before: (...args: any[]) => any,
36
36
  * @memberOf module:db-decorators
37
37
  */
38
38
  export declare function wrapMethodWithContext(obj: any, before: (...args: any[]) => any, method: (...args: any[]) => any, after: (...args: any[]) => any, methodName?: string): void;
39
+ export declare function wrapMethodWithContextForUpdate(obj: any, before: (...args: any[]) => any, method: (...args: any[]) => any, after: (...args: any[]) => any, methodName?: string): void;
@@ -1,5 +1,6 @@
1
1
  import { Context } from "./Context.js";
2
2
  import { InternalError } from "./errors.js";
3
+ import { Model } from "@decaf-ts/decorator-validation";
3
4
  /**
4
5
  * @summary Util method to change a method of an object prefixing it with another
5
6
  * @param {any} obj The Base Object
@@ -83,4 +84,31 @@ export function wrapMethodWithContext(obj, before, method, after, methodName) {
83
84
  },
84
85
  });
85
86
  }
87
+ export function wrapMethodWithContextForUpdate(obj, before, method, after, methodName) {
88
+ const name = methodName ? methodName : method.name;
89
+ obj[name] = new Proxy(obj[name], {
90
+ apply: async (target, thisArg, argArray) => {
91
+ let transformedArgs = before.call(thisArg, ...argArray);
92
+ if (transformedArgs instanceof Promise)
93
+ transformedArgs = await transformedArgs;
94
+ const oldModel = transformedArgs.pop();
95
+ const context = transformedArgs[transformedArgs.length - 1];
96
+ if (!(context instanceof Context))
97
+ throw new InternalError("Missing a context");
98
+ if (context.get("applyUpdateValidation") &&
99
+ !context.get("ignoreDevSafeGuards") &&
100
+ !(oldModel instanceof Model) &&
101
+ (!Array.isArray(oldModel) || !oldModel.every((o) => o instanceof Model))) {
102
+ throw new InternalError(`No previous versions os models found`);
103
+ }
104
+ let results = target.call(thisArg, ...transformedArgs);
105
+ if (results instanceof Promise)
106
+ results = await results;
107
+ results = after.call(thisArg, results, oldModel, context);
108
+ if (results instanceof Promise)
109
+ results = await results;
110
+ return results;
111
+ },
112
+ });
113
+ }
86
114
  //# sourceMappingURL=wrappers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrappers.js","sourceRoot":"","sources":["../../../src/repository/wrappers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,qBAAkB;AACpC,OAAO,EAAE,aAAa,EAAE,oBAAiB;AAEzC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC1B,GAAQ,EACR,KAA8B,EAC9B,MAA+B,EAC/B,SA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,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,EACxE,CAAC;gBACD,MAAM,IAAI,aAAa,CAAC,sCAAsC,CAAC,CAAC;YAClE,CAAC;YAED,IAAI,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,eAAe,CAAC,CAAC;YACvD,IAAI,OAAO,YAAY,OAAO;gBAAE,OAAO,GAAG,MAAM,OAAO,CAAC;YACxD,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;YAC1D,IAAI,OAAO,YAAY,OAAO;gBAAE,OAAO,GAAG,MAAM,OAAO,CAAC;YACxD,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
package/lib/index.cjs CHANGED
@@ -34,7 +34,7 @@ __exportStar(require("./validation/index.cjs"), exports);
34
34
  * @const VERSION
35
35
  * @memberOf module:db-decorators
36
36
  */
37
- exports.VERSION = "0.7.11";
37
+ exports.VERSION = "0.7.13";
38
38
  exports.PACKAGE_NAME = "@decaf-ts/db-decorators";
39
39
  decoration_1.Metadata.registerLibrary(exports.PACKAGE_NAME, exports.VERSION);
40
40
  //# sourceMappingURL=index.js.map
package/lib/index.d.ts CHANGED
@@ -16,5 +16,5 @@ export * from "./validation";
16
16
  * @const VERSION
17
17
  * @memberOf module:db-decorators
18
18
  */
19
- export declare const VERSION = "0.7.11";
19
+ export declare const VERSION = "0.7.13";
20
20
  export declare const PACKAGE_NAME = "@decaf-ts/db-decorators";
@@ -1,3 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("@decaf-ts/decoration");
3
4
  //# sourceMappingURL=Metadata.js.map
@@ -1,3 +1,4 @@
1
+ import "@decaf-ts/decoration";
1
2
  import type { Constructor } from "@decaf-ts/decoration";
2
3
  import type { Model } from "@decaf-ts/decorator-validation";
3
4
  declare module "@decaf-ts/decoration" {
@@ -1 +1 @@
1
- {"version":3,"file":"Metadata.js","sourceRoot":"","sources":["../../src/overrides/Metadata.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"Metadata.js","sourceRoot":"","sources":["../../src/overrides/Metadata.ts"],"names":[],"mappings":";;AAAA,gCAA8B"}
@@ -1,20 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Repository = void 0;
4
+ require("./../overrides/index.cjs");
5
+ const decorator_validation_1 = require("@decaf-ts/decorator-validation");
4
6
  const utils_1 = require("./utils.cjs");
5
7
  const constants_1 = require("./../operations/constants.cjs");
6
8
  const errors_1 = require("./errors.cjs");
7
- const BaseRepository_1 = require("./BaseRepository.cjs");
8
- const decorator_validation_1 = require("@decaf-ts/decorator-validation");
9
+ const wrappers_1 = require("./wrappers.cjs");
9
10
  const Context_1 = require("./Context.cjs");
10
11
  /**
11
- * @description Concrete repository implementation with validation support.
12
- * @summary The Repository class extends BaseRepository to provide additional validation
13
- * functionality. It overrides prefix methods to perform model validation before database
14
- * operations and throws ValidationError when validation fails.
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.
15
16
  * @template M - The model type extending Model
16
17
  * @template F - The repository flags type, defaults to RepositoryFlags
17
18
  * @template C - The context type, defaults to Context<F>
19
+ * @param {Constructor<M>} clazz - The constructor for the model class
18
20
  * @class Repository
19
21
  * @example
20
22
  * class UserModel extends Model {
@@ -22,42 +24,141 @@ const Context_1 = require("./Context.cjs");
22
24
  * id: string;
23
25
  *
24
26
  * @required()
25
- * @minLength(3)
26
27
  * name: string;
27
28
  * }
28
29
  *
29
- * class UserRepository extends Repository<UserModel> {
30
+ * class UserRepository extends BaseRepository<UserModel> {
30
31
  * constructor() {
31
32
  * super(UserModel);
32
33
  * }
33
34
  *
34
35
  * async create(model: UserModel): Promise<UserModel> {
35
- * // Implementation with automatic validation
36
+ * // Implementation
36
37
  * return model;
37
38
  * }
38
- * }
39
39
  *
40
- * // Using the repository
41
- * const repo = new UserRepository();
42
- * try {
43
- * const user = await repo.create({ name: 'Jo' }); // Will throw ValidationError
44
- * } catch (error) {
45
- * console.error(error); // ValidationError: name must be at least 3 characters
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
+ * }
46
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
47
101
  */
48
- class Repository extends BaseRepository_1.BaseRepository {
102
+ class Repository {
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
+ }
49
132
  constructor(clazz) {
50
- super(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.delete].forEach((m) => {
138
+ const name = m.name;
139
+ (0, wrappers_1.wrapMethodWithContext)(self, self[name + "Prefix"], m, self[name + "Suffix"]);
140
+ });
141
+ (0, wrappers_1.wrapMethodWithContextForUpdate)(self, self[this.update.name + "Prefix"], this.update, self[this.update.name + "Suffix"]);
51
142
  }
52
143
  /**
53
- * @description Prepares a model for creation with validation.
54
- * @summary Overrides the base createPrefix method to add validation checks.
55
- * Creates a context, instantiates a new model, enforces decorators, and validates
56
- * the model before allowing creation to proceed.
144
+ * @description Creates multiple model instances in the repository.
145
+ * @summary Persists multiple model instances to the underlying data store by calling
146
+ * the create method for each model in the array.
147
+ * @param {M[]} models - The array of model instances to create
148
+ * @param {any[]} args - Additional arguments for the create operation
149
+ * @return {Promise<M[]>} A promise that resolves to an array of created model instances
150
+ */
151
+ async createAll(models, ...args) {
152
+ return Promise.all(models.map((m) => this.create(m, ...args)));
153
+ }
154
+ /**
155
+ * @description Prepares a model for creation and executes pre-creation operations.
156
+ * @summary Processes a model before it is created in the data store. This includes
157
+ * creating a context, instantiating a new model instance, and enforcing any decorators
158
+ * that should be applied before creation.
57
159
  * @param {M} model - The model instance to prepare for creation
58
160
  * @param {any[]} args - Additional arguments for the create operation
59
- * @return A promise that resolves to an array containing the validated model and context arguments
60
- * @throws {ValidationError} If the model fails validation
161
+ * @return A promise that resolves to an array containing the prepared model and context arguments
61
162
  */
62
163
  async createPrefix(model, ...args) {
63
164
  const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.CREATE, this.class, args);
@@ -72,14 +173,25 @@ class Repository extends BaseRepository_1.BaseRepository {
72
173
  return [model, ...contextArgs.args];
73
174
  }
74
175
  /**
75
- * @description Prepares multiple models for creation with validation.
76
- * @summary Overrides the base createAllPrefix method to add validation checks for multiple models.
77
- * Creates a context, instantiates new models, enforces decorators, and validates
78
- * each model before allowing creation to proceed. Collects validation errors from all models.
176
+ * @description Processes a model after creation and executes post-creation operations.
177
+ * @summary Finalizes a model after it has been created in the data store. This includes
178
+ * enforcing any decorators that should be applied after creation.
179
+ * @param {M} model - The model instance that was created
180
+ * @param {C} context - The context for the operation
181
+ * @return {Promise<M>} A promise that resolves to the processed model instance
182
+ */
183
+ async createSuffix(model, context) {
184
+ await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.AFTER);
185
+ return model;
186
+ }
187
+ /**
188
+ * @description Prepares multiple models for creation and executes pre-creation operations.
189
+ * @summary Processes multiple models before they are created in the data store. This includes
190
+ * creating a context, instantiating new model instances, and enforcing any decorators
191
+ * that should be applied before creation for each model.
79
192
  * @param {M[]} models - The array of model instances to prepare for creation
80
193
  * @param {any[]} args - Additional arguments for the create operation
81
- * @return {Promise<any[]>} A promise that resolves to an array containing the validated models and context arguments
82
- * @throws {ValidationError} If any model fails validation, with details about which models failed
194
+ * @return A promise that resolves to an array containing the prepared models and context arguments
83
195
  */
84
196
  async createAllPrefix(models, ...args) {
85
197
  const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.CREATE, this.class, args);
@@ -100,26 +212,132 @@ class Repository extends BaseRepository_1.BaseRepository {
100
212
  return [models, ...contextArgs.args];
101
213
  }
102
214
  /**
103
- * @description Prepares a model for update with validation.
104
- * @summary Overrides the base updatePrefix method to add validation checks.
105
- * Creates a context, validates the primary key, retrieves the existing model,
106
- * merges the old and new models, enforces decorators, and validates the model
107
- * before allowing the update to proceed.
215
+ * @description Processes multiple models after creation and executes post-creation operations.
216
+ * @summary Finalizes multiple models after they have been created in the data store. This includes
217
+ * enforcing any decorators that should be applied after creation for each model.
218
+ * @param {M[]} models - The array of model instances that were created
219
+ * @param {C} context - The context for the operation
220
+ * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
221
+ */
222
+ async createAllSuffix(models, context) {
223
+ await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.CREATE, constants_1.OperationKeys.AFTER)));
224
+ return models;
225
+ }
226
+ /**
227
+ * @description Retrieves multiple model instances from the repository by their primary keys.
228
+ * @summary Fetches multiple model instances from the underlying data store using their primary keys
229
+ * by calling the read method for each key in the array.
230
+ * @param {string[] | number[]} keys - The array of primary keys of the models to retrieve
231
+ * @param {any[]} args - Additional arguments for the read operation
232
+ * @return {Promise<M[]>} A promise that resolves to an array of retrieved model instances
233
+ */
234
+ async readAll(keys, ...args) {
235
+ return await Promise.all(keys.map((id) => this.read(id, ...args)));
236
+ }
237
+ /**
238
+ * @description Processes a model after retrieval and executes post-read operations.
239
+ * @summary Finalizes a model after it has been retrieved from the data store. This includes
240
+ * enforcing any decorators that should be applied after reading.
241
+ * @param {M} model - The model instance that was retrieved
242
+ * @param {C} context - The context for the operation
243
+ * @return {Promise<M>} A promise that resolves to the processed model instance
244
+ */
245
+ async readSuffix(model, context) {
246
+ await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.READ, constants_1.OperationKeys.AFTER);
247
+ return model;
248
+ }
249
+ /**
250
+ * @description Prepares for reading a model and executes pre-read operations.
251
+ * @summary Processes a key before a model is read from the data store. This includes
252
+ * creating a context, instantiating a new model instance with the key, and enforcing any decorators
253
+ * that should be applied before reading.
254
+ * @param {string} key - The primary key of the model to read
255
+ * @param {any[]} args - Additional arguments for the read operation
256
+ * @return A promise that resolves to an array containing the key and context arguments
257
+ */
258
+ async readPrefix(key, ...args) {
259
+ const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.READ, this.class, args);
260
+ const model = new this.class();
261
+ model[this.pk] = key;
262
+ await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.READ, constants_1.OperationKeys.ON);
263
+ return [key, ...contextArgs.args];
264
+ }
265
+ /**
266
+ * @description Prepares for reading multiple models and executes pre-read operations.
267
+ * @summary Processes multiple keys before models are read from the data store. This includes
268
+ * creating a context, instantiating new model instances with the keys, and enforcing any decorators
269
+ * that should be applied before reading for each key.
270
+ * @param {string[] | number[]} keys - The array of primary keys of the models to read
271
+ * @param {any[]} args - Additional arguments for the read operation
272
+ * @return A promise that resolves to an array containing the keys and context arguments
273
+ */
274
+ async readAllPrefix(keys, ...args) {
275
+ const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.READ, this.class, args);
276
+ await Promise.all(keys.map(async (k) => {
277
+ const m = new this.class();
278
+ m[this.pk] = k;
279
+ return (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.READ, constants_1.OperationKeys.ON);
280
+ }));
281
+ return [keys, ...contextArgs.args];
282
+ }
283
+ /**
284
+ * @description Processes multiple models after retrieval and executes post-read operations.
285
+ * @summary Finalizes multiple models after they have been retrieved from the data store. This includes
286
+ * enforcing any decorators that should be applied after reading for each model.
287
+ * @param {M[]} models - The array of model instances that were retrieved
288
+ * @param {C} context - The context for the operation
289
+ * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
290
+ */
291
+ async readAllSuffix(models, context) {
292
+ await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.READ, constants_1.OperationKeys.AFTER)));
293
+ return models;
294
+ }
295
+ /**
296
+ * @description Updates multiple model instances in the repository.
297
+ * @summary Updates multiple model instances in the underlying data store by calling
298
+ * the update method for each model in the array.
299
+ * @param {M[]} models - The array of model instances to update
300
+ * @param {any[]} args - Additional arguments for the update operation
301
+ * @return {Promise<M[]>} A promise that resolves to an array of updated model instances
302
+ */
303
+ async updateAll(models, ...args) {
304
+ return Promise.all(models.map((m) => this.update(m, ...args)));
305
+ }
306
+ /**
307
+ * @description Processes a model after update and executes post-update operations.
308
+ * @summary Finalizes a model after it has been updated in the data store. This includes
309
+ * enforcing any decorators that should be applied after updating.
310
+ * @param {M} model - The model instance that was updated
311
+ * @param {C} context - The context for the operation
312
+ * @return {Promise<M>} A promise that resolves to the processed model instance
313
+ */
314
+ async updateSuffix(model, oldModel, context) {
315
+ await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.AFTER, oldModel);
316
+ return model;
317
+ }
318
+ /**
319
+ * @description Prepares a model for update and executes pre-update operations.
320
+ * @summary Processes a model before it is updated in the data store. This includes
321
+ * creating a context, validating the primary key, retrieving the existing model,
322
+ * and enforcing any decorators that should be applied before updating.
108
323
  * @param {M} model - The model instance to prepare for update
109
324
  * @param {any[]} args - Additional arguments for the update operation
110
- * @return A promise that resolves to an array containing the validated model and context arguments
111
- * @throws {InternalError} If the model doesn't have a primary key value
112
- * @throws {ValidationError} If the model fails validation
325
+ * @return A promise that resolves to an array containing the prepared model and context arguments
113
326
  */
114
327
  async updatePrefix(model, ...args) {
115
328
  const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.UPDATE, this.class, args);
329
+ const context = contextArgs.context;
116
330
  const ignoreHandlers = contextArgs.context.get("ignoreHandlers");
117
331
  const ignoreValidate = contextArgs.context.get("ignoreValidation");
118
332
  const pk = model[this.pk];
119
333
  if (!pk)
120
334
  throw new errors_1.InternalError(`No value for the Id is defined under the property ${this.pk}`);
121
- const oldModel = await this.read(pk);
122
- model = decorator_validation_1.Model.merge(oldModel, model, this.class);
335
+ let oldModel;
336
+ if (context.get("applyUpdateValidation")) {
337
+ oldModel = await this.read(pk);
338
+ if (context.get("mergeForUpdate"))
339
+ model = decorator_validation_1.Model.merge(oldModel, model, this.class);
340
+ }
123
341
  if (!ignoreHandlers)
124
342
  await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.ON, oldModel);
125
343
  if (!ignoreValidate) {
@@ -127,50 +345,141 @@ class Repository extends BaseRepository_1.BaseRepository {
127
345
  if (errors)
128
346
  throw new errors_1.ValidationError(errors.toString());
129
347
  }
130
- return [model, ...contextArgs.args];
348
+ return [model, ...contextArgs.args, oldModel];
131
349
  }
132
350
  /**
133
- * @description Prepares multiple models for update with validation.
134
- * @summary Overrides the base updateAllPrefix method to add validation checks for multiple models.
135
- * Creates a context, validates primary keys, retrieves existing models, merges old and new models,
136
- * enforces decorators, and validates each model before allowing updates to proceed.
137
- * Collects validation errors from all models.
351
+ * @description Prepares multiple models for update and executes pre-update operations.
352
+ * @summary Processes multiple models before they are updated in the data store. This includes
353
+ * creating a context, instantiating new model instances, and enforcing any decorators
354
+ * that should be applied before updating for each model.
138
355
  * @param {M[]} models - The array of model instances to prepare for update
139
356
  * @param {any[]} args - Additional arguments for the update operation
140
- * @return A promise that resolves to an array containing the validated models and context arguments
141
- * @throws {InternalError} If any model doesn't have a primary key value
142
- * @throws {ValidationError} If any model fails validation, with details about which models failed
357
+ * @return A promise that resolves to an array containing the prepared models and context arguments
143
358
  */
144
359
  async updateAllPrefix(models, ...args) {
145
360
  const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.UPDATE, this.class, args);
146
- const ignoreHandlers = contextArgs.context.get("ignoreHandlers");
147
- const ignoreValidate = contextArgs.context.get("ignoreValidation");
361
+ const context = contextArgs.context;
362
+ const ignoreHandlers = context.get("ignoreHandlers");
363
+ const ignoreValidate = context.get("ignoreValidation");
148
364
  const ids = models.map((m) => {
149
365
  const id = m[this.pk];
150
366
  if (typeof id === "undefined")
151
367
  throw new errors_1.InternalError(`No value for the Id is defined under the property ${this.pk}`);
152
368
  return id;
153
369
  });
154
- const oldModels = await this.readAll(ids, ...contextArgs.args);
155
- models = models.map((m, i) => decorator_validation_1.Model.merge(oldModels[i], m, this.class));
370
+ let oldModels;
371
+ if (context.get("applyUpdateValidation")) {
372
+ oldModels = await this.readAll(ids, context);
373
+ if (context.get("mergeForUpdate"))
374
+ models = models.map((m, i) => decorator_validation_1.Model.merge(oldModels[i], m, this.class));
375
+ }
156
376
  if (!ignoreHandlers)
157
- await Promise.all(models.map((m, i) => (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.ON, oldModels[i])));
377
+ await Promise.all(models.map((m, i) => (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.ON, oldModels ? oldModels[i] : undefined)));
158
378
  if (!ignoreValidate) {
159
- const modelsValidation = await Promise.all(models.map((m, i) => Promise.resolve(m.hasErrors(oldModels[i]))));
379
+ let modelsValidation;
380
+ if (!context.get("applyUpdateValidation")) {
381
+ modelsValidation = await Promise.resolve(models.map((m) => m.hasErrors()));
382
+ }
383
+ else {
384
+ modelsValidation = await Promise.all(models.map((m, i) => Promise.resolve(m.hasErrors(oldModels[i]))));
385
+ }
160
386
  const errors = (0, utils_1.reduceErrorsToPrint)(modelsValidation);
161
387
  if (errors)
162
388
  throw new errors_1.ValidationError(errors);
163
389
  }
164
- return [models, ...contextArgs.args];
390
+ return [models, ...contextArgs.args, oldModels];
165
391
  }
166
- async readPrefix(key, ...args) {
167
- return super.readPrefix(key, ...args);
392
+ /**
393
+ * @description Processes multiple models after update and executes post-update operations.
394
+ * @summary Finalizes multiple models after they have been updated in the data store. This includes
395
+ * enforcing any decorators that should be applied after updating for each model.
396
+ * @param {M[]} models - The array of model instances that were updated
397
+ * @param {C} context - The context for the operation
398
+ * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
399
+ */
400
+ async updateAllSuffix(models, oldModels, context) {
401
+ if (context.get("applyUpdateValidation") &&
402
+ !context.get("ignoreDevSafeGuards")) {
403
+ if (!oldModels)
404
+ throw new errors_1.InternalError("No previous versions of models provided");
405
+ }
406
+ await Promise.all(models.map((m, i) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.UPDATE, constants_1.OperationKeys.AFTER, oldModels ? oldModels[i] : undefined)));
407
+ return models;
168
408
  }
169
- async readAllPrefix(keys, ...args) {
170
- return super.readAllPrefix(keys, ...args);
409
+ /**
410
+ * @description Deletes multiple model instances from the repository by their primary keys.
411
+ * @summary Removes multiple model instances from the underlying data store using their primary keys
412
+ * by calling the delete method for each key in the array.
413
+ * @param {string[] | number[]} keys - The array of primary keys of the models to delete
414
+ * @param {any[]} args - Additional arguments for the delete operation
415
+ * @return {Promise<M[]>} A promise that resolves to an array of deleted model instances
416
+ */
417
+ async deleteAll(keys, ...args) {
418
+ return Promise.all(keys.map((k) => this.delete(k, ...args)));
419
+ }
420
+ /**
421
+ * @description Processes a model after deletion and executes post-delete operations.
422
+ * @summary Finalizes a model after it has been deleted from the data store. This includes
423
+ * enforcing any decorators that should be applied after deletion.
424
+ * @param {M} model - The model instance that was deleted
425
+ * @param {C} context - The context for the operation
426
+ * @return {Promise<M>} A promise that resolves to the processed model instance
427
+ */
428
+ async deleteSuffix(model, context) {
429
+ await (0, utils_1.enforceDBDecorators)(this, context, model, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.AFTER);
430
+ return model;
431
+ }
432
+ /**
433
+ * @description Prepares for deleting a model and executes pre-delete operations.
434
+ * @summary Processes a key before a model is deleted from the data store. This includes
435
+ * creating a context, retrieving the model to be deleted, and enforcing any decorators
436
+ * that should be applied before deletion.
437
+ * @param {any} key - The primary key of the model to delete
438
+ * @param {any[]} args - Additional arguments for the delete operation
439
+ * @return A promise that resolves to an array containing the key and context arguments
440
+ */
441
+ async deletePrefix(key, ...args) {
442
+ const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.DELETE, this.class, args);
443
+ const model = await this.read(key, ...contextArgs.args);
444
+ await (0, utils_1.enforceDBDecorators)(this, contextArgs.context, model, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.ON);
445
+ return [key, ...contextArgs.args];
171
446
  }
447
+ /**
448
+ * @description Prepares for deleting multiple models and executes pre-delete operations.
449
+ * @summary Processes multiple keys before models are deleted from the data store. This includes
450
+ * creating a context, retrieving the models to be deleted, and enforcing any decorators
451
+ * that should be applied before deletion for each model.
452
+ * @param {string[] | number[]} keys - The array of primary keys of the models to delete
453
+ * @param {any[]} args - Additional arguments for the delete operation
454
+ * @return A promise that resolves to an array containing the keys and context arguments
455
+ */
172
456
  async deleteAllPrefix(keys, ...args) {
173
- return super.deleteAllPrefix(keys, ...args);
457
+ const contextArgs = await Context_1.Context.args(constants_1.OperationKeys.DELETE, this.class, args);
458
+ const models = await this.readAll(keys, ...contextArgs.args);
459
+ await Promise.all(models.map(async (m) => {
460
+ return (0, utils_1.enforceDBDecorators)(this, contextArgs.context, m, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.ON);
461
+ }));
462
+ return [keys, ...contextArgs.args];
463
+ }
464
+ /**
465
+ * @description Processes multiple models after deletion and executes post-delete operations.
466
+ * @summary Finalizes multiple models after they have been deleted from the data store. This includes
467
+ * enforcing any decorators that should be applied after deletion for each model.
468
+ * @param {M[]} models - The array of model instances that were deleted
469
+ * @param {C} context - The context for the operation
470
+ * @return {Promise<M[]>} A promise that resolves to the array of processed model instances
471
+ */
472
+ async deleteAllSuffix(models, context) {
473
+ await Promise.all(models.map((m) => (0, utils_1.enforceDBDecorators)(this, context, m, constants_1.OperationKeys.DELETE, constants_1.OperationKeys.AFTER)));
474
+ return models;
475
+ }
476
+ /**
477
+ * @description Returns a string representation of the repository.
478
+ * @summary Creates a string that identifies this repository by the name of its model class.
479
+ * @return {string} A string representation of the repository
480
+ */
481
+ toString() {
482
+ return `${this.class.name} Repository`;
174
483
  }
175
484
  }
176
485
  exports.Repository = Repository;