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 000000000..e9bbef418 --- /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 175672cb7..a055cc32b 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 ba15f1e4f..09aa4dcfa 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -307,8 +307,14 @@ export class Repository