I would like to put the updates on the clothingModel inside a transaction, and if it commits then update reservationModel.
This is the code I am trying to rewrite into using 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 } }
);
But I have struggled to make it conform to the way transactions is used in 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
});
Is it possible? If so, how?