update 문을 사용할 때 트랜잭션을 사용하는게 안전합니다.
const { sequelize } = require('../models');
const t = await sequelize.transaction();
try {
await Cherish.increment(
{ postpone_number: 1 },
{
where: {
id: cherish_id,
},
},
{ transaction: t }
);
await User.increment(
{ postpone_count: 1 },
{
where: {
id: user_id,
},
},
{ transaction: t }
);
await t.commit();
}
catch(err) {
await t.rollback();
}
트랜잭션 내 수행이 완료된다면 commit
을 해줍니다.
트랜잭션 내 수행 중 에러가 발생한다면 catch 부분에서 rollback
을 해줍니다.