user2741831
user2741831

Reputation: 2398

Sequelize hits database with new query for every transaction command

I have followed this tutorial for sequelize transactions:

https://sequelize.org/master/manual/transactions.html

It does work, but I have noticed that for every command in that code sequelize sends a query to the database server and waits for a response.

The reasons this is a problem is because my database server is not really close to my node server, so every round trip can be expensive.

This seems extremely wastefull. Since all sequelize actually does is turn js code into MySQL syntax, wouldn't it be more efficient to just construct the query and then send it all at once to the database? Or at least, avoid sending a query when sequelize.transaction() is called and send "Start Transaction" on the subsequent command. So basically is there a way to create a transaction with multiple lines of query code and then send that all at once to the database?

Upvotes: 0

Views: 399

Answers (1)

hellikiam
hellikiam

Reputation: 415

Unfortunately, what you wanna do seems like anti-pattern of ORM, which includes sequelize.js. ORM is functional way to manage database connection not the query itself. You might wanna use query builer instead. check this out. http://knexjs.org/

Difference between query builder and ORM

  • ORM provides you the connection with database and you can run the query using APIs. Query is black-boxed to the user in most of cases.

  • Query builder API doesn't run the query until the user put the generated query into database connection.


Also, transaction is to be used rollback and commit for keeping same context(google atomicity) and can't put the queries together for relieving the network workload.

Upvotes: 1

Related Questions