Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
feat: get connection in hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Aug 1, 2020
1 parent 53fc704 commit df3a27c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
37 changes: 28 additions & 9 deletions src/lib/typeorm/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TypedService<T extends typeof BaseODataModel = any> extends ODataCo
}

protected async _getQueryRunner(ctx?: ODataHttpContext) {
return getOrCreateTransaction(getConnection(getConnectionName(this.constructor as typeof TypedService)), ctx);
return getOrCreateTransaction(getConnection(getConnectionName(this.constructor)), ctx);
}

protected async _getRepository(ctx?: ODataHttpContext): Promise<Repository<InstanceType<T>>> {
Expand All @@ -53,18 +53,39 @@ export class TypedService<T extends typeof BaseODataModel = any> extends ODataCo
if (ctx.entityType == undefined) {
ctx.entityType = this.elementType;
}
if (ctx.em == undefined) {
ctx.em = await this._getEntityManager(ctx.context);
}

if (ctx.hookType == undefined) {
throw new ServerInternalError('Hook Type must be specify by controller');
}
if (ctx.getService == undefined) {
ctx.getService = getEntityController;

if (ctx.getConnection == undefined) {
ctx.getConnection = () => getConnection(getConnectionName(this.constructor));
}

const isEvent = HookEvents.includes(ctx.hookType);

if (isEvent) {

if (ctx.getService == undefined) {
ctx.getService = () => {
throw new ServerInternalError('Not support get service in event hooks.');
};
}


} else {

if (ctx.getService == undefined) {
ctx.getService = getEntityController;
}

if (ctx.em == undefined) {
ctx.em = await this._getEntityManager(ctx.context);
}

}


const hooks = findHooks(this.elementType, ctx.hookType);

for (let idx = 0; idx < hooks.length; idx++) {
Expand All @@ -73,9 +94,7 @@ export class TypedService<T extends typeof BaseODataModel = any> extends ODataCo
if (isEvent) {
// is event, just trigger executor but not wait it finished
// @ts-ignore
hook.execute(ctx).catch((error) => {
// logger here
});
hook.execute(ctx).catch(console.error);
} else {
// is hook, wait them executed
// @ts-ignore
Expand Down
18 changes: 14 additions & 4 deletions src/lib/typeorm/hooks/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityManager } from 'typeorm';
import { Connection, EntityManager } from 'typeorm';
import { ODataHttpContext } from '../../server';
import { TypedService } from '../controller';
import { BaseODataModel } from '../model';
Expand All @@ -8,6 +8,7 @@ export interface HookContext<T = any> {
context: ODataHttpContext;
hookType: HookType;
entityType: typeof BaseODataModel;

/**
* data item for read/create/update
*/
Expand All @@ -22,14 +23,23 @@ export interface HookContext<T = any> {
key?: any;

/**
* (transaction) entity manager
* (transaction) entity manager,
* ONLY in sync hooks
*/
em: EntityManager;
em?: EntityManager;

/**
* get controller (service) instance for entity
*
* ONLY sync hooks
*/
getService?: <E extends typeof BaseODataModel>(entity: E) => TypedService<E>;


/**
* get root connection for server
*/
getService: <E extends typeof BaseODataModel>(entity: E) => TypedService<E>;
getConnection: () => Connection;

}

Expand Down

0 comments on commit df3a27c

Please sign in to comment.