I am trying to initialize a mongo Singleton connection with expressjs in the app/server.js file, in an async manner. I want to use the native driver and not Mongoose or something similar.
The whole internet is filled with bad patterns (IMHO) that open a new connection with each request.
Has anyone tried and succeeded in the former? Any link or advise will be appreciated.
For example
// app.js
let client = await initMongoConnection()
What I do is initialize the database connection, plus any other asynchronous initialization I need to do, and then bootstrap the entire application and send the connection to any component that needs it.
So basically what your snippet is hinting at:
You can check this project I built a while ago for a complete example.