@decaf-ts/core 0.7.2 → 0.7.4

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 (246) hide show
  1. package/README.md +6 -1
  2. package/dist/core.cjs +1 -1
  3. package/dist/core.cjs.map +1 -1
  4. package/dist/core.js +1 -1
  5. package/dist/core.js.map +1 -1
  6. package/lib/esm/identity/decorators.d.ts +3 -4
  7. package/lib/esm/identity/decorators.js +5 -11
  8. package/lib/esm/identity/decorators.js.map +1 -1
  9. package/lib/esm/identity/index.d.ts +0 -1
  10. package/lib/esm/identity/index.js +0 -1
  11. package/lib/esm/identity/index.js.map +1 -1
  12. package/lib/esm/index.d.ts +1 -1
  13. package/lib/esm/index.js +1 -1
  14. package/lib/esm/interfaces/ContextuallyLogged.d.ts +8 -0
  15. package/lib/esm/interfaces/ContextuallyLogged.js +2 -0
  16. package/lib/esm/interfaces/ContextuallyLogged.js.map +1 -0
  17. package/lib/esm/interfaces/ErrorParser.d.ts +2 -2
  18. package/lib/esm/interfaces/Executor.d.ts +1 -1
  19. package/lib/esm/interfaces/Observable.d.ts +8 -8
  20. package/lib/esm/interfaces/Observer.d.ts +2 -2
  21. package/lib/esm/interfaces/RawExecutor.d.ts +1 -1
  22. package/lib/esm/model/BaseModel.d.ts +2 -2
  23. package/lib/esm/model/BaseModel.js +2 -2
  24. package/lib/esm/{ram/model/RamSequenceModel.d.ts → model/SequenceModel.d.ts} +3 -3
  25. package/lib/esm/{ram/model/RamSequenceModel.js → model/SequenceModel.js} +12 -11
  26. package/lib/esm/model/SequenceModel.js.map +1 -0
  27. package/lib/esm/model/construction.d.ts +9 -8
  28. package/lib/esm/model/construction.js +2 -2
  29. package/lib/esm/model/construction.js.map +1 -1
  30. package/lib/esm/model/decorators.d.ts +5 -4
  31. package/lib/esm/model/decorators.js +3 -3
  32. package/lib/esm/model/decorators.js.map +1 -1
  33. package/lib/esm/model/index.d.ts +1 -0
  34. package/lib/esm/model/index.js +1 -0
  35. package/lib/esm/model/index.js.map +1 -1
  36. package/lib/esm/overrides/Metadata.d.ts +1 -8
  37. package/lib/esm/overrides/Model.d.ts +75 -0
  38. package/lib/esm/overrides/Model.js +2 -0
  39. package/lib/esm/overrides/Model.js.map +1 -0
  40. package/lib/esm/overrides/index.d.ts +2 -0
  41. package/lib/esm/overrides/index.js +2 -0
  42. package/lib/esm/overrides/index.js.map +1 -1
  43. package/lib/esm/overrides/injectables.d.ts +10 -0
  44. package/lib/esm/overrides/injectables.js +2 -0
  45. package/lib/esm/overrides/injectables.js.map +1 -0
  46. package/lib/esm/overrides/overrides.js +45 -3
  47. package/lib/esm/overrides/overrides.js.map +1 -1
  48. package/lib/esm/persistence/Adapter.d.ts +55 -53
  49. package/lib/esm/persistence/Adapter.js +85 -61
  50. package/lib/esm/persistence/Adapter.js.map +1 -1
  51. package/lib/esm/persistence/Dispatch.d.ts +7 -6
  52. package/lib/esm/persistence/Dispatch.js +25 -18
  53. package/lib/esm/persistence/Dispatch.js.map +1 -1
  54. package/lib/esm/persistence/ObserverHandler.d.ts +7 -6
  55. package/lib/esm/persistence/ObserverHandler.js +5 -4
  56. package/lib/esm/persistence/ObserverHandler.js.map +1 -1
  57. package/lib/esm/persistence/Sequence.d.ts +34 -25
  58. package/lib/esm/persistence/Sequence.js +136 -15
  59. package/lib/esm/persistence/Sequence.js.map +1 -1
  60. package/lib/esm/persistence/constants.d.ts +1 -0
  61. package/lib/esm/persistence/constants.js +1 -0
  62. package/lib/esm/persistence/constants.js.map +1 -1
  63. package/lib/esm/persistence/generators.d.ts +14 -0
  64. package/lib/esm/persistence/generators.js +31 -0
  65. package/lib/esm/persistence/generators.js.map +1 -0
  66. package/lib/esm/persistence/index.d.ts +1 -0
  67. package/lib/esm/persistence/index.js +1 -0
  68. package/lib/esm/persistence/index.js.map +1 -1
  69. package/lib/esm/persistence/migrations.d.ts +8 -8
  70. package/lib/esm/persistence/migrations.js +8 -9
  71. package/lib/esm/persistence/migrations.js.map +1 -1
  72. package/lib/esm/persistence/types.d.ts +38 -20
  73. package/lib/esm/query/Paginator.d.ts +6 -5
  74. package/lib/esm/query/Paginator.js +4 -4
  75. package/lib/esm/query/Paginator.js.map +1 -1
  76. package/lib/esm/query/Statement.d.ts +9 -8
  77. package/lib/esm/query/Statement.js +23 -10
  78. package/lib/esm/query/Statement.js.map +1 -1
  79. package/lib/esm/ram/RamAdapter.d.ts +22 -31
  80. package/lib/esm/ram/RamAdapter.js +36 -34
  81. package/lib/esm/ram/RamAdapter.js.map +1 -1
  82. package/lib/esm/ram/RamPaginator.d.ts +2 -1
  83. package/lib/esm/ram/RamPaginator.js +5 -3
  84. package/lib/esm/ram/RamPaginator.js.map +1 -1
  85. package/lib/esm/ram/RamStatement.d.ts +7 -7
  86. package/lib/esm/ram/RamStatement.js.map +1 -1
  87. package/lib/esm/ram/handlers.d.ts +3 -4
  88. package/lib/esm/ram/handlers.js.map +1 -1
  89. package/lib/esm/ram/index.d.ts +0 -3
  90. package/lib/esm/ram/index.js +0 -3
  91. package/lib/esm/ram/index.js.map +1 -1
  92. package/lib/esm/ram/types.d.ts +4 -12
  93. package/lib/esm/repository/Repository.d.ts +31 -95
  94. package/lib/esm/repository/Repository.js +107 -203
  95. package/lib/esm/repository/Repository.js.map +1 -1
  96. package/lib/esm/repository/decorators.js +4 -3
  97. package/lib/esm/repository/decorators.js.map +1 -1
  98. package/lib/esm/repository/utils.d.ts +1 -1
  99. package/lib/esm/repository/utils.js +2 -3
  100. package/lib/esm/repository/utils.js.map +1 -1
  101. package/lib/esm/utils/ContextualLoggedClass.d.ts +16 -0
  102. package/lib/esm/utils/ContextualLoggedClass.js +29 -0
  103. package/lib/esm/utils/ContextualLoggedClass.js.map +1 -0
  104. package/lib/esm/utils/Services.d.ts +50 -0
  105. package/lib/esm/utils/Services.js +154 -0
  106. package/lib/esm/utils/Services.js.map +1 -0
  107. package/lib/esm/utils/decorators.d.ts +1 -9
  108. package/lib/esm/utils/decorators.js +26 -15
  109. package/lib/esm/utils/decorators.js.map +1 -1
  110. package/lib/esm/utils/index.d.ts +2 -1
  111. package/lib/esm/utils/index.js +2 -1
  112. package/lib/esm/utils/index.js.map +1 -1
  113. package/lib/identity/decorators.cjs +5 -11
  114. package/lib/identity/decorators.d.ts +3 -4
  115. package/lib/identity/decorators.js.map +1 -1
  116. package/lib/identity/index.cjs +0 -1
  117. package/lib/identity/index.d.ts +0 -1
  118. package/lib/identity/index.js.map +1 -1
  119. package/lib/index.cjs +1 -1
  120. package/lib/index.d.ts +1 -1
  121. package/lib/interfaces/ContextuallyLogged.cjs +3 -0
  122. package/lib/interfaces/ContextuallyLogged.d.ts +8 -0
  123. package/lib/interfaces/ContextuallyLogged.js.map +1 -0
  124. package/lib/interfaces/ErrorParser.d.ts +2 -2
  125. package/lib/interfaces/Executor.d.ts +1 -1
  126. package/lib/interfaces/Observable.d.ts +8 -8
  127. package/lib/interfaces/Observer.d.ts +2 -2
  128. package/lib/interfaces/RawExecutor.d.ts +1 -1
  129. package/lib/model/BaseModel.cjs +2 -2
  130. package/lib/model/BaseModel.d.ts +2 -2
  131. package/lib/{ram/model/RamSequenceModel.cjs → model/SequenceModel.cjs} +14 -13
  132. package/lib/{ram/model/RamSequenceModel.d.ts → model/SequenceModel.d.ts} +3 -3
  133. package/lib/model/SequenceModel.js.map +1 -0
  134. package/lib/model/construction.cjs +2 -2
  135. package/lib/model/construction.d.ts +9 -8
  136. package/lib/model/construction.js.map +1 -1
  137. package/lib/model/decorators.cjs +3 -3
  138. package/lib/model/decorators.d.ts +5 -4
  139. package/lib/model/decorators.js.map +1 -1
  140. package/lib/model/index.cjs +1 -0
  141. package/lib/model/index.d.ts +1 -0
  142. package/lib/model/index.js.map +1 -1
  143. package/lib/overrides/Metadata.d.ts +1 -8
  144. package/lib/overrides/Model.cjs +4 -0
  145. package/lib/overrides/Model.d.ts +75 -0
  146. package/lib/overrides/Model.js.map +1 -0
  147. package/lib/overrides/index.cjs +2 -0
  148. package/lib/overrides/index.d.ts +2 -0
  149. package/lib/overrides/index.js.map +1 -1
  150. package/lib/overrides/injectables.cjs +4 -0
  151. package/lib/overrides/injectables.d.ts +10 -0
  152. package/lib/overrides/injectables.js.map +1 -0
  153. package/lib/overrides/overrides.cjs +43 -1
  154. package/lib/overrides/overrides.js.map +1 -1
  155. package/lib/persistence/Adapter.cjs +90 -66
  156. package/lib/persistence/Adapter.d.ts +55 -53
  157. package/lib/persistence/Adapter.js.map +1 -1
  158. package/lib/persistence/Dispatch.cjs +25 -18
  159. package/lib/persistence/Dispatch.d.ts +7 -6
  160. package/lib/persistence/Dispatch.js.map +1 -1
  161. package/lib/persistence/ObserverHandler.cjs +5 -4
  162. package/lib/persistence/ObserverHandler.d.ts +7 -6
  163. package/lib/persistence/ObserverHandler.js.map +1 -1
  164. package/lib/persistence/Sequence.cjs +136 -15
  165. package/lib/persistence/Sequence.d.ts +34 -25
  166. package/lib/persistence/Sequence.js.map +1 -1
  167. package/lib/persistence/constants.cjs +1 -0
  168. package/lib/persistence/constants.d.ts +1 -0
  169. package/lib/persistence/constants.js.map +1 -1
  170. package/lib/persistence/generators.cjs +36 -0
  171. package/lib/persistence/generators.d.ts +14 -0
  172. package/lib/persistence/generators.js.map +1 -0
  173. package/lib/persistence/index.cjs +1 -0
  174. package/lib/persistence/index.d.ts +1 -0
  175. package/lib/persistence/index.js.map +1 -1
  176. package/lib/persistence/migrations.cjs +7 -8
  177. package/lib/persistence/migrations.d.ts +8 -8
  178. package/lib/persistence/migrations.js.map +1 -1
  179. package/lib/persistence/types.d.ts +38 -20
  180. package/lib/query/Paginator.cjs +4 -4
  181. package/lib/query/Paginator.d.ts +6 -5
  182. package/lib/query/Paginator.js.map +1 -1
  183. package/lib/query/Statement.cjs +34 -21
  184. package/lib/query/Statement.d.ts +9 -8
  185. package/lib/query/Statement.js.map +1 -1
  186. package/lib/ram/RamAdapter.cjs +35 -33
  187. package/lib/ram/RamAdapter.d.ts +22 -31
  188. package/lib/ram/RamAdapter.js.map +1 -1
  189. package/lib/ram/RamPaginator.cjs +5 -3
  190. package/lib/ram/RamPaginator.d.ts +2 -1
  191. package/lib/ram/RamPaginator.js.map +1 -1
  192. package/lib/ram/RamStatement.d.ts +7 -7
  193. package/lib/ram/RamStatement.js.map +1 -1
  194. package/lib/ram/handlers.d.ts +3 -4
  195. package/lib/ram/handlers.js.map +1 -1
  196. package/lib/ram/index.cjs +0 -3
  197. package/lib/ram/index.d.ts +0 -3
  198. package/lib/ram/index.js.map +1 -1
  199. package/lib/ram/types.d.ts +4 -12
  200. package/lib/repository/Repository.cjs +108 -204
  201. package/lib/repository/Repository.d.ts +31 -95
  202. package/lib/repository/Repository.js.map +1 -1
  203. package/lib/repository/decorators.cjs +2 -1
  204. package/lib/repository/decorators.js.map +1 -1
  205. package/lib/repository/utils.cjs +2 -3
  206. package/lib/repository/utils.d.ts +1 -1
  207. package/lib/repository/utils.js.map +1 -1
  208. package/lib/utils/ContextualLoggedClass.cjs +33 -0
  209. package/lib/utils/ContextualLoggedClass.d.ts +16 -0
  210. package/lib/utils/ContextualLoggedClass.js.map +1 -0
  211. package/lib/utils/Services.cjs +159 -0
  212. package/lib/utils/Services.d.ts +50 -0
  213. package/lib/utils/Services.js.map +1 -0
  214. package/lib/utils/decorators.cjs +27 -16
  215. package/lib/utils/decorators.d.ts +1 -9
  216. package/lib/utils/decorators.js.map +1 -1
  217. package/lib/utils/index.cjs +2 -1
  218. package/lib/utils/index.d.ts +2 -1
  219. package/lib/utils/index.js.map +1 -1
  220. package/package.json +1 -1
  221. package/lib/esm/identity/utils.d.ts +0 -24
  222. package/lib/esm/identity/utils.js +0 -44
  223. package/lib/esm/identity/utils.js.map +0 -1
  224. package/lib/esm/ram/RamContext.d.ts +0 -28
  225. package/lib/esm/ram/RamContext.js +0 -30
  226. package/lib/esm/ram/RamContext.js.map +0 -1
  227. package/lib/esm/ram/RamSequence.d.ts +0 -75
  228. package/lib/esm/ram/RamSequence.js +0 -145
  229. package/lib/esm/ram/RamSequence.js.map +0 -1
  230. package/lib/esm/ram/model/RamSequenceModel.js.map +0 -1
  231. package/lib/esm/ram/model/index.d.ts +0 -1
  232. package/lib/esm/ram/model/index.js +0 -2
  233. package/lib/esm/ram/model/index.js.map +0 -1
  234. package/lib/identity/utils.cjs +0 -49
  235. package/lib/identity/utils.d.ts +0 -24
  236. package/lib/identity/utils.js.map +0 -1
  237. package/lib/ram/RamContext.cjs +0 -34
  238. package/lib/ram/RamContext.d.ts +0 -28
  239. package/lib/ram/RamContext.js.map +0 -1
  240. package/lib/ram/RamSequence.cjs +0 -149
  241. package/lib/ram/RamSequence.d.ts +0 -75
  242. package/lib/ram/RamSequence.js.map +0 -1
  243. package/lib/ram/model/RamSequenceModel.js.map +0 -1
  244. package/lib/ram/model/index.cjs +0 -18
  245. package/lib/ram/model/index.d.ts +0 -1
  246. package/lib/ram/model/index.js.map +0 -1
@@ -13,12 +13,12 @@ exports.Adapter = void 0;
13
13
  const db_decorators_1 = require("@decaf-ts/db-decorators");
14
14
  const decorator_validation_1 = require("@decaf-ts/decorator-validation");
15
15
  const constants_1 = require("./constants.cjs");
16
- const utils_1 = require("./../utils/index.cjs");
17
- const ObserverHandler_1 = require("./ObserverHandler.cjs");
18
16
  const logging_1 = require("@decaf-ts/logging");
19
- const utils_2 = require("./../identity/utils.cjs");
17
+ const ObserverHandler_1 = require("./ObserverHandler.cjs");
18
+ const logging_2 = require("@decaf-ts/logging");
20
19
  const decoration_1 = require("@decaf-ts/decoration");
21
20
  const errors_1 = require("./errors.cjs");
21
+ const ContextualLoggedClass_1 = require("./../utils/ContextualLoggedClass.cjs");
22
22
  const flavourResolver = decoration_1.Decoration["flavourResolver"].bind(decoration_1.Decoration);
23
23
  decoration_1.Decoration["flavourResolver"] = (obj) => {
24
24
  try {
@@ -138,7 +138,7 @@ decoration_1.Decoration["flavourResolver"] = (obj) => {
138
138
  * Adapter --|> Observer
139
139
  * Adapter --|> ErrorParser
140
140
  */
141
- class Adapter extends logging_1.LoggedClass {
141
+ class Adapter extends ContextualLoggedClass_1.ContextualLoggedClass {
142
142
  static { this._cache = {}; }
143
143
  /**
144
144
  * @description Gets the native persistence config
@@ -161,7 +161,7 @@ class Adapter extends logging_1.LoggedClass {
161
161
  * @description Gets the repository constructor for this adapter
162
162
  * @summary Returns the constructor for creating repositories that work with this adapter
163
163
  * @template M - The model type
164
- * @return {Constructor<Repository<M, QUERY, Adapter<CONF, CONN, QUERY, FLAGS, CONTEXT>, FLAGS, CONTEXT>>} The repository constructor
164
+ * @return {Constructor<Repository<any, Adapter<CONF, CONN, QUERY, CONTEXT>>>} The repository constructor
165
165
  */
166
166
  repository() {
167
167
  if (!Adapter._baseRepository)
@@ -262,6 +262,15 @@ class Adapter extends logging_1.LoggedClass {
262
262
  */
263
263
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
264
264
  async initialize(...args) { }
265
+ /**
266
+ * @description Creates a sequence generator
267
+ * @summary Factory method that creates a sequence generator for generating sequential values
268
+ * @param {SequenceOptions} options - Configuration options for the sequence
269
+ * @return {Promise<Sequence>} A promise that resolves to a new sequence instance
270
+ */
271
+ async Sequence(options) {
272
+ return new Adapter._baseSequence(options, this);
273
+ }
265
274
  /**
266
275
  * @description Creates repository flags for an operation
267
276
  * @summary Generates a set of flags that describe a database operation, combining default flags with overrides
@@ -276,12 +285,18 @@ class Adapter extends logging_1.LoggedClass {
276
285
  async flags(operation, model, flags,
277
286
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
278
287
  ...args) {
288
+ let log = (flags.logger || logging_2.Logging.for(this.toString()));
289
+ if (flags.correlationId)
290
+ log = log.for({ correlationId: flags.correlationId });
279
291
  return Object.assign({}, db_decorators_1.DefaultRepositoryFlags, flags, {
280
- affectedTables: (0, utils_2.getTableName)(model),
292
+ affectedTables: (Array.isArray(model) ? model : [model]).map(decorator_validation_1.Model.tableName),
281
293
  writeOperation: operation !== db_decorators_1.OperationKeys.READ,
282
294
  timestamp: new Date(),
283
295
  operation: operation,
284
- ignoredValidationProperties: decoration_1.Metadata.validationExceptions(model, operation),
296
+ ignoredValidationProperties: Array.isArray(model)
297
+ ? []
298
+ : decoration_1.Metadata.validationExceptions(model, operation),
299
+ logger: log,
285
300
  });
286
301
  }
287
302
  /**
@@ -297,7 +312,7 @@ class Adapter extends logging_1.LoggedClass {
297
312
  */
298
313
  async context(operation, overrides, model, ...args) {
299
314
  const log = this.log.for(this.context);
300
- log.debug(`Creating new context for ${operation} operation on ${model.name} model with flag overrides: ${JSON.stringify(overrides)}`);
315
+ log.debug(`Creating new context for ${operation} operation on ${Array.isArray(model) ? model.map((m) => m.name) : model.name} model with flag overrides: ${JSON.stringify(overrides)}`);
301
316
  const flags = await this.flags(operation, model, overrides, ...args);
302
317
  return new this.Context().accumulate(flags);
303
318
  }
@@ -305,38 +320,37 @@ class Adapter extends logging_1.LoggedClass {
305
320
  * @description Prepares a model for persistence
306
321
  * @summary Converts a model instance into a format suitable for database storage,
307
322
  * handling column mapping and separating transient properties
323
+ * handling column mapping and separating transient properties
308
324
  * @template M - The model type
309
325
  * @param {M} model - The model instance to prepare
310
- * @param pk - The primary key property name
311
326
  * @param args - optional args for subclassing purposes
312
327
  * @return The prepared data
313
328
  */
314
- prepare(model, pk,
315
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
316
- ...args) {
317
- const log = this.log.for(this.prepare);
329
+ prepare(model, ...args) {
330
+ const { log } = this.logCtx(args, this.prepare);
318
331
  const split = model.segregate();
319
332
  const result = Object.entries(split.model).reduce((accum, [key, val]) => {
320
333
  if (typeof val === "undefined")
321
334
  return accum;
322
- const mappedProp = (0, utils_2.getColumnName)(model, key);
335
+ const mappedProp = decorator_validation_1.Model.columnName(model.constructor, key);
323
336
  if (this.isReserved(mappedProp))
324
337
  throw new db_decorators_1.InternalError(`Property name ${mappedProp} is reserved`);
325
338
  accum[mappedProp] = val;
326
339
  return accum;
327
340
  }, {});
328
341
  if (model[constants_1.PersistenceKeys.METADATA]) {
342
+ // TODO movo to couchdb
329
343
  log.silly(`Passing along persistence metadata for ${model[constants_1.PersistenceKeys.METADATA]}`);
330
344
  Object.defineProperty(result, constants_1.PersistenceKeys.METADATA, {
331
345
  enumerable: false,
332
- writable: false,
346
+ writable: true,
333
347
  configurable: true,
334
348
  value: model[constants_1.PersistenceKeys.METADATA],
335
349
  });
336
350
  }
337
351
  return {
338
352
  record: result,
339
- id: model[pk],
353
+ id: model[decorator_validation_1.Model.pk(model.constructor)],
340
354
  transient: split.transient,
341
355
  };
342
356
  }
@@ -346,29 +360,29 @@ class Adapter extends logging_1.LoggedClass {
346
360
  * and reattaching transient properties
347
361
  * @template M - The model type
348
362
  * @param obj - The database record
349
- * @param {string|Constructor<M>} clazz - The model class or name
363
+ * @param {Constructor<M>} clazz - The model class or name
350
364
  * @param pk - The primary key property name
351
365
  * @param {string|number|bigint} id - The primary key value
352
366
  * @param [transient] - Transient properties to reattach
353
367
  * @param [args] - options args for subclassing purposes
354
368
  * @return {M} The reconstructed model instance
355
369
  */
356
- revert(obj, clazz, pk, id, transient,
357
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
358
- ...args) {
359
- const log = this.log.for(this.revert);
370
+ revert(obj, clazz, id, transient, ...args) {
371
+ const { log, ctx } = this.logCtx(args, this.revert);
360
372
  const ob = {};
373
+ const pk = decorator_validation_1.Model.pk(clazz);
361
374
  ob[pk] = id;
362
- const m = (typeof clazz === "string" ? decorator_validation_1.Model.build(ob, clazz) : new clazz(ob));
375
+ const m = new clazz(ob);
363
376
  log.silly(`Rebuilding model ${m.constructor.name} id ${id}`);
364
- const metadata = obj[constants_1.PersistenceKeys.METADATA];
377
+ const metadata = obj[constants_1.PersistenceKeys.METADATA]; // TODO move to couchdb
365
378
  const result = Object.keys(m).reduce((accum, key) => {
366
379
  if (key === pk)
367
380
  return accum;
368
- accum[key] = obj[(0, utils_2.getColumnName)(accum, key)];
381
+ accum[key] =
382
+ obj[decorator_validation_1.Model.columnName(clazz, key)];
369
383
  return accum;
370
384
  }, m);
371
- if (transient) {
385
+ if (ctx.get("rebuildWithTransient") && transient) {
372
386
  log.verbose(`re-adding transient properties: ${Object.keys(transient).join(", ")}`);
373
387
  Object.entries(transient).forEach(([key, val]) => {
374
388
  if (key in result)
@@ -377,11 +391,12 @@ class Adapter extends logging_1.LoggedClass {
377
391
  });
378
392
  }
379
393
  if (metadata) {
394
+ // TODO move to couchdb
380
395
  log.silly(`Passing along ${this.flavour} persistence metadata for ${m.constructor.name} id ${id}: ${metadata}`);
381
396
  Object.defineProperty(result, constants_1.PersistenceKeys.METADATA, {
382
397
  enumerable: false,
383
- configurable: false,
384
- writable: false,
398
+ configurable: true,
399
+ writable: true,
385
400
  value: metadata,
386
401
  });
387
402
  }
@@ -396,13 +411,13 @@ class Adapter extends logging_1.LoggedClass {
396
411
  * @param {...any[]} args - Additional arguments specific to the adapter implementation
397
412
  * @return A promise that resolves to an array of created records
398
413
  */
399
- async createAll(tableName, id, model, ...args) {
414
+ async createAll(clazz, id, model, ...args) {
400
415
  if (id.length !== model.length)
401
416
  throw new db_decorators_1.InternalError("Ids and models must have the same length");
402
- const log = this.log.for(this.createAll);
403
- log.verbose(`Creating ${id.length} entries ${tableName} table`);
404
- log.debug(`pks: ${id}`);
405
- return Promise.all(id.map((i, count) => this.create(tableName, i, model[count], ...args)));
417
+ const { log, ctxArgs } = this.logCtx(args, this.createAll);
418
+ const tableLabel = decorator_validation_1.Model.tableName(clazz);
419
+ log.debug(`Creating ${id.length} entries ${tableLabel} table`);
420
+ return Promise.all(id.map((i, count) => this.create(clazz, i, model[count], ...ctxArgs)));
406
421
  }
407
422
  /**
408
423
  * @description Retrieves multiple records from the database
@@ -412,28 +427,28 @@ class Adapter extends logging_1.LoggedClass {
412
427
  * @param {...any[]} args - Additional arguments specific to the adapter implementation
413
428
  * @return A promise that resolves to an array of retrieved records
414
429
  */
415
- async readAll(tableName, id, ...args) {
416
- const log = this.log.for(this.readAll);
417
- log.verbose(`Reading ${id.length} entries ${tableName} table`);
418
- log.debug(`pks: ${id}`);
419
- return Promise.all(id.map((i) => this.read(tableName, i, ...args)));
430
+ async readAll(clazz, id, ...args) {
431
+ const { log, ctxArgs } = this.logCtx(args, this.readAll);
432
+ const tableName = decorator_validation_1.Model.tableName(clazz);
433
+ log.debug(`Reading ${id.length} entries ${tableName} table`);
434
+ return Promise.all(id.map((i) => this.read(clazz, i, ...ctxArgs)));
420
435
  }
421
436
  /**
422
437
  * @description Updates multiple records in the database
423
438
  * @summary Modifies multiple existing records with the given IDs in the specified table
424
- * @param {string} tableName - The name of the table to update
439
+ * @param {Constructor<M>} tableName - The name of the table to update
425
440
  * @param {string[]|number[]} id - The identifiers of the records to update
426
441
  * @param model - The new data for each record
427
442
  * @param {...any[]} args - Additional arguments specific to the adapter implementation
428
443
  * @return A promise that resolves to an array of updated records
429
444
  */
430
- async updateAll(tableName, id, model, ...args) {
445
+ async updateAll(clazz, id, model, ...args) {
431
446
  if (id.length !== model.length)
432
447
  throw new db_decorators_1.InternalError("Ids and models must have the same length");
433
- const log = this.log.for(this.updateAll);
434
- log.verbose(`Updating ${id.length} entries ${tableName} table`);
435
- log.debug(`pks: ${id}`);
436
- return Promise.all(id.map((i, count) => this.update(tableName, i, model[count], ...args)));
448
+ const { log, ctxArgs } = this.logCtx(args, this.updateAll);
449
+ const tableLabel = decorator_validation_1.Model.tableName(clazz);
450
+ log.debug(`Updating ${id.length} entries ${tableLabel} table`);
451
+ return Promise.all(id.map((i, count) => this.update(clazz, i, model[count], ...ctxArgs)));
437
452
  }
438
453
  /**
439
454
  * @description Deletes multiple records from the database
@@ -444,10 +459,9 @@ class Adapter extends logging_1.LoggedClass {
444
459
  * @return A promise that resolves to an array of deleted records
445
460
  */
446
461
  async deleteAll(tableName, id, ...args) {
447
- const log = this.log.for(this.createAll);
448
- log.verbose(`Deleting ${id.length} entries ${tableName} table`);
449
- log.debug(`pks: ${id}`);
450
- return Promise.all(id.map((i) => this.delete(tableName, i, ...args)));
462
+ const { log, ctxArgs } = Adapter.logCtx(args, this.deleteAll);
463
+ log.verbose(`Deleting ${id.length} entries from ${tableName} table`);
464
+ return Promise.all(id.map((i) => this.delete(tableName, i, ...ctxArgs)));
451
465
  }
452
466
  /**
453
467
  * @description Registers an observer for database events
@@ -464,11 +478,10 @@ class Adapter extends logging_1.LoggedClass {
464
478
  writable: false,
465
479
  });
466
480
  this.observerHandler.observe(observer, filter);
467
- this.log
468
- .for(this.observe)
469
- .verbose(`Registering new observer ${observer.toString()}`);
481
+ const log = this.log.for(this.observe);
482
+ log.verbose(`Registering new observer ${observer.toString()}`);
470
483
  if (!this.dispatch) {
471
- this.log.for(this.observe).info(`Creating dispatch for ${this.alias}`);
484
+ log.info(`Creating dispatch for ${this.alias}`);
472
485
  this.dispatch = this.Dispatch();
473
486
  this.dispatch.observe(this);
474
487
  }
@@ -500,9 +513,9 @@ class Adapter extends logging_1.LoggedClass {
500
513
  async updateObservers(table, event, id, ...args) {
501
514
  if (!this.observerHandler)
502
515
  throw new db_decorators_1.InternalError("ObserverHandler not initialized. Did you register any observables?");
503
- const log = this.log.for(this.updateObservers);
504
- log.verbose(`Updating ${this.observerHandler.count()} observers for adapter ${this.alias}`);
505
- await this.observerHandler.updateObservers(this.log, table, event, id, ...args);
516
+ const { log, ctxArgs } = Adapter.logCtx(args, this.updateObservers);
517
+ log.verbose(`Updating ${this.observerHandler.count()} observers for adapter ${this.alias}: Event: `);
518
+ await this.observerHandler.updateObservers(table, event, id, ...ctxArgs);
506
519
  }
507
520
  /**
508
521
  * @description Refreshes data based on a database event
@@ -522,7 +535,7 @@ class Adapter extends logging_1.LoggedClass {
522
535
  * @return {string} A string representation of the adapter
523
536
  */
524
537
  toString() {
525
- return `${this.flavour} persistence Adapter`;
538
+ return `${this.flavour} adapter`;
526
539
  }
527
540
  /**
528
541
  * @description Gets the adapter flavor associated with a model
@@ -553,10 +566,9 @@ class Adapter extends logging_1.LoggedClass {
553
566
  * @template CONF - The database driver config
554
567
  * @template CONN - The database driver instance
555
568
  * @template QUERY - The query type
556
- * @template CCONTEXT - The context type
557
- * @template FLAGS - The repository flags type
569
+ * @template CONTEXT - The context type
558
570
  * @param {string} flavour - The flavor name of the adapter to retrieve
559
- * @return {Adapter<CONF, CONN, QUERY, CONTEXT, FLAGS> | undefined} The adapter instance or undefined if not found
571
+ * @return {Adapter<CONF, CONN, QUERY, CONTEXT> | undefined} The adapter instance or undefined if not found
560
572
  */
561
573
  static get(flavour) {
562
574
  if (!flavour)
@@ -590,6 +602,9 @@ class Adapter extends logging_1.LoggedClass {
590
602
  }
591
603
  }
592
604
  static decoration() { }
605
+ static logCtx(args, method) {
606
+ return super.logCtx(args, method);
607
+ }
593
608
  get client() {
594
609
  if (!this._client) {
595
610
  this._client = this.getClient();
@@ -629,12 +644,21 @@ class Adapter extends logging_1.LoggedClass {
629
644
  migrations() {
630
645
  return decoration_1.Metadata.migrationsFor(this);
631
646
  }
632
- async migrate(migrations = this.migrations()) {
647
+ async getQueryRunner() {
648
+ return this;
649
+ }
650
+ async migrate(migrations = this.migrations(), ...args) {
651
+ if (migrations instanceof db_decorators_1.Context) {
652
+ args = [migrations];
653
+ migrations = this.migrations();
654
+ }
655
+ const { ctx } = Adapter.logCtx(args, this.migrate);
656
+ const qr = await this.getQueryRunner();
633
657
  for (const migration of migrations) {
634
658
  try {
635
659
  const m = new migration();
636
- await m.up(this, this, this.log);
637
- await m.down(this, this, this.log);
660
+ await m.up(qr, this, ctx);
661
+ await m.down(qr, this, ctx);
638
662
  }
639
663
  catch (e) {
640
664
  throw new errors_1.MigrationError(e);
@@ -644,31 +668,31 @@ class Adapter extends logging_1.LoggedClass {
644
668
  }
645
669
  exports.Adapter = Adapter;
646
670
  __decorate([
647
- (0, utils_1.final)(),
671
+ (0, logging_1.final)(),
648
672
  __metadata("design:type", Function),
649
673
  __metadata("design:paramtypes", [String]),
650
674
  __metadata("design:returntype", Promise)
651
675
  ], Adapter.prototype, "shutdownProxies", null);
652
676
  __decorate([
653
- (0, utils_1.final)(),
677
+ (0, logging_1.final)(),
654
678
  __metadata("design:type", Function),
655
679
  __metadata("design:paramtypes", [String, Object, Object, Object]),
656
680
  __metadata("design:returntype", Promise)
657
681
  ], Adapter.prototype, "context", null);
658
682
  __decorate([
659
- (0, utils_1.final)(),
683
+ (0, logging_1.final)(),
660
684
  __metadata("design:type", Function),
661
685
  __metadata("design:paramtypes", [Object, Function]),
662
686
  __metadata("design:returntype", void 0)
663
687
  ], Adapter.prototype, "observe", null);
664
688
  __decorate([
665
- (0, utils_1.final)(),
689
+ (0, logging_1.final)(),
666
690
  __metadata("design:type", Function),
667
691
  __metadata("design:paramtypes", [Object]),
668
692
  __metadata("design:returntype", void 0)
669
693
  ], Adapter.prototype, "unObserve", null);
670
694
  __decorate([
671
- (0, utils_1.final)(),
695
+ (0, logging_1.final)(),
672
696
  __metadata("design:type", Object),
673
697
  __metadata("design:paramtypes", [])
674
698
  ], Adapter.prototype, "client", null);