fix: missing transaction (#531)

This commit is contained in:
chenos 2022-06-23 23:37:55 +08:00 committed by GitHub
parent 33a08288c0
commit 245bd01bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 6 deletions

View File

@ -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);
});
});

View File

@ -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,
});
}
}

View File

@ -307,8 +307,14 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
});
if (options.hooks !== false) {
await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, options);
await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, {
...options,
transaction,
});
await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, {
...options,
transaction,
});
}
return instance;
@ -362,8 +368,14 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
if (options.hooks !== false) {
for (const instance of instances) {
await this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, options);
await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
await this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, {
...options,
transaction,
});
await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, {
...options,
transaction,
});
}
}