DEV Community

Cover image for Coding Unmanaged and Managed Transactions with NodeJS, Express and Sequelize
Luiz Calaça
Luiz Calaça

Posted on

2 2

Coding Unmanaged and Managed Transactions with NodeJS, Express and Sequelize

Hi, Devs!

We have two types of transactions using Sequelize: unmanaged and managed transactions.


Wha is transaction on persistence layer?

A transaction is a small unit of a sotfware and it may contain several chunks that are tasks to be commited to the database just all together and could garantee Atomicity, Consistency, Isolation, and Durability.

Example: When we make a bank transfer transaction, we need it to be debited from our account and credited to another. You can't just do one or the other.

Transaction

Unmanaged Transactions

Unmanaged transactions means that committing and rolling back the transaction should be done manually by the user (by calling the appropriate Sequelize methods).


//Unmanaged transactions

const express = require('express')
const { User, Product } = require('../models')
const router = express.Router()

// ...
router.post('/userBook', (request, response) => {

const transaction = await sequelize.transaction()

try {
const { name, username, nameBook
description, price }= request.body;

const newBook = Book.create(
  { nameBook, description, price },
  { transaction: transaction }
);

const userProduct = Product.create(
  { name, userName, price, newBook.id },
  { transaction: transaction }
);

await transaction.commit()

response.status(200).json({message: 'Success'})
Enter fullscreen mode Exit fullscreen mode

} catch (error) {
await transaction.rollback()
response.status(500).json({ message: 'Wrong' })
}

});


## **Managed Transactions**
> [Managed transactions](https://sequelize.org/master/manual/transactions) handle committing or rolling back the transaction automatically. You start a managed transaction by passing a callback to sequelize.transaction

Enter fullscreen mode Exit fullscreen mode

// Managed Transactions

const express = require('express')
const { User, Product } = require('../models')
const router = express.Router()

// ...
router.post('/userBook', (request, response) => {
try {
const { name, username, nameBook
description, price }= request.body

await sequelize.transaction(async (t) => {

  const newUser = User.create({ name, username})

  const userProduct = Product.create(
  { nameBook, description, newUser.id })

  response.status(200).json({message: 'Success'})
  })
Enter fullscreen mode Exit fullscreen mode

} catch (error) {
response.status(500).json({ message: 'Wrong' })
}

})


See you soon!

**Contacts**
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay