diff --git a/src/core/BaseService.ts b/src/core/BaseService.ts index 470f66a2..7a8fccc3 100644 --- a/src/core/BaseService.ts +++ b/src/core/BaseService.ts @@ -28,6 +28,8 @@ export interface BaseOptions { manager?: EntityManager; // Allows consumers to pass in a TransactionManager } +export type BaseOptionsExtended = BaseOptions | EntityManager; + export interface Node { id?: string | number; getValue(field: string): string | number; @@ -404,8 +406,22 @@ export class BaseService { return items[0]; } - async create(data: DeepPartial, userId: string, options?: BaseOptions): Promise { - const manager = options?.manager ?? this.manager; + extractManager(options?: BaseOptionsExtended) { + if (!options) { + return this.manager; + } + if (options instanceof EntityManager) { + return options; + } + if (options.manager && options.manager instanceof EntityManager) { + return options.manager; + } + + return this.manager; + } + + async create(data: DeepPartial, userId: string, options?: BaseOptionsExtended): Promise { + const manager = this.extractManager(options); const createdByIdObject: WarthogSpecialModel = this.hasColumn('createdById') ? { createdById: userId } : {}; @@ -431,7 +447,7 @@ export class BaseService { } async createMany(data: DeepPartial[], userId: string, options?: BaseOptions): Promise { - const manager = options?.manager ?? this.manager; + const manager = this.extractManager(options); const createdByIdObject: WarthogSpecialModel = this.hasColumn('createdById') ? { createdById: userId } : {}; @@ -471,7 +487,7 @@ export class BaseService { userId: string, options?: BaseOptions ): Promise { - const manager = options?.manager ?? this.manager; + const manager = this.extractManager(options); const found = await this.findOne(where); const updatedByIdObject: WarthogSpecialModel = this.hasColumn('updatedById') ? { updatedById: userId } @@ -490,7 +506,7 @@ export class BaseService { } async delete(where: any, userId: string, options?: BaseOptions): Promise { - const manager = options?.manager ?? this.manager; + const manager = this.extractManager(options); // V3: TODO: we shouldn't look for the column name, we should see if they've decorated the // model with a DeletedDate decorator