fix(database): fix type and transaction in repository (#366)

* fix(database): fix type and transaction in repository

* fix: transaction

* fix: transaction

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
Junyi 2022-05-08 12:47:08 +08:00 committed by GitHub
parent 846615937a
commit a01b385d65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

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

View File

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

View File

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