fix: call rollback in catch
This commit is contained in:
parent
a0fc7a17d1
commit
0ef730d7e4
@ -255,21 +255,26 @@ export async function create(ctx: Context, next: Next) {
|
|||||||
const transaction = await ctx.db.sequelize.transaction();
|
const transaction = await ctx.db.sequelize.transaction();
|
||||||
const options = { transaction, context: ctx };
|
const options = { transaction, context: ctx };
|
||||||
let model: Model;
|
let model: Model;
|
||||||
if (associated && resourceField) {
|
try {
|
||||||
const AssociatedModel = ctx.db.getModel(associatedName);
|
if (associated && resourceField) {
|
||||||
if (!(associated instanceof AssociatedModel)) {
|
const AssociatedModel = ctx.db.getModel(associatedName);
|
||||||
throw new Error(`${associatedName} associated model invalid`);
|
if (!(associated instanceof AssociatedModel)) {
|
||||||
|
throw new Error(`${associatedName} associated model invalid`);
|
||||||
|
}
|
||||||
|
const { create } = resourceField.getAccessors();
|
||||||
|
model = await associated[create](values, options);
|
||||||
|
} else {
|
||||||
|
const ResourceModel = ctx.db.getModel(resourceName);
|
||||||
|
model = await ResourceModel.create(values, options);
|
||||||
}
|
}
|
||||||
const { create } = resourceField.getAccessors();
|
await model.updateAssociations(values, options);
|
||||||
model = await associated[create](values, options);
|
await transaction.commit();
|
||||||
} else {
|
ctx.body = model;
|
||||||
const ResourceModel = ctx.db.getModel(resourceName);
|
await next();
|
||||||
model = await ResourceModel.create(values, options);
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
await model.updateAssociations(values, options);
|
|
||||||
await transaction.commit();
|
|
||||||
ctx.body = model;
|
|
||||||
await next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user