this is more of a basic question, trying to clear up my understanding…
usually if I have a simple program such as below, it creates a db connection and then closes it once the script exits
import Database from 'better-sqlite3';
const db = new Database('./path/to/db.sqlite');
function foo(db) {
return db.prepare('SELECT * FROM table').all();
}
console.log(foo(db));
but what actually happens when I am running a nodejs
REST server that starts up and then may run for days, weeks, months, without shutting it down? Is a new db connection created every time the user makes a query and then the connection is closed when the results are sent back? For example, using fastify
fastify.get('/', async (request, reply) => {
return foo(db)
})
would it be better to create a connection when node
starts so it is not recreated everytime a user hits the server? Very likely I don't really understand how this works hence my question.
Note, sqlite3 + node: when to close db? is a similar question but not quite what I am asking.