From 245bd01bd689c38b620959999697a67bbfd3b27e Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 23 Jun 2022 23:37:55 +0800 Subject: [PATCH] fix: missing transaction (#531) --- .../hooks/afterCreateWithAssociations.test.ts | 33 +++++++++++++++++++ .../multiple-relation-repository.ts | 10 ++++-- packages/core/database/src/repository.ts | 20 ++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 packages/core/database/src/__tests__/hooks/afterCreateWithAssociations.test.ts diff --git a/packages/core/database/src/__tests__/hooks/afterCreateWithAssociations.test.ts b/packages/core/database/src/__tests__/hooks/afterCreateWithAssociations.test.ts new file mode 100644 index 0000000000..e9bbef418e --- /dev/null +++ b/packages/core/database/src/__tests__/hooks/afterCreateWithAssociations.test.ts @@ -0,0 +1,33 @@ +import { mockDatabase } from '../'; +import { Database } from '../../database'; + +describe('afterCreateWithAssociations', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase(); + }); + + afterEach(async () => { + await db.close(); + }); + + test('case 1', async () => { + db.collection({ + name: 'test', + }); + await db.sync(); + const repo = db.getRepository('test'); + db.on('test.afterCreateWithAssociations', async (model, { transaction }) => { + throw new Error('test error'); + }); + try { + await repo.create({ + values: {}, + }); + } catch (error) { + } + const count = await repo.count(); + expect(count).toBe(0); + }); +}); diff --git a/packages/core/database/src/relation-repository/multiple-relation-repository.ts b/packages/core/database/src/relation-repository/multiple-relation-repository.ts index 175672cb7e..a055cc32ba 100644 --- a/packages/core/database/src/relation-repository/multiple-relation-repository.ts +++ b/packages/core/database/src/relation-repository/multiple-relation-repository.ts @@ -165,8 +165,14 @@ export abstract class MultipleRelationRepository extends RelationRepository { for (const instance of instances) { if (options.hooks !== false) { - await this.db.emitAsync(`${this.targetCollection.name}.afterUpdateWithAssociations`, instance, {...options, transaction}); - await this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, {...options, transaction}); + await this.db.emitAsync(`${this.targetCollection.name}.afterUpdateWithAssociations`, instance, { + ...options, + transaction, + }); + await this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, { + ...options, + transaction, + }); } } diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index ba15f1e4f1..09aa4dcfae 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -307,8 +307,14 @@ export class Repository