0

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?

1 Answer 1

1

It would be better to turn transaction callback into async to make it look like sequential code:

try {
  const createdUser = await sequelize.transaction(async t => {
    const user = await User.create({
      firstName: 'Abraham',
      lastName: 'Lincoln'
    }, {transaction: t});
    await user.setShooter({
        firstName: 'John',
        lastName: 'Boothe'
      }, {transaction: t});
    })
  });
  // transaction committed
  .. other code
} catch (err) {
  // transaction rolled back
}

Additional example with a cycle:

await sequelize.transaction(async t => {
  for(const clothing of data.clothes) {
     await this.clothingModel.update(
       { price: clothing.price },
       { where: { id: clothing.clothingId },
         transaction: t
       }
     );
  }
  // you can move this `update` outside the callback
  // if you wish to execute it out of transaction
  this.reservationModel.update(
    { dropoffStatus: 'APPROVED' },
    { where: { id: data.reservationId },
      transaction: t
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I think this is more of a suggestion/proposal than an actual answer. I will give you an upvote, but it does not solve my problem
As I understood you wish to do several changes in the same transaction. If not please clarify a bit in your question what you're trying to achieve
I would like to use the data.clothes array for the queries inside of the transaction
I added an example with a cycle to my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.