【发布时间】:2021-03-09 20:10:51
【问题描述】:
我想将clothingModel 上的更新放在一个事务中,如果它提交则更新reservationModel。
这是我尝试使用sequelize.transaction重写的代码
try {
data.clothes.forEach(async (clothing) => {
await this.clothingModel.update(
{ price: clothing.price },
{ where: { id: clothing.clothingId } }
);
});
} catch (e) {
//throw exception
}
//if exception is not thrown
this.reservationModel.update(
{ dropoffStatus: 'APPROVED' },
{ where: { id: data.reservationId } }
);
但我一直在努力使其符合 sequelize 中使用事务的方式
sequelize.transaction(function (t) {
return User.create({
firstName: 'Abraham',
lastName: 'Lincoln'
}, {transaction: t}).then(function (user) {
return user.setShooter({
firstName: 'John',
lastName: 'Boothe'
}, {transaction: t});
});
}).then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback
});
有可能吗?如果有,怎么做?
【问题讨论】:
标签: node.js typescript sequelize.js