B
B
bqio2019-10-19 06:04:54
MySQL
bqio, 2019-10-19 06:04:54

What is the best way to use a database in nodejs?

I am making a project on nw.js + vue
How to correctly implement the work of a database (for example, Mysql)? Make a connection once, make requests while the application is running, and close the connection to the database when the application ends? Or at each request to open new connection and to close after reception of the data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2019-10-19
@bqio

Keeping 1 connection is more correct. But not always enough. And sooner or later it will fall off and you will need to reconnect, since the database has a limit on an idle connection. You can read about connection pools in Node.js, how to manage and use them.
You don't have to think about it and use query-builders, which do everything they need to do with the connection. I prefer Knex - very easy to learn and easy to use. Supports callbacks, then and async/await.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question