I am developing a Node.js application. It is made up of two modules: the first one with all the main operations, and the second one (which is an helper module) where I store the CRUD operations for my MongoDB database. On the first module I call the second module methods (e.g. second-module-method inside first-module-method).
When should I open and close the connection in the helper module? Should I either call MongoClient.connect()
and db.close()
in each method or connect to the database at the top of the .js file and never close the connection (or do anything else)?
My application may never stop running (or stop just for a few time). So should I just open the connection and never close it?