Skip to content

Commit

Permalink
feat(base-service): allow manager option to be passed directly
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 committed Sep 19, 2021
1 parent 88d1eac commit 0e81c61
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/core/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -404,8 +406,22 @@ export class BaseService<E extends Node> {
return items[0];
}

async create(data: DeepPartial<E>, userId: string, options?: BaseOptions): Promise<E> {
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<E>, userId: string, options?: BaseOptionsExtended): Promise<E> {
const manager = this.extractManager(options);
const createdByIdObject: WarthogSpecialModel = this.hasColumn('createdById')
? { createdById: userId }
: {};
Expand All @@ -431,7 +447,7 @@ export class BaseService<E extends Node> {
}

async createMany(data: DeepPartial<E>[], userId: string, options?: BaseOptions): Promise<E[]> {
const manager = options?.manager ?? this.manager;
const manager = this.extractManager(options);
const createdByIdObject: WarthogSpecialModel = this.hasColumn('createdById')
? { createdById: userId }
: {};
Expand Down Expand Up @@ -471,7 +487,7 @@ export class BaseService<E extends Node> {
userId: string,
options?: BaseOptions
): Promise<E> {
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 }
Expand All @@ -490,7 +506,7 @@ export class BaseService<E extends Node> {
}

async delete(where: any, userId: string, options?: BaseOptions): Promise<StandardDeleteResponse> {
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
Expand Down

0 comments on commit 0e81c61

Please sign in to comment.