Should you create an instance of the database connection outside the scope of route handlers, or create one for each handler (e.g., app.get('/todos')
)?
Correct me if I'm wrong, but I'd guess the first approach is better because it can reuse the same connection. However, I've seen other examples that creates one for each route handler.
What's the recommended approach? Or does it matter due to how the database can cache connections on its end?
EDIT: I may be confused about how database drivers connect. Most provide a method such as connectDB(config)
where you specify the location of the resource, authentication, etc. Is this actually making a connection or does it initiate the connection when you actually request something with that database object?