How to control transactions in Node.js?

Asked

Viewed 645 times

1

I’m starting in Node.js and looked a lot about transaction control, but I haven’t found anything like what I’m used to in Java.

First I’ll clarify what I mean by transaction control: Imagine a complex operation where business logic is separated into 2 or more services (or modules or files, etc.), each service interacts with the bank and can make changes. If any error occurs in any of the services, everything must be undone (rollback) so that no data is inconsistent in the database.

A classic example of where transaction control is important, is a bank transfer, two transactions are required, debit on one account and credit on another, either both occur successfully or none can be performed.

In java, frameworks like Spring support transaction control in a very simple and transparent way for the developer, that is, by making the correct settings, no need to worry about it anymore, the framework does all the control: Start the transaction and commit or roolback as needed, even when logic is distributed across multiple services.

I looked for some framework that did the same in Node.js, but the closest I found were the 'Promises', which basically allow to execute several database operations in an orderly manner and in case of error perform the rollback, however when logic is distributed across multiple services Promises are not enough.

The question is: is there any framework or technique for Node.js capable of doing this control or simply Node.js is not a good option for applications that need transactional control?

I am using Mysql database and lib mysql2 (https://github.com/sidorares/node-mysql2)

  • 1

    found this project: https://www.npmjs.com/package/node-bemis, if useful.

  • Node-Emis even controls transactions, but manually, it’s similar to what Promises offer...

  • which database you are using?

  • This will depend on the library used to perform the queries, and consequently the database used... updates the question with this information that will be easier for someone to answer

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.