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 fd808114f..137999b2a 100644 --- a/packages/core/database/src/relation-repository/multiple-relation-repository.ts +++ b/packages/core/database/src/relation-repository/multiple-relation-repository.ts @@ -150,7 +150,10 @@ export abstract class MultipleRelationRepository extends RelationRepository { const queryOptions = this.buildQueryOptions(options as any); - const instances = await this.find(queryOptions); + const instances = await this.find({ + ...queryOptions, + transaction, + }); for (const instance of instances) { await updateModelByValues(instance, values, { @@ -163,8 +166,8 @@ 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); - await this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, options); + 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/relation-repository/relation-repository.ts b/packages/core/database/src/relation-repository/relation-repository.ts index 49ddb3a55..51d63314c 100644 --- a/packages/core/database/src/relation-repository/relation-repository.ts +++ b/packages/core/database/src/relation-repository/relation-repository.ts @@ -53,28 +53,33 @@ export abstract class RelationRepository { const guard = UpdateGuard.fromOptions(this.targetModel, options); const values = options.values; + const transaction = await this.getTransaction(options); - const sourceModel = await this.getSourceModel(); + const sourceModel = await this.getSourceModel(transaction); - const instance = await sourceModel[createAccessor](guard.sanitize(options.values), options); + const instance = await sourceModel[createAccessor](guard.sanitize(options.values), { ...options, transaction }); - await updateAssociations(instance, values, options); + await updateAssociations(instance, values, { ...options, transaction }); if (options.hooks !== false) { - await this.db.emitAsync(`${this.targetCollection.name}.afterCreateWithAssociations`, instance, options); + await this.db.emitAsync(`${this.targetCollection.name}.afterCreateWithAssociations`, instance, { + ...options, + transaction, + }); const eventName = `${this.targetCollection.name}.afterSaveWithAssociations`; - await this.db.emitAsync(eventName, instance, options); + await this.db.emitAsync(eventName, instance, { ...options, transaction }); } return instance; } - async getSourceModel(transaction?: any) { + async getSourceModel(transaction?: Transaction) { if (!this.sourceInstance) { this.sourceInstance = await this.sourceCollection.model.findOne({ where: { [this.associationField.sourceKey]: this.sourceKeyValue, }, + transaction, }); } diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index 5d98d7aec..d9b80f887 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -68,7 +68,7 @@ export interface FilterByTk { export interface FindOptions extends SequelizeFindOptions, CommonFindOptions, FilterByTk {} -export interface CommonFindOptions { +export interface CommonFindOptions extends TransactionAble { filter?: Filter; fields?: Fields; appends?: Appends;