Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // First, we start a transaction from your connection and save it into a variable
- const t = await sequelize.transaction();
- try {
- // Then, we do some calls passing this transaction as an option:
- const user = await User.create(
- {
- firstName: 'Bart',
- lastName: 'Simpson',
- },
- { transaction: t },
- );
- await user.addSibling(
- {
- firstName: 'Lisa',
- lastName: 'Simpson',
- },
- { transaction: t },
- );
- // If the execution reaches this line, no errors were thrown.
- // We commit the transaction.
- await t.commit();
- } catch (error) {
- // If the execution reaches this line, an error was thrown.
- // We rollback the transaction.
- await t.rollback();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement