@decaf-ts/core 0.7.2 → 0.7.3
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 +1 -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 +4 -10
- 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/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 +4 -3
- package/lib/esm/model/decorators.js +2 -2
- package/lib/esm/model/decorators.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 +52 -51
- package/lib/esm/persistence/Adapter.js +76 -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 +10 -17
- package/lib/esm/persistence/Sequence.js +10 -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/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 +21 -21
- package/lib/esm/ram/RamAdapter.js +36 -23
- 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/RamSequence.d.ts +4 -11
- package/lib/esm/ram/RamSequence.js +20 -24
- package/lib/esm/ram/RamSequence.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 -1
- package/lib/esm/ram/index.js +0 -1
- 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 +106 -201
- package/lib/esm/repository/Repository.js.map +1 -1
- package/lib/esm/repository/decorators.js +3 -2
- 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 +28 -0
- package/lib/esm/utils/Services.js +88 -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 +3 -9
- 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/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 +1 -1
- package/lib/model/decorators.d.ts +4 -3
- package/lib/model/decorators.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 +81 -66
- package/lib/persistence/Adapter.d.ts +52 -51
- 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 +10 -15
- package/lib/persistence/Sequence.d.ts +10 -17
- 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/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 -22
- package/lib/ram/RamAdapter.d.ts +21 -21
- 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/RamSequence.cjs +19 -23
- package/lib/ram/RamSequence.d.ts +4 -11
- package/lib/ram/RamSequence.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 -1
- package/lib/ram/index.d.ts +0 -1
- package/lib/ram/index.js.map +1 -1
- package/lib/ram/types.d.ts +4 -12
- package/lib/repository/Repository.cjs +107 -202
- package/lib/repository/Repository.d.ts +31 -95
- package/lib/repository/Repository.js.map +1 -1
- package/lib/repository/decorators.cjs +1 -0
- 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 +93 -0
- package/lib/utils/Services.d.ts +28 -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/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
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { BaseError, Context, OperationKeys,
|
|
1
|
+
import { BaseError, Context, OperationKeys, Contextual, BulkCrudOperationKeys, PrimaryKeyType } from "@decaf-ts/db-decorators";
|
|
2
2
|
import { type Observer } from "../interfaces/Observer";
|
|
3
3
|
import { Model, ModelConstructor } from "@decaf-ts/decorator-validation";
|
|
4
4
|
import { SequenceOptions } from "../interfaces/SequenceOptions";
|
|
5
5
|
import { RawExecutor } from "../interfaces/RawExecutor";
|
|
6
|
-
import { Observable } from "../interfaces/Observable";
|
|
7
6
|
import type { Repository } from "../repository/Repository";
|
|
8
7
|
import { Sequence } from "./Sequence";
|
|
9
8
|
import { ErrorParser } from "../interfaces";
|
|
10
9
|
import { Statement } from "../query/Statement";
|
|
11
10
|
import type { Dispatch } from "./Dispatch";
|
|
12
|
-
import { type EventIds, Migration, type ObserverFilter } from "./types";
|
|
11
|
+
import { type EventIds, FlagsOf, Migration, type ObserverFilter, PersistenceObservable, PersistenceObserver, PreparedModel } from "./types";
|
|
13
12
|
import { ObserverHandler } from "./ObserverHandler";
|
|
14
|
-
import {
|
|
13
|
+
import { Impersonatable } from "@decaf-ts/logging";
|
|
15
14
|
import { AdapterDispatch } from "./types";
|
|
16
15
|
import { type Constructor } from "@decaf-ts/decoration";
|
|
16
|
+
import { ContextualArgs, ContextualizedArgs, ContextualLoggedClass } from "../utils/ContextualLoggedClass";
|
|
17
|
+
export type AdapterSubClass<A> = A extends Adapter<any, any, any, any> ? A : never;
|
|
17
18
|
/**
|
|
18
19
|
* @description Abstract Facade class for persistence adapters
|
|
19
20
|
* @summary Provides the foundation for all database adapters in the persistence layer. This class
|
|
@@ -106,7 +107,7 @@ import { type Constructor } from "@decaf-ts/decoration";
|
|
|
106
107
|
* Adapter --|> Observer
|
|
107
108
|
* Adapter --|> ErrorParser
|
|
108
109
|
*/
|
|
109
|
-
export declare abstract class Adapter<CONF, CONN, QUERY,
|
|
110
|
+
export declare abstract class Adapter<CONF, CONN, QUERY, CONTEXT extends Context<any> = Context> extends ContextualLoggedClass<CONTEXT> implements RawExecutor<QUERY>, Contextual<CONTEXT>, PersistenceObservable<CONTEXT>, PersistenceObserver<CONTEXT>, Impersonatable<any, [Partial<CONF>, ...any[]]>, ErrorParser {
|
|
110
111
|
private readonly _config;
|
|
111
112
|
readonly flavour: string;
|
|
112
113
|
private readonly _alias?;
|
|
@@ -114,7 +115,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
114
115
|
private static _cache;
|
|
115
116
|
private static _baseRepository;
|
|
116
117
|
private static _baseDispatch;
|
|
117
|
-
protected dispatch?: AdapterDispatch
|
|
118
|
+
protected dispatch?: AdapterDispatch<typeof this>;
|
|
118
119
|
protected readonly observerHandler?: ObserverHandler;
|
|
119
120
|
protected _client?: CONN;
|
|
120
121
|
/**
|
|
@@ -134,9 +135,9 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
134
135
|
* @description Gets the repository constructor for this adapter
|
|
135
136
|
* @summary Returns the constructor for creating repositories that work with this adapter
|
|
136
137
|
* @template M - The model type
|
|
137
|
-
* @return {Constructor<Repository<
|
|
138
|
+
* @return {Constructor<Repository<any, Adapter<CONF, CONN, QUERY, CONTEXT>>>} The repository constructor
|
|
138
139
|
*/
|
|
139
|
-
repository<
|
|
140
|
+
repository<R extends Repository<any, Adapter<CONF, CONN, QUERY, CONTEXT>>>(): Constructor<R>;
|
|
140
141
|
protected shutdownProxies(k?: string): Promise<void>;
|
|
141
142
|
/**
|
|
142
143
|
* @description Shuts down the adapter
|
|
@@ -156,13 +157,13 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
156
157
|
* @template M - The model type
|
|
157
158
|
* @return {Statement} A statement builder for the model
|
|
158
159
|
*/
|
|
159
|
-
abstract Statement<M extends Model>(): Statement<
|
|
160
|
+
abstract Statement<M extends Model>(): Statement<M, Adapter<CONF, CONN, QUERY, CONTEXT>, any>;
|
|
160
161
|
/**
|
|
161
162
|
* @description Creates a new dispatch instance
|
|
162
163
|
* @summary Factory method that creates a dispatch instance for this adapter
|
|
163
164
|
* @return {Dispatch} A new dispatch instance
|
|
164
165
|
*/
|
|
165
|
-
protected Dispatch(): Dispatch
|
|
166
|
+
protected Dispatch(): Dispatch<Adapter<CONF, CONN, QUERY, CONTEXT>>;
|
|
166
167
|
/**
|
|
167
168
|
* @description Creates a new observer handler
|
|
168
169
|
* @summary Factory method that creates an observer handler for this adapter
|
|
@@ -180,9 +181,10 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
180
181
|
* @description Parses a database error into a standardized error
|
|
181
182
|
* @summary Converts database-specific errors into standardized application errors
|
|
182
183
|
* @param {Error} err - The original database error
|
|
184
|
+
* @param args
|
|
183
185
|
* @return {BaseError} A standardized error
|
|
184
186
|
*/
|
|
185
|
-
abstract parseError(err: Error):
|
|
187
|
+
abstract parseError<E extends BaseError>(err: Error, ...args: any[]): E;
|
|
186
188
|
/**
|
|
187
189
|
* @description Initializes the adapter
|
|
188
190
|
* @summary Performs any necessary setup for the adapter, such as establishing connections
|
|
@@ -208,18 +210,12 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
208
210
|
* @param {...any[]} args - Additional arguments
|
|
209
211
|
* @return {Promise<F>} The complete set of flags
|
|
210
212
|
*/
|
|
211
|
-
protected flags<M extends Model>(operation: OperationKeys, model: Constructor<M
|
|
213
|
+
protected flags<M extends Model>(operation: OperationKeys | string, model: Constructor<M> | Constructor<M>[], flags: Partial<FlagsOf<CONTEXT>>, ...args: any[]): Promise<FlagsOf<CONTEXT>>;
|
|
212
214
|
/**
|
|
213
215
|
* @description The context constructor for this adapter
|
|
214
216
|
* @summary Reference to the context class constructor used by this adapter
|
|
215
217
|
*/
|
|
216
|
-
protected Context:
|
|
217
|
-
new (): Context<FLAGS>;
|
|
218
|
-
factory: import("@decaf-ts/db-decorators").ContextFactory<any>;
|
|
219
|
-
childFrom<F extends RepositoryFlags, C extends Context<F>>(context: C, overrides?: Partial<F>): C;
|
|
220
|
-
from<M extends Model, F extends RepositoryFlags, C extends Context<F>>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE, overrides: Partial<F>, model: Constructor<M>, ...args: any[]): Promise<C>;
|
|
221
|
-
args<M extends Model<any>, C extends Context<F>, F extends RepositoryFlags>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE, model: Constructor<M>, args: any[], contextual?: Contextual<F>, overrides?: Partial<F>): Promise<import("@decaf-ts/db-decorators").ContextArgs<F, C>>;
|
|
222
|
-
};
|
|
218
|
+
protected readonly Context: Constructor<CONTEXT>;
|
|
223
219
|
/**
|
|
224
220
|
* @description Creates a context for a database operation
|
|
225
221
|
* @summary Generates a context object that describes a database operation, used for tracking and auditing
|
|
@@ -231,46 +227,42 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
231
227
|
* @param {...any[]} args - Additional arguments
|
|
232
228
|
* @return {Promise<C>} A promise that resolves to the context object
|
|
233
229
|
*/
|
|
234
|
-
context<M extends Model>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE, overrides: Partial<
|
|
230
|
+
context<M extends Model>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE | string, overrides: Partial<FlagsOf<CONTEXT>>, model: Constructor<M> | Constructor<M>[], ...args: any[]): Promise<CONTEXT>;
|
|
235
231
|
/**
|
|
236
232
|
* @description Prepares a model for persistence
|
|
237
233
|
* @summary Converts a model instance into a format suitable for database storage,
|
|
238
234
|
* handling column mapping and separating transient properties
|
|
235
|
+
* handling column mapping and separating transient properties
|
|
239
236
|
* @template M - The model type
|
|
240
237
|
* @param {M} model - The model instance to prepare
|
|
241
|
-
* @param pk - The primary key property name
|
|
242
238
|
* @param args - optional args for subclassing purposes
|
|
243
239
|
* @return The prepared data
|
|
244
240
|
*/
|
|
245
|
-
prepare<M extends Model>(model: M,
|
|
246
|
-
record: Record<string, any>;
|
|
247
|
-
id: string;
|
|
248
|
-
transient?: Record<string, any>;
|
|
249
|
-
};
|
|
241
|
+
prepare<M extends Model>(model: M, ...args: ContextualArgs<CONTEXT>): PreparedModel;
|
|
250
242
|
/**
|
|
251
243
|
* @description Converts database data back into a model instance
|
|
252
244
|
* @summary Reconstructs a model instance from database data, handling column mapping
|
|
253
245
|
* and reattaching transient properties
|
|
254
246
|
* @template M - The model type
|
|
255
247
|
* @param obj - The database record
|
|
256
|
-
* @param {
|
|
248
|
+
* @param {Constructor<M>} clazz - The model class or name
|
|
257
249
|
* @param pk - The primary key property name
|
|
258
250
|
* @param {string|number|bigint} id - The primary key value
|
|
259
251
|
* @param [transient] - Transient properties to reattach
|
|
260
252
|
* @param [args] - options args for subclassing purposes
|
|
261
253
|
* @return {M} The reconstructed model instance
|
|
262
254
|
*/
|
|
263
|
-
revert<M extends Model>(obj: Record<string, any>, clazz:
|
|
255
|
+
revert<M extends Model>(obj: Record<string, any>, clazz: Constructor<M>, id: PrimaryKeyType, transient?: Record<string, any>, ...args: ContextualArgs<CONTEXT>): M;
|
|
264
256
|
/**
|
|
265
257
|
* @description Creates a new record in the database
|
|
266
258
|
* @summary Inserts a new record with the given ID and data into the specified table
|
|
267
|
-
* @param {string}
|
|
268
|
-
* @param {
|
|
259
|
+
* @param {string} clazz - The name of the table to insert into
|
|
260
|
+
* @param {PrimaryKeyType} id - The identifier for the new record
|
|
269
261
|
* @param model - The data to insert
|
|
270
262
|
* @param {any[]} args - Additional arguments specific to the adapter implementation
|
|
271
263
|
* @return A promise that resolves to the created record
|
|
272
264
|
*/
|
|
273
|
-
abstract create(
|
|
265
|
+
abstract create<M extends Model>(clazz: Constructor<M>, id: PrimaryKeyType, model: Record<string, any>, ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>>;
|
|
274
266
|
/**
|
|
275
267
|
* @description Creates multiple records in the database
|
|
276
268
|
* @summary Inserts multiple records with the given IDs and data into the specified table
|
|
@@ -280,7 +272,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
280
272
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
281
273
|
* @return A promise that resolves to an array of created records
|
|
282
274
|
*/
|
|
283
|
-
createAll(
|
|
275
|
+
createAll<M extends Model>(clazz: Constructor<M>, id: PrimaryKeyType[], model: Record<string, any>[], ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>[]>;
|
|
284
276
|
/**
|
|
285
277
|
* @description Retrieves a record from the database
|
|
286
278
|
* @summary Fetches a record with the given ID from the specified table
|
|
@@ -289,7 +281,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
289
281
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
290
282
|
* @return A promise that resolves to the retrieved record
|
|
291
283
|
*/
|
|
292
|
-
abstract read(tableName:
|
|
284
|
+
abstract read<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>>;
|
|
293
285
|
/**
|
|
294
286
|
* @description Retrieves multiple records from the database
|
|
295
287
|
* @summary Fetches multiple records with the given IDs from the specified table
|
|
@@ -298,27 +290,28 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
298
290
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
299
291
|
* @return A promise that resolves to an array of retrieved records
|
|
300
292
|
*/
|
|
301
|
-
readAll(
|
|
293
|
+
readAll<M extends Model>(clazz: Constructor<M>, id: PrimaryKeyType[], ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>[]>;
|
|
302
294
|
/**
|
|
303
295
|
* @description Updates a record in the database
|
|
304
296
|
* @summary Modifies an existing record with the given ID in the specified table
|
|
305
|
-
* @
|
|
306
|
-
* @param {
|
|
297
|
+
* @template M - The model type
|
|
298
|
+
* @param {Constructor<M>} tableName - The name of the table to update
|
|
299
|
+
* @param {PrimaryKeyType} id - The identifier of the record to update
|
|
307
300
|
* @param model - The new data for the record
|
|
308
301
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
309
302
|
* @return A promise that resolves to the updated record
|
|
310
303
|
*/
|
|
311
|
-
abstract update(
|
|
304
|
+
abstract update<M extends Model>(clazz: Constructor<M>, id: PrimaryKeyType, model: Record<string, any>, ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>>;
|
|
312
305
|
/**
|
|
313
306
|
* @description Updates multiple records in the database
|
|
314
307
|
* @summary Modifies multiple existing records with the given IDs in the specified table
|
|
315
|
-
* @param {
|
|
308
|
+
* @param {Constructor<M>} tableName - The name of the table to update
|
|
316
309
|
* @param {string[]|number[]} id - The identifiers of the records to update
|
|
317
310
|
* @param model - The new data for each record
|
|
318
311
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
319
312
|
* @return A promise that resolves to an array of updated records
|
|
320
313
|
*/
|
|
321
|
-
updateAll(
|
|
314
|
+
updateAll<M extends Model>(clazz: Constructor<M>, id: PrimaryKeyType[], model: Record<string, any>[], ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>[]>;
|
|
322
315
|
/**
|
|
323
316
|
* @description Deletes a record from the database
|
|
324
317
|
* @summary Removes a record with the given ID from the specified table
|
|
@@ -327,7 +320,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
327
320
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
328
321
|
* @return A promise that resolves to the deleted record
|
|
329
322
|
*/
|
|
330
|
-
abstract delete(tableName:
|
|
323
|
+
abstract delete<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>>;
|
|
331
324
|
/**
|
|
332
325
|
* @description Deletes multiple records from the database
|
|
333
326
|
* @summary Removes multiple records with the given IDs from the specified table
|
|
@@ -336,7 +329,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
336
329
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
337
330
|
* @return A promise that resolves to an array of deleted records
|
|
338
331
|
*/
|
|
339
|
-
deleteAll(tableName:
|
|
332
|
+
deleteAll<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType[], ...args: ContextualArgs<CONTEXT>): Promise<Record<string, any>[]>;
|
|
340
333
|
/**
|
|
341
334
|
* @description Executes a raw query against the database
|
|
342
335
|
* @summary Allows executing database-specific queries directly
|
|
@@ -346,7 +339,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
346
339
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
347
340
|
* @return {Promise<R>} A promise that resolves to the query result
|
|
348
341
|
*/
|
|
349
|
-
abstract raw<R>(rawInput: QUERY, ...args:
|
|
342
|
+
abstract raw<R>(rawInput: QUERY, ...args: ContextualArgs<CONTEXT>): Promise<R>;
|
|
350
343
|
/**
|
|
351
344
|
* @description Registers an observer for database events
|
|
352
345
|
* @summary Adds an observer to be notified about database changes. The observer can optionally
|
|
@@ -373,7 +366,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
373
366
|
* @param {...any[]} args - Additional arguments to pass to the observers
|
|
374
367
|
* @return {Promise<void>} A promise that resolves when all observers have been notified
|
|
375
368
|
*/
|
|
376
|
-
updateObservers(table: string, event: OperationKeys | BulkCrudOperationKeys | string, id: EventIds, ...args:
|
|
369
|
+
updateObservers<M extends Model>(table: Constructor<M> | string, event: OperationKeys | BulkCrudOperationKeys | string, id: EventIds, ...args: ContextualArgs<CONTEXT>): Promise<void>;
|
|
377
370
|
/**
|
|
378
371
|
* @description Refreshes data based on a database event
|
|
379
372
|
* @summary Implementation of the Observer interface method that delegates to updateObservers
|
|
@@ -383,7 +376,7 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
383
376
|
* @param {...any[]} args - Additional arguments related to the event
|
|
384
377
|
* @return {Promise<void>} A promise that resolves when the refresh is complete
|
|
385
378
|
*/
|
|
386
|
-
refresh(table: string, event: OperationKeys | BulkCrudOperationKeys | string, id: EventIds, ...args:
|
|
379
|
+
refresh<M extends Model>(table: Constructor<M> | string, event: OperationKeys | BulkCrudOperationKeys | string, id: EventIds, ...args: ContextualArgs<CONTEXT>): Promise<void>;
|
|
387
380
|
/**
|
|
388
381
|
* @description Gets a string representation of the adapter
|
|
389
382
|
* @summary Returns a human-readable string identifying this adapter
|
|
@@ -411,12 +404,11 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
411
404
|
* @template CONF - The database driver config
|
|
412
405
|
* @template CONN - The database driver instance
|
|
413
406
|
* @template QUERY - The query type
|
|
414
|
-
* @template
|
|
415
|
-
* @template FLAGS - The repository flags type
|
|
407
|
+
* @template CONTEXT - The context type
|
|
416
408
|
* @param {string} flavour - The flavor name of the adapter to retrieve
|
|
417
|
-
* @return {Adapter<CONF, CONN, QUERY, CONTEXT
|
|
409
|
+
* @return {Adapter<CONF, CONN, QUERY, CONTEXT> | undefined} The adapter instance or undefined if not found
|
|
418
410
|
*/
|
|
419
|
-
static get<
|
|
411
|
+
static get<A extends Adapter<any, any, any, any>>(flavour?: any): A | undefined;
|
|
420
412
|
/**
|
|
421
413
|
* @description Sets the current default adapter
|
|
422
414
|
* @summary Changes which adapter is used as the default for operations
|
|
@@ -433,6 +425,14 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
433
425
|
*/
|
|
434
426
|
static models<M extends Model>(flavour: string): ModelConstructor<M>[];
|
|
435
427
|
static decoration(): void;
|
|
428
|
+
/**
|
|
429
|
+
* @description retrieves the context from args and returns it, the logger and the args (with context at the end)
|
|
430
|
+
* @summary NOTE: if the last argument was a context, this removes the context from the arg list
|
|
431
|
+
* @param args
|
|
432
|
+
* @param method
|
|
433
|
+
*/
|
|
434
|
+
static logCtx<CONTEXT extends Context<any>, ARGS extends any[] = any[]>(this: any, args: ARGS, method: string): ContextualizedArgs<CONTEXT, ARGS>;
|
|
435
|
+
static logCtx<CONTEXT extends Context<any>, ARGS extends any[] = any[]>(this: any, args: ARGS, method: (...args: any[]) => any): ContextualizedArgs<CONTEXT, ARGS>;
|
|
436
436
|
protected proxies?: Record<string, typeof this>;
|
|
437
437
|
/**
|
|
438
438
|
* @description Returns the client instance for the adapter
|
|
@@ -447,7 +447,8 @@ export declare abstract class Adapter<CONF, CONN, QUERY, FLAGS extends Repositor
|
|
|
447
447
|
*/
|
|
448
448
|
protected abstract getClient(): CONN;
|
|
449
449
|
get client(): CONN;
|
|
450
|
-
for(config: Partial<CONF>, ...args: any[]):
|
|
451
|
-
migrations(): Constructor<Migration<any, this
|
|
452
|
-
|
|
450
|
+
for(config: Partial<CONF>, ...args: any[]): this;
|
|
451
|
+
migrations(): Constructor<Migration<any, this>>[];
|
|
452
|
+
protected getQueryRunner(): Promise<CONN>;
|
|
453
|
+
migrate(...args: [...any[], CONTEXT]): Promise<void>;
|
|
453
454
|
}
|
|
@@ -10,12 +10,12 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
import { InternalError, Context, OperationKeys, DefaultRepositoryFlags, } from "@decaf-ts/db-decorators";
|
|
11
11
|
import { hashObj, Model, } from "@decaf-ts/decorator-validation";
|
|
12
12
|
import { PersistenceKeys } from "./constants.js";
|
|
13
|
-
import { final } from "
|
|
13
|
+
import { final } from "@decaf-ts/logging";
|
|
14
14
|
import { ObserverHandler } from "./ObserverHandler.js";
|
|
15
|
-
import {
|
|
16
|
-
import { getColumnName, getTableName } from "./../identity/utils.js";
|
|
15
|
+
import { Logging } from "@decaf-ts/logging";
|
|
17
16
|
import { Decoration, DefaultFlavour, Metadata, } from "@decaf-ts/decoration";
|
|
18
17
|
import { MigrationError } from "./errors.js";
|
|
18
|
+
import { ContextualLoggedClass, } from "./../utils/ContextualLoggedClass.js";
|
|
19
19
|
const flavourResolver = Decoration["flavourResolver"].bind(Decoration);
|
|
20
20
|
Decoration["flavourResolver"] = (obj) => {
|
|
21
21
|
try {
|
|
@@ -135,7 +135,7 @@ Decoration["flavourResolver"] = (obj) => {
|
|
|
135
135
|
* Adapter --|> Observer
|
|
136
136
|
* Adapter --|> ErrorParser
|
|
137
137
|
*/
|
|
138
|
-
export class Adapter extends
|
|
138
|
+
export class Adapter extends ContextualLoggedClass {
|
|
139
139
|
static { this._cache = {}; }
|
|
140
140
|
/**
|
|
141
141
|
* @description Gets the native persistence config
|
|
@@ -158,7 +158,7 @@ export class Adapter extends LoggedClass {
|
|
|
158
158
|
* @description Gets the repository constructor for this adapter
|
|
159
159
|
* @summary Returns the constructor for creating repositories that work with this adapter
|
|
160
160
|
* @template M - The model type
|
|
161
|
-
* @return {Constructor<Repository<
|
|
161
|
+
* @return {Constructor<Repository<any, Adapter<CONF, CONN, QUERY, CONTEXT>>>} The repository constructor
|
|
162
162
|
*/
|
|
163
163
|
repository() {
|
|
164
164
|
if (!Adapter._baseRepository)
|
|
@@ -273,12 +273,18 @@ export class Adapter extends LoggedClass {
|
|
|
273
273
|
async flags(operation, model, flags,
|
|
274
274
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
275
275
|
...args) {
|
|
276
|
+
let log = (flags.logger || Logging.for(this.toString()));
|
|
277
|
+
if (flags.correlationId)
|
|
278
|
+
log = log.for({ correlationId: flags.correlationId });
|
|
276
279
|
return Object.assign({}, DefaultRepositoryFlags, flags, {
|
|
277
|
-
affectedTables:
|
|
280
|
+
affectedTables: (Array.isArray(model) ? model : [model]).map(Model.tableName),
|
|
278
281
|
writeOperation: operation !== OperationKeys.READ,
|
|
279
282
|
timestamp: new Date(),
|
|
280
283
|
operation: operation,
|
|
281
|
-
ignoredValidationProperties:
|
|
284
|
+
ignoredValidationProperties: Array.isArray(model)
|
|
285
|
+
? []
|
|
286
|
+
: Metadata.validationExceptions(model, operation),
|
|
287
|
+
logger: log,
|
|
282
288
|
});
|
|
283
289
|
}
|
|
284
290
|
/**
|
|
@@ -294,7 +300,7 @@ export class Adapter extends LoggedClass {
|
|
|
294
300
|
*/
|
|
295
301
|
async context(operation, overrides, model, ...args) {
|
|
296
302
|
const log = this.log.for(this.context);
|
|
297
|
-
log.debug(`Creating new context for ${operation} operation on ${model.name} model with flag overrides: ${JSON.stringify(overrides)}`);
|
|
303
|
+
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)}`);
|
|
298
304
|
const flags = await this.flags(operation, model, overrides, ...args);
|
|
299
305
|
return new this.Context().accumulate(flags);
|
|
300
306
|
}
|
|
@@ -302,38 +308,37 @@ export class Adapter extends LoggedClass {
|
|
|
302
308
|
* @description Prepares a model for persistence
|
|
303
309
|
* @summary Converts a model instance into a format suitable for database storage,
|
|
304
310
|
* handling column mapping and separating transient properties
|
|
311
|
+
* handling column mapping and separating transient properties
|
|
305
312
|
* @template M - The model type
|
|
306
313
|
* @param {M} model - The model instance to prepare
|
|
307
|
-
* @param pk - The primary key property name
|
|
308
314
|
* @param args - optional args for subclassing purposes
|
|
309
315
|
* @return The prepared data
|
|
310
316
|
*/
|
|
311
|
-
prepare(model,
|
|
312
|
-
|
|
313
|
-
...args) {
|
|
314
|
-
const log = this.log.for(this.prepare);
|
|
317
|
+
prepare(model, ...args) {
|
|
318
|
+
const { log } = this.logCtx(args, this.prepare);
|
|
315
319
|
const split = model.segregate();
|
|
316
320
|
const result = Object.entries(split.model).reduce((accum, [key, val]) => {
|
|
317
321
|
if (typeof val === "undefined")
|
|
318
322
|
return accum;
|
|
319
|
-
const mappedProp =
|
|
323
|
+
const mappedProp = Model.columnName(model.constructor, key);
|
|
320
324
|
if (this.isReserved(mappedProp))
|
|
321
325
|
throw new InternalError(`Property name ${mappedProp} is reserved`);
|
|
322
326
|
accum[mappedProp] = val;
|
|
323
327
|
return accum;
|
|
324
328
|
}, {});
|
|
325
329
|
if (model[PersistenceKeys.METADATA]) {
|
|
330
|
+
// TODO movo to couchdb
|
|
326
331
|
log.silly(`Passing along persistence metadata for ${model[PersistenceKeys.METADATA]}`);
|
|
327
332
|
Object.defineProperty(result, PersistenceKeys.METADATA, {
|
|
328
333
|
enumerable: false,
|
|
329
|
-
writable:
|
|
334
|
+
writable: true,
|
|
330
335
|
configurable: true,
|
|
331
336
|
value: model[PersistenceKeys.METADATA],
|
|
332
337
|
});
|
|
333
338
|
}
|
|
334
339
|
return {
|
|
335
340
|
record: result,
|
|
336
|
-
id: model[pk],
|
|
341
|
+
id: model[Model.pk(model.constructor)],
|
|
337
342
|
transient: split.transient,
|
|
338
343
|
};
|
|
339
344
|
}
|
|
@@ -343,29 +348,29 @@ export class Adapter extends LoggedClass {
|
|
|
343
348
|
* and reattaching transient properties
|
|
344
349
|
* @template M - The model type
|
|
345
350
|
* @param obj - The database record
|
|
346
|
-
* @param {
|
|
351
|
+
* @param {Constructor<M>} clazz - The model class or name
|
|
347
352
|
* @param pk - The primary key property name
|
|
348
353
|
* @param {string|number|bigint} id - The primary key value
|
|
349
354
|
* @param [transient] - Transient properties to reattach
|
|
350
355
|
* @param [args] - options args for subclassing purposes
|
|
351
356
|
* @return {M} The reconstructed model instance
|
|
352
357
|
*/
|
|
353
|
-
revert(obj, clazz,
|
|
354
|
-
|
|
355
|
-
...args) {
|
|
356
|
-
const log = this.log.for(this.revert);
|
|
358
|
+
revert(obj, clazz, id, transient, ...args) {
|
|
359
|
+
const { log, ctx } = this.logCtx(args, this.revert);
|
|
357
360
|
const ob = {};
|
|
361
|
+
const pk = Model.pk(clazz);
|
|
358
362
|
ob[pk] = id;
|
|
359
|
-
const m =
|
|
363
|
+
const m = new clazz(ob);
|
|
360
364
|
log.silly(`Rebuilding model ${m.constructor.name} id ${id}`);
|
|
361
|
-
const metadata = obj[PersistenceKeys.METADATA];
|
|
365
|
+
const metadata = obj[PersistenceKeys.METADATA]; // TODO move to couchdb
|
|
362
366
|
const result = Object.keys(m).reduce((accum, key) => {
|
|
363
367
|
if (key === pk)
|
|
364
368
|
return accum;
|
|
365
|
-
accum[key] =
|
|
369
|
+
accum[key] =
|
|
370
|
+
obj[Model.columnName(clazz, key)];
|
|
366
371
|
return accum;
|
|
367
372
|
}, m);
|
|
368
|
-
if (transient) {
|
|
373
|
+
if (ctx.get("rebuildWithTransient") && transient) {
|
|
369
374
|
log.verbose(`re-adding transient properties: ${Object.keys(transient).join(", ")}`);
|
|
370
375
|
Object.entries(transient).forEach(([key, val]) => {
|
|
371
376
|
if (key in result)
|
|
@@ -374,11 +379,12 @@ export class Adapter extends LoggedClass {
|
|
|
374
379
|
});
|
|
375
380
|
}
|
|
376
381
|
if (metadata) {
|
|
382
|
+
// TODO move to couchdb
|
|
377
383
|
log.silly(`Passing along ${this.flavour} persistence metadata for ${m.constructor.name} id ${id}: ${metadata}`);
|
|
378
384
|
Object.defineProperty(result, PersistenceKeys.METADATA, {
|
|
379
385
|
enumerable: false,
|
|
380
|
-
configurable:
|
|
381
|
-
writable:
|
|
386
|
+
configurable: true,
|
|
387
|
+
writable: true,
|
|
382
388
|
value: metadata,
|
|
383
389
|
});
|
|
384
390
|
}
|
|
@@ -393,13 +399,13 @@ export class Adapter extends LoggedClass {
|
|
|
393
399
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
394
400
|
* @return A promise that resolves to an array of created records
|
|
395
401
|
*/
|
|
396
|
-
async createAll(
|
|
402
|
+
async createAll(clazz, id, model, ...args) {
|
|
397
403
|
if (id.length !== model.length)
|
|
398
404
|
throw new InternalError("Ids and models must have the same length");
|
|
399
|
-
const log = this.
|
|
400
|
-
|
|
401
|
-
log.debug(`
|
|
402
|
-
return Promise.all(id.map((i, count) => this.create(
|
|
405
|
+
const { log, ctxArgs } = this.logCtx(args, this.createAll);
|
|
406
|
+
const tableLabel = Model.tableName(clazz);
|
|
407
|
+
log.debug(`Creating ${id.length} entries ${tableLabel} table`);
|
|
408
|
+
return Promise.all(id.map((i, count) => this.create(clazz, i, model[count], ...ctxArgs)));
|
|
403
409
|
}
|
|
404
410
|
/**
|
|
405
411
|
* @description Retrieves multiple records from the database
|
|
@@ -409,28 +415,28 @@ export class Adapter extends LoggedClass {
|
|
|
409
415
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
410
416
|
* @return A promise that resolves to an array of retrieved records
|
|
411
417
|
*/
|
|
412
|
-
async readAll(
|
|
413
|
-
const log = this.
|
|
414
|
-
|
|
415
|
-
log.debug(`
|
|
416
|
-
return Promise.all(id.map((i) => this.read(
|
|
418
|
+
async readAll(clazz, id, ...args) {
|
|
419
|
+
const { log, ctxArgs } = this.logCtx(args, this.readAll);
|
|
420
|
+
const tableName = Model.tableName(clazz);
|
|
421
|
+
log.debug(`Reading ${id.length} entries ${tableName} table`);
|
|
422
|
+
return Promise.all(id.map((i) => this.read(clazz, i, ...ctxArgs)));
|
|
417
423
|
}
|
|
418
424
|
/**
|
|
419
425
|
* @description Updates multiple records in the database
|
|
420
426
|
* @summary Modifies multiple existing records with the given IDs in the specified table
|
|
421
|
-
* @param {
|
|
427
|
+
* @param {Constructor<M>} tableName - The name of the table to update
|
|
422
428
|
* @param {string[]|number[]} id - The identifiers of the records to update
|
|
423
429
|
* @param model - The new data for each record
|
|
424
430
|
* @param {...any[]} args - Additional arguments specific to the adapter implementation
|
|
425
431
|
* @return A promise that resolves to an array of updated records
|
|
426
432
|
*/
|
|
427
|
-
async updateAll(
|
|
433
|
+
async updateAll(clazz, id, model, ...args) {
|
|
428
434
|
if (id.length !== model.length)
|
|
429
435
|
throw new InternalError("Ids and models must have the same length");
|
|
430
|
-
const log = this.
|
|
431
|
-
|
|
432
|
-
log.debug(`
|
|
433
|
-
return Promise.all(id.map((i, count) => this.update(
|
|
436
|
+
const { log, ctxArgs } = this.logCtx(args, this.updateAll);
|
|
437
|
+
const tableLabel = Model.tableName(clazz);
|
|
438
|
+
log.debug(`Updating ${id.length} entries ${tableLabel} table`);
|
|
439
|
+
return Promise.all(id.map((i, count) => this.update(clazz, i, model[count], ...ctxArgs)));
|
|
434
440
|
}
|
|
435
441
|
/**
|
|
436
442
|
* @description Deletes multiple records from the database
|
|
@@ -441,10 +447,9 @@ export class Adapter extends LoggedClass {
|
|
|
441
447
|
* @return A promise that resolves to an array of deleted records
|
|
442
448
|
*/
|
|
443
449
|
async deleteAll(tableName, id, ...args) {
|
|
444
|
-
const log =
|
|
445
|
-
log.verbose(`Deleting ${id.length} entries ${tableName} table`);
|
|
446
|
-
|
|
447
|
-
return Promise.all(id.map((i) => this.delete(tableName, i, ...args)));
|
|
450
|
+
const { log, ctxArgs } = Adapter.logCtx(args, this.deleteAll);
|
|
451
|
+
log.verbose(`Deleting ${id.length} entries from ${tableName} table`);
|
|
452
|
+
return Promise.all(id.map((i) => this.delete(tableName, i, ...ctxArgs)));
|
|
448
453
|
}
|
|
449
454
|
/**
|
|
450
455
|
* @description Registers an observer for database events
|
|
@@ -461,11 +466,10 @@ export class Adapter extends LoggedClass {
|
|
|
461
466
|
writable: false,
|
|
462
467
|
});
|
|
463
468
|
this.observerHandler.observe(observer, filter);
|
|
464
|
-
this.log
|
|
465
|
-
|
|
466
|
-
.verbose(`Registering new observer ${observer.toString()}`);
|
|
469
|
+
const log = this.log.for(this.observe);
|
|
470
|
+
log.verbose(`Registering new observer ${observer.toString()}`);
|
|
467
471
|
if (!this.dispatch) {
|
|
468
|
-
|
|
472
|
+
log.info(`Creating dispatch for ${this.alias}`);
|
|
469
473
|
this.dispatch = this.Dispatch();
|
|
470
474
|
this.dispatch.observe(this);
|
|
471
475
|
}
|
|
@@ -497,9 +501,9 @@ export class Adapter extends LoggedClass {
|
|
|
497
501
|
async updateObservers(table, event, id, ...args) {
|
|
498
502
|
if (!this.observerHandler)
|
|
499
503
|
throw new InternalError("ObserverHandler not initialized. Did you register any observables?");
|
|
500
|
-
const log =
|
|
501
|
-
log.verbose(`Updating ${this.observerHandler.count()} observers for adapter ${this.alias}`);
|
|
502
|
-
await this.observerHandler.updateObservers(
|
|
504
|
+
const { log, ctxArgs } = Adapter.logCtx(args, this.updateObservers);
|
|
505
|
+
log.verbose(`Updating ${this.observerHandler.count()} observers for adapter ${this.alias}: Event: `);
|
|
506
|
+
await this.observerHandler.updateObservers(table, event, id, ...ctxArgs);
|
|
503
507
|
}
|
|
504
508
|
/**
|
|
505
509
|
* @description Refreshes data based on a database event
|
|
@@ -519,7 +523,7 @@ export class Adapter extends LoggedClass {
|
|
|
519
523
|
* @return {string} A string representation of the adapter
|
|
520
524
|
*/
|
|
521
525
|
toString() {
|
|
522
|
-
return `${this.flavour}
|
|
526
|
+
return `${this.flavour} adapter`;
|
|
523
527
|
}
|
|
524
528
|
/**
|
|
525
529
|
* @description Gets the adapter flavor associated with a model
|
|
@@ -550,10 +554,9 @@ export class Adapter extends LoggedClass {
|
|
|
550
554
|
* @template CONF - The database driver config
|
|
551
555
|
* @template CONN - The database driver instance
|
|
552
556
|
* @template QUERY - The query type
|
|
553
|
-
* @template
|
|
554
|
-
* @template FLAGS - The repository flags type
|
|
557
|
+
* @template CONTEXT - The context type
|
|
555
558
|
* @param {string} flavour - The flavor name of the adapter to retrieve
|
|
556
|
-
* @return {Adapter<CONF, CONN, QUERY, CONTEXT
|
|
559
|
+
* @return {Adapter<CONF, CONN, QUERY, CONTEXT> | undefined} The adapter instance or undefined if not found
|
|
557
560
|
*/
|
|
558
561
|
static get(flavour) {
|
|
559
562
|
if (!flavour)
|
|
@@ -587,6 +590,9 @@ export class Adapter extends LoggedClass {
|
|
|
587
590
|
}
|
|
588
591
|
}
|
|
589
592
|
static decoration() { }
|
|
593
|
+
static logCtx(args, method) {
|
|
594
|
+
return super.logCtx(args, method);
|
|
595
|
+
}
|
|
590
596
|
get client() {
|
|
591
597
|
if (!this._client) {
|
|
592
598
|
this._client = this.getClient();
|
|
@@ -626,12 +632,21 @@ export class Adapter extends LoggedClass {
|
|
|
626
632
|
migrations() {
|
|
627
633
|
return Metadata.migrationsFor(this);
|
|
628
634
|
}
|
|
629
|
-
async
|
|
635
|
+
async getQueryRunner() {
|
|
636
|
+
return this;
|
|
637
|
+
}
|
|
638
|
+
async migrate(migrations = this.migrations(), ...args) {
|
|
639
|
+
if (migrations instanceof Context) {
|
|
640
|
+
args = [migrations];
|
|
641
|
+
migrations = this.migrations();
|
|
642
|
+
}
|
|
643
|
+
const { ctx } = Adapter.logCtx(args, this.migrate);
|
|
644
|
+
const qr = await this.getQueryRunner();
|
|
630
645
|
for (const migration of migrations) {
|
|
631
646
|
try {
|
|
632
647
|
const m = new migration();
|
|
633
|
-
await m.up(
|
|
634
|
-
await m.down(
|
|
648
|
+
await m.up(qr, this, ctx);
|
|
649
|
+
await m.down(qr, this, ctx);
|
|
635
650
|
}
|
|
636
651
|
catch (e) {
|
|
637
652
|
throw new MigrationError(e);
|