@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.
- package/README.md +6 -1
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +1 -1
- package/dist/core.js.map +1 -1
- package/lib/esm/identity/decorators.d.ts +3 -4
- package/lib/esm/identity/decorators.js +5 -11
- package/lib/esm/identity/decorators.js.map +1 -1
- package/lib/esm/identity/index.d.ts +0 -1
- package/lib/esm/identity/index.js +0 -1
- package/lib/esm/identity/index.js.map +1 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/interfaces/ContextuallyLogged.d.ts +8 -0
- package/lib/esm/interfaces/ContextuallyLogged.js +2 -0
- package/lib/esm/interfaces/ContextuallyLogged.js.map +1 -0
- package/lib/esm/interfaces/ErrorParser.d.ts +2 -2
- package/lib/esm/interfaces/Executor.d.ts +1 -1
- package/lib/esm/interfaces/Observable.d.ts +8 -8
- package/lib/esm/interfaces/Observer.d.ts +2 -2
- package/lib/esm/interfaces/RawExecutor.d.ts +1 -1
- package/lib/esm/model/BaseModel.d.ts +2 -2
- package/lib/esm/model/BaseModel.js +2 -2
- package/lib/esm/{ram/model/RamSequenceModel.d.ts → model/SequenceModel.d.ts} +3 -3
- package/lib/esm/{ram/model/RamSequenceModel.js → model/SequenceModel.js} +12 -11
- package/lib/esm/model/SequenceModel.js.map +1 -0
- package/lib/esm/model/construction.d.ts +9 -8
- package/lib/esm/model/construction.js +2 -2
- package/lib/esm/model/construction.js.map +1 -1
- package/lib/esm/model/decorators.d.ts +5 -4
- package/lib/esm/model/decorators.js +3 -3
- package/lib/esm/model/decorators.js.map +1 -1
- package/lib/esm/model/index.d.ts +1 -0
- package/lib/esm/model/index.js +1 -0
- package/lib/esm/model/index.js.map +1 -1
- package/lib/esm/overrides/Metadata.d.ts +1 -8
- package/lib/esm/overrides/Model.d.ts +75 -0
- package/lib/esm/overrides/Model.js +2 -0
- package/lib/esm/overrides/Model.js.map +1 -0
- package/lib/esm/overrides/index.d.ts +2 -0
- package/lib/esm/overrides/index.js +2 -0
- package/lib/esm/overrides/index.js.map +1 -1
- package/lib/esm/overrides/injectables.d.ts +10 -0
- package/lib/esm/overrides/injectables.js +2 -0
- package/lib/esm/overrides/injectables.js.map +1 -0
- package/lib/esm/overrides/overrides.js +45 -3
- package/lib/esm/overrides/overrides.js.map +1 -1
- package/lib/esm/persistence/Adapter.d.ts +55 -53
- package/lib/esm/persistence/Adapter.js +85 -61
- package/lib/esm/persistence/Adapter.js.map +1 -1
- package/lib/esm/persistence/Dispatch.d.ts +7 -6
- package/lib/esm/persistence/Dispatch.js +25 -18
- package/lib/esm/persistence/Dispatch.js.map +1 -1
- package/lib/esm/persistence/ObserverHandler.d.ts +7 -6
- package/lib/esm/persistence/ObserverHandler.js +5 -4
- package/lib/esm/persistence/ObserverHandler.js.map +1 -1
- package/lib/esm/persistence/Sequence.d.ts +34 -25
- package/lib/esm/persistence/Sequence.js +136 -15
- package/lib/esm/persistence/Sequence.js.map +1 -1
- package/lib/esm/persistence/constants.d.ts +1 -0
- package/lib/esm/persistence/constants.js +1 -0
- package/lib/esm/persistence/constants.js.map +1 -1
- package/lib/esm/persistence/generators.d.ts +14 -0
- package/lib/esm/persistence/generators.js +31 -0
- package/lib/esm/persistence/generators.js.map +1 -0
- package/lib/esm/persistence/index.d.ts +1 -0
- package/lib/esm/persistence/index.js +1 -0
- package/lib/esm/persistence/index.js.map +1 -1
- package/lib/esm/persistence/migrations.d.ts +8 -8
- package/lib/esm/persistence/migrations.js +8 -9
- package/lib/esm/persistence/migrations.js.map +1 -1
- package/lib/esm/persistence/types.d.ts +38 -20
- package/lib/esm/query/Paginator.d.ts +6 -5
- package/lib/esm/query/Paginator.js +4 -4
- package/lib/esm/query/Paginator.js.map +1 -1
- package/lib/esm/query/Statement.d.ts +9 -8
- package/lib/esm/query/Statement.js +23 -10
- package/lib/esm/query/Statement.js.map +1 -1
- package/lib/esm/ram/RamAdapter.d.ts +22 -31
- package/lib/esm/ram/RamAdapter.js +36 -34
- package/lib/esm/ram/RamAdapter.js.map +1 -1
- package/lib/esm/ram/RamPaginator.d.ts +2 -1
- package/lib/esm/ram/RamPaginator.js +5 -3
- package/lib/esm/ram/RamPaginator.js.map +1 -1
- package/lib/esm/ram/RamStatement.d.ts +7 -7
- package/lib/esm/ram/RamStatement.js.map +1 -1
- package/lib/esm/ram/handlers.d.ts +3 -4
- package/lib/esm/ram/handlers.js.map +1 -1
- package/lib/esm/ram/index.d.ts +0 -3
- package/lib/esm/ram/index.js +0 -3
- package/lib/esm/ram/index.js.map +1 -1
- package/lib/esm/ram/types.d.ts +4 -12
- package/lib/esm/repository/Repository.d.ts +31 -95
- package/lib/esm/repository/Repository.js +107 -203
- package/lib/esm/repository/Repository.js.map +1 -1
- package/lib/esm/repository/decorators.js +4 -3
- package/lib/esm/repository/decorators.js.map +1 -1
- package/lib/esm/repository/utils.d.ts +1 -1
- package/lib/esm/repository/utils.js +2 -3
- package/lib/esm/repository/utils.js.map +1 -1
- package/lib/esm/utils/ContextualLoggedClass.d.ts +16 -0
- package/lib/esm/utils/ContextualLoggedClass.js +29 -0
- package/lib/esm/utils/ContextualLoggedClass.js.map +1 -0
- package/lib/esm/utils/Services.d.ts +50 -0
- package/lib/esm/utils/Services.js +154 -0
- package/lib/esm/utils/Services.js.map +1 -0
- package/lib/esm/utils/decorators.d.ts +1 -9
- package/lib/esm/utils/decorators.js +26 -15
- package/lib/esm/utils/decorators.js.map +1 -1
- package/lib/esm/utils/index.d.ts +2 -1
- package/lib/esm/utils/index.js +2 -1
- package/lib/esm/utils/index.js.map +1 -1
- package/lib/identity/decorators.cjs +5 -11
- package/lib/identity/decorators.d.ts +3 -4
- package/lib/identity/decorators.js.map +1 -1
- package/lib/identity/index.cjs +0 -1
- package/lib/identity/index.d.ts +0 -1
- package/lib/identity/index.js.map +1 -1
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/interfaces/ContextuallyLogged.cjs +3 -0
- package/lib/interfaces/ContextuallyLogged.d.ts +8 -0
- package/lib/interfaces/ContextuallyLogged.js.map +1 -0
- package/lib/interfaces/ErrorParser.d.ts +2 -2
- package/lib/interfaces/Executor.d.ts +1 -1
- package/lib/interfaces/Observable.d.ts +8 -8
- package/lib/interfaces/Observer.d.ts +2 -2
- package/lib/interfaces/RawExecutor.d.ts +1 -1
- package/lib/model/BaseModel.cjs +2 -2
- package/lib/model/BaseModel.d.ts +2 -2
- package/lib/{ram/model/RamSequenceModel.cjs → model/SequenceModel.cjs} +14 -13
- package/lib/{ram/model/RamSequenceModel.d.ts → model/SequenceModel.d.ts} +3 -3
- package/lib/model/SequenceModel.js.map +1 -0
- package/lib/model/construction.cjs +2 -2
- package/lib/model/construction.d.ts +9 -8
- package/lib/model/construction.js.map +1 -1
- package/lib/model/decorators.cjs +3 -3
- package/lib/model/decorators.d.ts +5 -4
- package/lib/model/decorators.js.map +1 -1
- package/lib/model/index.cjs +1 -0
- package/lib/model/index.d.ts +1 -0
- package/lib/model/index.js.map +1 -1
- package/lib/overrides/Metadata.d.ts +1 -8
- package/lib/overrides/Model.cjs +4 -0
- package/lib/overrides/Model.d.ts +75 -0
- package/lib/overrides/Model.js.map +1 -0
- package/lib/overrides/index.cjs +2 -0
- package/lib/overrides/index.d.ts +2 -0
- package/lib/overrides/index.js.map +1 -1
- package/lib/overrides/injectables.cjs +4 -0
- package/lib/overrides/injectables.d.ts +10 -0
- package/lib/overrides/injectables.js.map +1 -0
- package/lib/overrides/overrides.cjs +43 -1
- package/lib/overrides/overrides.js.map +1 -1
- package/lib/persistence/Adapter.cjs +90 -66
- package/lib/persistence/Adapter.d.ts +55 -53
- package/lib/persistence/Adapter.js.map +1 -1
- package/lib/persistence/Dispatch.cjs +25 -18
- package/lib/persistence/Dispatch.d.ts +7 -6
- package/lib/persistence/Dispatch.js.map +1 -1
- package/lib/persistence/ObserverHandler.cjs +5 -4
- package/lib/persistence/ObserverHandler.d.ts +7 -6
- package/lib/persistence/ObserverHandler.js.map +1 -1
- package/lib/persistence/Sequence.cjs +136 -15
- package/lib/persistence/Sequence.d.ts +34 -25
- package/lib/persistence/Sequence.js.map +1 -1
- package/lib/persistence/constants.cjs +1 -0
- package/lib/persistence/constants.d.ts +1 -0
- package/lib/persistence/constants.js.map +1 -1
- package/lib/persistence/generators.cjs +36 -0
- package/lib/persistence/generators.d.ts +14 -0
- package/lib/persistence/generators.js.map +1 -0
- package/lib/persistence/index.cjs +1 -0
- package/lib/persistence/index.d.ts +1 -0
- package/lib/persistence/index.js.map +1 -1
- package/lib/persistence/migrations.cjs +7 -8
- package/lib/persistence/migrations.d.ts +8 -8
- package/lib/persistence/migrations.js.map +1 -1
- package/lib/persistence/types.d.ts +38 -20
- package/lib/query/Paginator.cjs +4 -4
- package/lib/query/Paginator.d.ts +6 -5
- package/lib/query/Paginator.js.map +1 -1
- package/lib/query/Statement.cjs +34 -21
- package/lib/query/Statement.d.ts +9 -8
- package/lib/query/Statement.js.map +1 -1
- package/lib/ram/RamAdapter.cjs +35 -33
- package/lib/ram/RamAdapter.d.ts +22 -31
- package/lib/ram/RamAdapter.js.map +1 -1
- package/lib/ram/RamPaginator.cjs +5 -3
- package/lib/ram/RamPaginator.d.ts +2 -1
- package/lib/ram/RamPaginator.js.map +1 -1
- package/lib/ram/RamStatement.d.ts +7 -7
- package/lib/ram/RamStatement.js.map +1 -1
- package/lib/ram/handlers.d.ts +3 -4
- package/lib/ram/handlers.js.map +1 -1
- package/lib/ram/index.cjs +0 -3
- package/lib/ram/index.d.ts +0 -3
- package/lib/ram/index.js.map +1 -1
- package/lib/ram/types.d.ts +4 -12
- package/lib/repository/Repository.cjs +108 -204
- package/lib/repository/Repository.d.ts +31 -95
- package/lib/repository/Repository.js.map +1 -1
- package/lib/repository/decorators.cjs +2 -1
- package/lib/repository/decorators.js.map +1 -1
- package/lib/repository/utils.cjs +2 -3
- package/lib/repository/utils.d.ts +1 -1
- package/lib/repository/utils.js.map +1 -1
- package/lib/utils/ContextualLoggedClass.cjs +33 -0
- package/lib/utils/ContextualLoggedClass.d.ts +16 -0
- package/lib/utils/ContextualLoggedClass.js.map +1 -0
- package/lib/utils/Services.cjs +159 -0
- package/lib/utils/Services.d.ts +50 -0
- package/lib/utils/Services.js.map +1 -0
- package/lib/utils/decorators.cjs +27 -16
- package/lib/utils/decorators.d.ts +1 -9
- package/lib/utils/decorators.js.map +1 -1
- package/lib/utils/index.cjs +2 -1
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/lib/esm/identity/utils.d.ts +0 -24
- package/lib/esm/identity/utils.js +0 -44
- package/lib/esm/identity/utils.js.map +0 -1
- package/lib/esm/ram/RamContext.d.ts +0 -28
- package/lib/esm/ram/RamContext.js +0 -30
- package/lib/esm/ram/RamContext.js.map +0 -1
- package/lib/esm/ram/RamSequence.d.ts +0 -75
- package/lib/esm/ram/RamSequence.js +0 -145
- package/lib/esm/ram/RamSequence.js.map +0 -1
- package/lib/esm/ram/model/RamSequenceModel.js.map +0 -1
- package/lib/esm/ram/model/index.d.ts +0 -1
- package/lib/esm/ram/model/index.js +0 -2
- package/lib/esm/ram/model/index.js.map +0 -1
- package/lib/identity/utils.cjs +0 -49
- package/lib/identity/utils.d.ts +0 -24
- package/lib/identity/utils.js.map +0 -1
- package/lib/ram/RamContext.cjs +0 -34
- package/lib/ram/RamContext.d.ts +0 -28
- package/lib/ram/RamContext.js.map +0 -1
- package/lib/ram/RamSequence.cjs +0 -149
- package/lib/ram/RamSequence.d.ts +0 -75
- package/lib/ram/RamSequence.js.map +0 -1
- package/lib/ram/model/RamSequenceModel.js.map +0 -1
- package/lib/ram/model/index.cjs +0 -18
- package/lib/ram/model/index.d.ts +0 -1
- package/lib/ram/model/index.js.map +0 -1
|
@@ -15,11 +15,9 @@ const Adapter_1 = require("./../persistence/Adapter.cjs");
|
|
|
15
15
|
const decorator_validation_1 = require("@decaf-ts/decorator-validation");
|
|
16
16
|
const constants_1 = require("./../persistence/constants.cjs");
|
|
17
17
|
const constants_2 = require("./constants.cjs");
|
|
18
|
-
const Sequence_1 = require("./../persistence/Sequence.cjs");
|
|
19
18
|
const Condition_1 = require("./../query/Condition.cjs");
|
|
20
|
-
const utils_1 = require("./../identity/utils.cjs");
|
|
21
19
|
const ObserverHandler_1 = require("./../persistence/ObserverHandler.cjs");
|
|
22
|
-
const
|
|
20
|
+
const logging_1 = require("@decaf-ts/logging");
|
|
23
21
|
const decoration_1 = require("@decaf-ts/decoration");
|
|
24
22
|
/**
|
|
25
23
|
* @description Core repository implementation for database operations on models on a table by table way.
|
|
@@ -105,13 +103,14 @@ class Repository extends db_decorators_1.Repository {
|
|
|
105
103
|
*/
|
|
106
104
|
get tableName() {
|
|
107
105
|
if (!this._tableName)
|
|
108
|
-
this._tableName =
|
|
106
|
+
this._tableName = decorator_validation_1.Model.tableName(this.class);
|
|
109
107
|
return this._tableName;
|
|
110
108
|
}
|
|
111
109
|
/**
|
|
112
110
|
* @description Primary key properties for this repository's model.
|
|
113
111
|
* @summary Gets the sequence options containing primary key information.
|
|
114
112
|
* @return {SequenceOptions} The primary key properties.
|
|
113
|
+
* @deprecated for Model.sequenceFor(class)
|
|
115
114
|
*/
|
|
116
115
|
get pkProps() {
|
|
117
116
|
return super.pkProps;
|
|
@@ -125,17 +124,6 @@ class Repository extends db_decorators_1.Repository {
|
|
|
125
124
|
if (clazz) {
|
|
126
125
|
Repository.register(clazz, this, this.adapter.alias);
|
|
127
126
|
if (adapter) {
|
|
128
|
-
// const flavour = Metadata.flavourOf(clazz);
|
|
129
|
-
// if (
|
|
130
|
-
// flavour &&
|
|
131
|
-
// flavour !== DefaultFlavour &&
|
|
132
|
-
// flavour !== adapter.flavour
|
|
133
|
-
// ) {
|
|
134
|
-
// this.log.warn(
|
|
135
|
-
// `Incompatible flavours detected between adapter (${adapter.flavour}) and model (${flavour})`
|
|
136
|
-
// );
|
|
137
|
-
// // throw new InternalError("Incompatible flavours");
|
|
138
|
-
// }
|
|
139
127
|
const flavour = decoration_1.Metadata.get(clazz, decoration_1.DecorationKeys.FLAVOUR);
|
|
140
128
|
if (flavour === decoration_1.DefaultFlavour) {
|
|
141
129
|
(0, decoration_1.uses)(adapter.flavour)(clazz);
|
|
@@ -147,6 +135,9 @@ class Repository extends db_decorators_1.Repository {
|
|
|
147
135
|
(0, db_decorators_1.wrapMethodWithContext)(this, this[name + "Prefix"], m, this[name + "Suffix"]);
|
|
148
136
|
});
|
|
149
137
|
}
|
|
138
|
+
logCtx(args, method) {
|
|
139
|
+
return Adapter_1.Adapter.logCtx(args, method);
|
|
140
|
+
}
|
|
150
141
|
/**
|
|
151
142
|
* @description Creates a proxy with overridden repository flags.
|
|
152
143
|
* @summary Returns a proxy of this repository with the specified flags overridden.
|
|
@@ -154,9 +145,6 @@ class Repository extends db_decorators_1.Repository {
|
|
|
154
145
|
* @return {Repository} A proxy of this repository with overridden flags.
|
|
155
146
|
*/
|
|
156
147
|
override(flags) {
|
|
157
|
-
this.log
|
|
158
|
-
.for(this.override)
|
|
159
|
-
.debug(`Overriding repository flags with ${JSON.stringify(flags)}`);
|
|
160
148
|
return new Proxy(this, {
|
|
161
149
|
get: (target, p, receiver) => {
|
|
162
150
|
const result = Reflect.get(target, p, receiver);
|
|
@@ -208,11 +196,16 @@ class Repository extends db_decorators_1.Repository {
|
|
|
208
196
|
*/
|
|
209
197
|
async createPrefix(model, ...args) {
|
|
210
198
|
const contextArgs = await db_decorators_1.Context.args(db_decorators_1.OperationKeys.CREATE, this.class, args, this.adapter, this._overrides || {});
|
|
199
|
+
const shouldRunHandlers = contextArgs.context.get("ignoreHandlers") !== false;
|
|
200
|
+
const shouldValidate = !contextArgs.context.get("ignoreValidation");
|
|
211
201
|
model = new this.class(model);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (
|
|
215
|
-
|
|
202
|
+
if (shouldRunHandlers)
|
|
203
|
+
await (0, db_decorators_1.enforceDBDecorators)(this, contextArgs.context, model, db_decorators_1.OperationKeys.CREATE, db_decorators_1.OperationKeys.ON);
|
|
204
|
+
if (shouldValidate) {
|
|
205
|
+
const errors = await Promise.resolve(model.hasErrors(...(contextArgs.context.get("ignoredValidationProperties") || [])));
|
|
206
|
+
if (errors)
|
|
207
|
+
throw new db_decorators_1.ValidationError(errors.toString());
|
|
208
|
+
}
|
|
216
209
|
return [model, ...contextArgs.args];
|
|
217
210
|
}
|
|
218
211
|
/**
|
|
@@ -223,13 +216,12 @@ class Repository extends db_decorators_1.Repository {
|
|
|
223
216
|
* @return {Promise<M>} The created model with updated properties.
|
|
224
217
|
*/
|
|
225
218
|
async create(model, ...args) {
|
|
219
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.create);
|
|
220
|
+
log.debug(`Creating new ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)}`);
|
|
226
221
|
// eslint-disable-next-line prefer-const
|
|
227
|
-
let { record, id, transient } = this.adapter.prepare(model,
|
|
228
|
-
record = await this.adapter.create(this.
|
|
229
|
-
|
|
230
|
-
if (args.length)
|
|
231
|
-
c = args[args.length - 1];
|
|
232
|
-
return this.adapter.revert(record, this.class, this.pk, id, c && c.get("rebuildWithTransient") ? transient : undefined);
|
|
222
|
+
let { record, id, transient } = this.adapter.prepare(model, ctx);
|
|
223
|
+
record = await this.adapter.create(this.class, id, record, ...ctxArgs);
|
|
224
|
+
return this.adapter.revert(record, this.class, id, transient, ctx);
|
|
233
225
|
}
|
|
234
226
|
/**
|
|
235
227
|
* @description Post-creation hook.
|
|
@@ -251,11 +243,13 @@ class Repository extends db_decorators_1.Repository {
|
|
|
251
243
|
async createAll(models, ...args) {
|
|
252
244
|
if (!models.length)
|
|
253
245
|
return models;
|
|
254
|
-
const
|
|
246
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.createAll);
|
|
247
|
+
log.debug(`Creating ${models.length} new ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)}`);
|
|
248
|
+
const prepared = models.map((m) => this.adapter.prepare(m, ctx));
|
|
255
249
|
const ids = prepared.map((p) => p.id);
|
|
256
250
|
let records = prepared.map((p) => p.record);
|
|
257
|
-
records = await this.adapter.createAll(this.
|
|
258
|
-
return records.map((r, i) => this.adapter.revert(r, this.class,
|
|
251
|
+
records = await this.adapter.createAll(this.class, ids, records, ...ctxArgs);
|
|
252
|
+
return records.map((r, i) => this.adapter.revert(r, this.class, ids[i], ctx.get("rebuildWithTransient") ? prepared[i].transient : undefined, ctx));
|
|
259
253
|
}
|
|
260
254
|
/**
|
|
261
255
|
* @description Prepares multiple models for creation.
|
|
@@ -267,14 +261,16 @@ class Repository extends db_decorators_1.Repository {
|
|
|
267
261
|
*/
|
|
268
262
|
async createAllPrefix(models, ...args) {
|
|
269
263
|
const contextArgs = await db_decorators_1.Context.args(db_decorators_1.OperationKeys.CREATE, this.class, args, this.adapter, this._overrides || {});
|
|
264
|
+
const shouldRunHandlers = contextArgs.context.get("ignoreHandlers") !== false;
|
|
265
|
+
const shouldValidate = !contextArgs.context.get("ignoreValidation");
|
|
270
266
|
if (!models.length)
|
|
271
267
|
return [models, ...contextArgs.args];
|
|
272
|
-
const opts =
|
|
268
|
+
const opts = decorator_validation_1.Model.sequenceFor(models[0]);
|
|
273
269
|
let ids = [];
|
|
274
270
|
if (opts.type) {
|
|
275
271
|
if (!opts.name)
|
|
276
|
-
opts.name =
|
|
277
|
-
ids = await (await this.adapter.Sequence(opts)).range(models.length);
|
|
272
|
+
opts.name = decorator_validation_1.Model.sequenceName(models[0], "pk");
|
|
273
|
+
ids = await (await this.adapter.Sequence(opts)).range(models.length, ...contextArgs.args);
|
|
278
274
|
}
|
|
279
275
|
else {
|
|
280
276
|
ids = models.map((m, i) => {
|
|
@@ -292,21 +288,17 @@ class Repository extends db_decorators_1.Repository {
|
|
|
292
288
|
? ids[i]
|
|
293
289
|
: `${m[this.pk]}`.toString());
|
|
294
290
|
}
|
|
295
|
-
|
|
291
|
+
if (shouldRunHandlers)
|
|
292
|
+
await (0, db_decorators_1.enforceDBDecorators)(this, contextArgs.context, m, db_decorators_1.OperationKeys.CREATE, db_decorators_1.OperationKeys.ON);
|
|
296
293
|
return m;
|
|
297
294
|
}));
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
: ` - ${i}: ${e.toString()}`;
|
|
306
|
-
return accum;
|
|
307
|
-
}, undefined);
|
|
308
|
-
if (errorMessages)
|
|
309
|
-
throw new db_decorators_1.ValidationError(errorMessages);
|
|
295
|
+
if (shouldValidate) {
|
|
296
|
+
const ignoredProps = contextArgs.context.get("ignoredValidationProperties") || [];
|
|
297
|
+
const errors = await Promise.all(models.map((m) => Promise.resolve(m.hasErrors(...ignoredProps))));
|
|
298
|
+
const errorMessages = (0, db_decorators_1.reduceErrorsToPrint)(errors);
|
|
299
|
+
if (errorMessages)
|
|
300
|
+
throw new db_decorators_1.ValidationError(errorMessages);
|
|
301
|
+
}
|
|
310
302
|
return [models, ...contextArgs.args];
|
|
311
303
|
}
|
|
312
304
|
/**
|
|
@@ -326,13 +318,15 @@ class Repository extends db_decorators_1.Repository {
|
|
|
326
318
|
/**
|
|
327
319
|
* @description Reads a model from the database by ID.
|
|
328
320
|
* @summary Retrieves a model instance from the database using its primary key.
|
|
329
|
-
* @param {
|
|
321
|
+
* @param {PrimaryKeyType} id - The primary key of the model to read.
|
|
330
322
|
* @param {...any[]} args - Additional arguments.
|
|
331
323
|
* @return {Promise<M>} The retrieved model instance.
|
|
332
324
|
*/
|
|
333
325
|
async read(id, ...args) {
|
|
334
|
-
const
|
|
335
|
-
|
|
326
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.read);
|
|
327
|
+
log.debug(`reading ${this.class.name} from table ${decorator_validation_1.Model.tableName(this.class)} with pk ${this.pk}`);
|
|
328
|
+
const m = await this.adapter.read(this.class, id, ...ctxArgs);
|
|
329
|
+
return this.adapter.revert(m, this.class, id, undefined, ctx);
|
|
336
330
|
}
|
|
337
331
|
/**
|
|
338
332
|
* @description Prepares for reading multiple models by IDs.
|
|
@@ -358,8 +352,10 @@ class Repository extends db_decorators_1.Repository {
|
|
|
358
352
|
* @return {Promise<M[]>} The retrieved model instances.
|
|
359
353
|
*/
|
|
360
354
|
async readAll(keys, ...args) {
|
|
361
|
-
const
|
|
362
|
-
|
|
355
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.readAll);
|
|
356
|
+
log.debug(`reading ${keys.length} ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)}`);
|
|
357
|
+
const records = await this.adapter.readAll(this.class, keys, ...ctxArgs);
|
|
358
|
+
return records.map((r, i) => this.adapter.revert(r, this.class, keys[i], undefined, ctx));
|
|
363
359
|
}
|
|
364
360
|
/**
|
|
365
361
|
* @description Updates a model in the database.
|
|
@@ -369,10 +365,12 @@ class Repository extends db_decorators_1.Repository {
|
|
|
369
365
|
* @return {Promise<M>} The updated model with refreshed properties.
|
|
370
366
|
*/
|
|
371
367
|
async update(model, ...args) {
|
|
368
|
+
const { ctxArgs, log, ctx } = this.logCtx(args, this.update);
|
|
372
369
|
// eslint-disable-next-line prefer-const
|
|
373
|
-
let { record, id, transient } = this.adapter.prepare(model,
|
|
374
|
-
|
|
375
|
-
|
|
370
|
+
let { record, id, transient } = this.adapter.prepare(model, ctx);
|
|
371
|
+
log.debug(`updating ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)} with id ${id}`);
|
|
372
|
+
record = await this.adapter.update(this.class, id, record, ...ctxArgs);
|
|
373
|
+
return this.adapter.revert(record, this.class, id, transient, ctx);
|
|
376
374
|
}
|
|
377
375
|
/**
|
|
378
376
|
* @description Prepares a model for update.
|
|
@@ -385,18 +383,19 @@ class Repository extends db_decorators_1.Repository {
|
|
|
385
383
|
*/
|
|
386
384
|
async updatePrefix(model, ...args) {
|
|
387
385
|
const contextArgs = await db_decorators_1.Context.args(db_decorators_1.OperationKeys.UPDATE, this.class, args, this.adapter, this._overrides || {});
|
|
386
|
+
const shouldRunHandlers = contextArgs.context.get("ignoreHandlers") !== false;
|
|
387
|
+
const shouldValidate = !contextArgs.context.get("ignoreValidation");
|
|
388
388
|
const pk = model[this.pk];
|
|
389
389
|
if (!pk)
|
|
390
390
|
throw new db_decorators_1.InternalError(`No value for the Id is defined under the property ${this.pk}`);
|
|
391
391
|
const oldModel = await this.read(pk, ...contextArgs.args);
|
|
392
|
-
model =
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if (
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
Repository.setMetadata(model, Repository.getMetadata(oldModel));
|
|
392
|
+
model = decorator_validation_1.Model.merge(oldModel, model, this.class);
|
|
393
|
+
if (shouldRunHandlers)
|
|
394
|
+
await (0, db_decorators_1.enforceDBDecorators)(this, contextArgs.context, model, db_decorators_1.OperationKeys.UPDATE, db_decorators_1.OperationKeys.ON, oldModel);
|
|
395
|
+
if (shouldValidate) {
|
|
396
|
+
const errors = await Promise.resolve(model.hasErrors(oldModel, ...decorator_validation_1.Model.relations(this.class), ...(contextArgs.context.get("ignoredValidationProperties") || [])));
|
|
397
|
+
if (errors)
|
|
398
|
+
throw new db_decorators_1.ValidationError(errors.toString());
|
|
400
399
|
}
|
|
401
400
|
return [model, ...contextArgs.args];
|
|
402
401
|
}
|
|
@@ -408,9 +407,11 @@ class Repository extends db_decorators_1.Repository {
|
|
|
408
407
|
* @return {Promise<M[]>} The updated models with refreshed properties.
|
|
409
408
|
*/
|
|
410
409
|
async updateAll(models, ...args) {
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
410
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.updateAll);
|
|
411
|
+
log.debug(`Updating ${models.length} new ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)}`);
|
|
412
|
+
const records = models.map((m) => this.adapter.prepare(m, ctx));
|
|
413
|
+
const updated = await this.adapter.updateAll(this.class, records.map((r) => r.id), records.map((r) => r.record), ...ctxArgs);
|
|
414
|
+
return updated.map((u, i) => this.adapter.revert(u, this.class, records[i].id, ctx.get("rebuildWithTransient") ? records[i].transient : undefined, ctx));
|
|
414
415
|
}
|
|
415
416
|
/**
|
|
416
417
|
* @description Prepares multiple models for update.
|
|
@@ -423,6 +424,8 @@ class Repository extends db_decorators_1.Repository {
|
|
|
423
424
|
*/
|
|
424
425
|
async updateAllPrefix(models, ...args) {
|
|
425
426
|
const contextArgs = await db_decorators_1.Context.args(db_decorators_1.OperationKeys.UPDATE, this.class, args, this.adapter, this._overrides || {});
|
|
427
|
+
const shouldRunHandlers = contextArgs.context.get("ignoreHandlers") !== false;
|
|
428
|
+
const shouldValidate = !contextArgs.context.get("ignoreValidation");
|
|
426
429
|
const ids = models.map((m) => {
|
|
427
430
|
const id = m[this.pk];
|
|
428
431
|
if (!id)
|
|
@@ -431,32 +434,18 @@ class Repository extends db_decorators_1.Repository {
|
|
|
431
434
|
});
|
|
432
435
|
const oldModels = await this.readAll(ids, ...contextArgs.args);
|
|
433
436
|
models = models.map((m, i) => {
|
|
434
|
-
m =
|
|
435
|
-
if (Repository.getMetadata(oldModels[i])) {
|
|
436
|
-
if (!Repository.getMetadata(m))
|
|
437
|
-
Repository.setMetadata(m, Repository.getMetadata(oldModels[i]));
|
|
438
|
-
}
|
|
437
|
+
m = decorator_validation_1.Model.merge(oldModels[i], m, this.class);
|
|
439
438
|
return m;
|
|
440
439
|
});
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
return accum;
|
|
451
|
-
}, undefined);
|
|
452
|
-
if (errorMessages)
|
|
453
|
-
throw new db_decorators_1.ValidationError(errorMessages);
|
|
454
|
-
models.forEach((m, i) => {
|
|
455
|
-
if (Repository.getMetadata(oldModels[i])) {
|
|
456
|
-
if (!Repository.getMetadata(m))
|
|
457
|
-
Repository.setMetadata(m, Repository.getMetadata(oldModels[i]));
|
|
458
|
-
}
|
|
459
|
-
});
|
|
440
|
+
if (shouldRunHandlers)
|
|
441
|
+
await Promise.all(models.map((m, i) => (0, db_decorators_1.enforceDBDecorators)(this, contextArgs.context, m, db_decorators_1.OperationKeys.UPDATE, db_decorators_1.OperationKeys.ON, oldModels[i])));
|
|
442
|
+
if (shouldValidate) {
|
|
443
|
+
const ignoredProps = contextArgs.context.get("ignoredValidationProperties") || [];
|
|
444
|
+
const errors = await Promise.all(models.map((m, i) => Promise.resolve(m.hasErrors(oldModels[i], m, ...ignoredProps))));
|
|
445
|
+
const errorMessages = (0, db_decorators_1.reduceErrorsToPrint)(errors);
|
|
446
|
+
if (errorMessages)
|
|
447
|
+
throw new db_decorators_1.ValidationError(errorMessages);
|
|
448
|
+
}
|
|
460
449
|
return [models, ...contextArgs.args];
|
|
461
450
|
}
|
|
462
451
|
/**
|
|
@@ -480,8 +469,10 @@ class Repository extends db_decorators_1.Repository {
|
|
|
480
469
|
* @return {Promise<M>} The deleted model instance.
|
|
481
470
|
*/
|
|
482
471
|
async delete(id, ...args) {
|
|
483
|
-
const
|
|
484
|
-
|
|
472
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.delete);
|
|
473
|
+
log.debug(`deleting new ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)} with pk ${id}`);
|
|
474
|
+
const m = await this.adapter.delete(this.class, id, ...ctxArgs);
|
|
475
|
+
return this.adapter.revert(m, this.class, id, undefined, ctx);
|
|
485
476
|
}
|
|
486
477
|
/**
|
|
487
478
|
* @description Prepares for deleting multiple models by IDs.
|
|
@@ -506,8 +497,10 @@ class Repository extends db_decorators_1.Repository {
|
|
|
506
497
|
* @return {Promise<M[]>} The deleted model instances.
|
|
507
498
|
*/
|
|
508
499
|
async deleteAll(keys, ...args) {
|
|
509
|
-
const
|
|
510
|
-
|
|
500
|
+
const { ctx, log, ctxArgs } = this.logCtx(args, this.create);
|
|
501
|
+
log.debug(`deleting ${keys.length} ${this.class.name} in table ${decorator_validation_1.Model.tableName(this.class)}`);
|
|
502
|
+
const results = await this.adapter.deleteAll(this.class, keys, ...ctxArgs);
|
|
503
|
+
return results.map((r, i) => this.adapter.revert(r, this.class, keys[i], undefined, ctx));
|
|
511
504
|
}
|
|
512
505
|
/**
|
|
513
506
|
* @description Implementation of the select method.
|
|
@@ -559,8 +552,18 @@ class Repository extends db_decorators_1.Repository {
|
|
|
559
552
|
writable: false,
|
|
560
553
|
});
|
|
561
554
|
const log = this.log.for(this.observe);
|
|
562
|
-
const tableName =
|
|
563
|
-
this.adapter.observe(this, (table
|
|
555
|
+
const tableName = decorator_validation_1.Model.tableName(this.class);
|
|
556
|
+
this.adapter.observe(this, (table,
|
|
557
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
558
|
+
event,
|
|
559
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
560
|
+
id,
|
|
561
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
562
|
+
...args) => {
|
|
563
|
+
if (typeof table === "string")
|
|
564
|
+
return table === tableName;
|
|
565
|
+
return decoration_1.Metadata.constr(table) === decoration_1.Metadata.constr(this.class);
|
|
566
|
+
});
|
|
564
567
|
log.verbose(`now observing ${this.adapter} filtering on table === ${tableName}`);
|
|
565
568
|
this.observerHandler.observe(observer, filter);
|
|
566
569
|
log.verbose(`Registered new observer ${observer.toString()}`);
|
|
@@ -599,12 +602,11 @@ class Repository extends db_decorators_1.Repository {
|
|
|
599
602
|
async updateObservers(table, event, id, ...args) {
|
|
600
603
|
if (!this.observerHandler)
|
|
601
604
|
throw new db_decorators_1.InternalError("ObserverHandler not initialized. Did you register any observables?");
|
|
602
|
-
this.
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
: Sequence_1.Sequence.parseValue(this.pkProps?.type, id), ...args);
|
|
605
|
+
const { log, ctxArgs } = this.logCtx(args, this.updateObservers);
|
|
606
|
+
log.verbose(`Updating ${this.observerHandler.count()} observers for ${this}`);
|
|
607
|
+
await this.observerHandler.updateObservers(table, event, Array.isArray(id)
|
|
608
|
+
? id.map((i) => Adapter_1.Adapter["_baseSequence"].parseValue(decorator_validation_1.Model.sequenceFor(this.class).type, i))
|
|
609
|
+
: Adapter_1.Adapter["_baseSequence"].parseValue(decorator_validation_1.Model.sequenceFor(this.class).type, id), ...ctxArgs);
|
|
608
610
|
}
|
|
609
611
|
/**
|
|
610
612
|
* @description Alias for updateObservers.
|
|
@@ -663,7 +665,7 @@ class Repository extends db_decorators_1.Repository {
|
|
|
663
665
|
* @throws {InternalError} If no repository is registered for the model.
|
|
664
666
|
*/
|
|
665
667
|
static get(model, alias) {
|
|
666
|
-
const name =
|
|
668
|
+
const name = decorator_validation_1.Model.tableName(model);
|
|
667
669
|
let registryName = name;
|
|
668
670
|
if (alias) {
|
|
669
671
|
registryName = [name, alias].join(db_decorators_1.DefaultSeparator);
|
|
@@ -684,7 +686,7 @@ class Repository extends db_decorators_1.Repository {
|
|
|
684
686
|
* @throws {InternalError} If a repository is already registered for the model.
|
|
685
687
|
*/
|
|
686
688
|
static register(model, repo, alias) {
|
|
687
|
-
let name =
|
|
689
|
+
let name = decorator_validation_1.Model.tableName(model);
|
|
688
690
|
if (alias) {
|
|
689
691
|
name = [name, alias].join(db_decorators_1.DefaultSeparator);
|
|
690
692
|
}
|
|
@@ -694,114 +696,16 @@ class Repository extends db_decorators_1.Repository {
|
|
|
694
696
|
}
|
|
695
697
|
this._cache[name] = repo;
|
|
696
698
|
}
|
|
697
|
-
/**
|
|
698
|
-
* @description Sets metadata on a model instance.
|
|
699
|
-
* @summary Attaches metadata to a model instance using a non-enumerable property.
|
|
700
|
-
* @template M - The model type that extends Model.
|
|
701
|
-
* @param {M} model - The model instance.
|
|
702
|
-
* @param {any} metadata - The metadata to attach to the model.
|
|
703
|
-
*/
|
|
704
|
-
static setMetadata(model, metadata) {
|
|
705
|
-
Object.defineProperty(model, constants_1.PersistenceKeys.METADATA, {
|
|
706
|
-
enumerable: false,
|
|
707
|
-
configurable: true,
|
|
708
|
-
writable: false,
|
|
709
|
-
value: metadata,
|
|
710
|
-
});
|
|
711
|
-
}
|
|
712
|
-
/**
|
|
713
|
-
* @description Gets metadata from a model instance.
|
|
714
|
-
* @summary Retrieves previously attached metadata from a model instance.
|
|
715
|
-
* @template M - The model type that extends Model.
|
|
716
|
-
* @param {M} model - The model instance.
|
|
717
|
-
* @return {any} The metadata or undefined if not found.
|
|
718
|
-
*/
|
|
719
|
-
static getMetadata(model) {
|
|
720
|
-
const descriptor = Object.getOwnPropertyDescriptor(model, constants_1.PersistenceKeys.METADATA);
|
|
721
|
-
return descriptor ? descriptor.value : undefined;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* @description Removes metadata from a model instance.
|
|
725
|
-
* @summary Deletes the metadata property from a model instance.
|
|
726
|
-
* @template M - The model type that extends Model.
|
|
727
|
-
* @param {M} model - The model instance.
|
|
728
|
-
*/
|
|
729
|
-
static removeMetadata(model) {
|
|
730
|
-
const descriptor = Object.getOwnPropertyDescriptor(model, constants_1.PersistenceKeys.METADATA);
|
|
731
|
-
if (descriptor)
|
|
732
|
-
delete model[constants_1.PersistenceKeys.METADATA];
|
|
733
|
-
}
|
|
734
|
-
/**
|
|
735
|
-
* @description Gets sequence options for a model's primary key.
|
|
736
|
-
* @summary Retrieves the sequence configuration for a model's primary key from metadata.
|
|
737
|
-
* @template M - The model type that extends Model.
|
|
738
|
-
* @param {M} model - The model instance.
|
|
739
|
-
* @return {SequenceOptions} The sequence options for the model's primary key.
|
|
740
|
-
* @throws {InternalError} If no sequence options are defined for the model.
|
|
741
|
-
*/
|
|
742
|
-
static getSequenceOptions(model) {
|
|
743
|
-
const pkName = decorator_validation_1.Model.pk(model.constructor);
|
|
744
|
-
const key = decoration_1.Metadata.key(db_decorators_1.DBKeys.ID, pkName);
|
|
745
|
-
const metadata = decoration_1.Metadata.get(model.constructor, key);
|
|
746
|
-
if (!metadata)
|
|
747
|
-
throw new db_decorators_1.InternalError("No sequence options defined for model. did you use the @pk decorator?");
|
|
748
|
-
return metadata;
|
|
749
|
-
}
|
|
750
|
-
/**
|
|
751
|
-
* @description Gets all indexes defined on a model.
|
|
752
|
-
* @summary Retrieves all index metadata from a model's property decorators.
|
|
753
|
-
* @template M - The model type that extends Model.
|
|
754
|
-
* @param {M | Constructor<M>} model - The model instance or constructor.
|
|
755
|
-
* @return {Record<string, Record<string, IndexMetadata>>} A nested record of property names to index metadata.
|
|
756
|
-
*/
|
|
757
|
-
static indexes(model) {
|
|
758
|
-
const indexDecorators = decoration_1.Metadata.get(model instanceof decorator_validation_1.Model ? model.constructor : model, constants_1.PersistenceKeys.INDEX);
|
|
759
|
-
return Object.keys(indexDecorators || {}).reduce((acum, t) => {
|
|
760
|
-
acum[t] = { [constants_1.PersistenceKeys.INDEX]: indexDecorators[t] };
|
|
761
|
-
return acum;
|
|
762
|
-
}, {});
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
|
-
* @description Gets all relation properties defined on a model.
|
|
766
|
-
* @summary Retrieves the names of all properties marked as relations in the model hierarchy.
|
|
767
|
-
* @template M - The model type that extends Model.
|
|
768
|
-
* @param {M | Constructor<M>} model - The model instance or constructor.
|
|
769
|
-
* @return {string[]} An array of property names that are relations.
|
|
770
|
-
*/
|
|
771
|
-
static relations(model) {
|
|
772
|
-
return (decoration_1.Metadata.relations(model instanceof decorator_validation_1.Model ? model.constructor : model) || []);
|
|
773
|
-
}
|
|
774
|
-
/**
|
|
775
|
-
* @description Gets the table name for a model.
|
|
776
|
-
* @summary Retrieves the database table name associated with a model.
|
|
777
|
-
* @template M - The model type that extends Model.
|
|
778
|
-
* @param {M | Constructor<M>} model - The model instance or constructor.
|
|
779
|
-
* @return {string} The table name for the model.
|
|
780
|
-
*/
|
|
781
|
-
static table(model) {
|
|
782
|
-
return (0, utils_1.getTableName)(model);
|
|
783
|
-
}
|
|
784
|
-
/**
|
|
785
|
-
* @description Gets the column name for a model attribute.
|
|
786
|
-
* @summary Retrieves the database column name for a model property.
|
|
787
|
-
* @template M - The model type that extends Model.
|
|
788
|
-
* @param {M} model - The model instance.
|
|
789
|
-
* @param {string} attribute - The attribute/property name.
|
|
790
|
-
* @return {string} The column name for the attribute.
|
|
791
|
-
*/
|
|
792
|
-
static column(model, attribute) {
|
|
793
|
-
return (0, utils_1.getColumnName)(model, attribute);
|
|
794
|
-
}
|
|
795
699
|
}
|
|
796
700
|
exports.Repository = Repository;
|
|
797
701
|
__decorate([
|
|
798
|
-
(0,
|
|
702
|
+
(0, logging_1.final)(),
|
|
799
703
|
__metadata("design:type", Function),
|
|
800
704
|
__metadata("design:paramtypes", [Object, Function]),
|
|
801
705
|
__metadata("design:returntype", void 0)
|
|
802
706
|
], Repository.prototype, "observe", null);
|
|
803
707
|
__decorate([
|
|
804
|
-
(0,
|
|
708
|
+
(0, logging_1.final)(),
|
|
805
709
|
__metadata("design:type", Function),
|
|
806
710
|
__metadata("design:paramtypes", [Object]),
|
|
807
711
|
__metadata("design:returntype", void 0)
|