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:
parent
846615937a
commit
a01b385d65
@ -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});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user