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

Commit

Permalink
fix(hook): transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 29, 2020
1 parent 1dfc93d commit 4f1e7ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ module.exports = {

'space-before-blocks': ['error', 'always'],

'space-before-function-paren': ['error', 'never'],

'space-in-parens': ['error', 'never'],

'space-unary-ops': ['error', {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/typeorm/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat
* @param runner transaction runner
*/
protected async _tx<X>(runner: TXRunner<T, X>): Promise<X> {
return new Promise(async(resolve, reject) => {
return new Promise(async (resolve, reject) => {
try {
await this._getConnection().transaction(async(em) => {
await this._getConnection().transaction(async (em) => {
const repo = em.getRepository(this.elementType);
// @ts-ignore
resolve(await runner(repo, em));
Expand All @@ -55,7 +55,7 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat
ctx.entityType = this.elementType;
}
if (ctx.em == undefined) {
ctx.em = this._getConnection().manager;
throw new ServerInternalError('Please set parameter [em] (transaction entity manager) in controller method');
}
if (ctx.hookType == undefined) {
throw new ServerInternalError('Hook Type must be specify by controller');
Expand Down Expand Up @@ -86,7 +86,7 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat

@odata.GET
async findOne(@odata.key key, @odata.context ctx: ODataHttpContext) {
return this._tx(async(repo, em) => {
return this._tx(async (repo, em) => {
const data = await repo.findOne(key);
await this._executeHooks({
context: ctx, hookType: HookType.afterLoad, data, em, entityType: this.elementType
Expand All @@ -97,7 +97,7 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat

@odata.GET
async find(@odata.query query, @odata.context ctx: ODataHttpContext) {
return this._tx(async(repo, em) => {
return this._tx(async (repo, em) => {
const conn = em.connection;
let data = [];

Expand Down Expand Up @@ -129,26 +129,26 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat

@odata.POST
async create(@odata.body body, @odata.context ctx: ODataHttpContext) {
return this._tx(async(repo) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeCreate, data: body });
return this._tx(async (repo, em) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeCreate, data: body, em });
return repo.save(body);
});
}

// odata patch will response no content
@odata.PATCH
async update(@odata.key key, @odata.body body, @odata.context ctx: ODataHttpContext) {
return this._tx(async(repo) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeUpdate, data: body, key });
return this._tx(async (repo, em) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeUpdate, data: body, key, em });
return repo.update(key, body);
});
}

// odata delete will response no content
@odata.DELETE
async delete(@odata.key key, @odata.context ctx: ODataHttpContext) {
return this._tx(async(repo) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeDelete, key });
return this._tx(async (repo, em) => {
await this._executeHooks({ context: ctx, hookType: HookType.beforeDelete, key, em });
return repo.delete(key);
});
}
Expand Down

0 comments on commit 4f1e7ea

Please sign in to comment.