The process of connecting to a mongodb database can some times be a little complicated. When connecting locally the hostname and port might not be of much interest, if the mongod service is running on the default port, and there are no issues with using localhost as the hostname. However the situation can become very different when it comes to deployment, where not only does the hostname and port matter, but there is often a username and password that need to be specified as well in order to connect to and use a database. As such there seems to be a need to create, and maintain a module, that can be used to quickly connect depending on the environment. In many cases this module may need to be custom trailered depending on the environment, or at the very least bust be able to accept arguments, or look for environment variables.
So for a project in which I am going to be using mongodb as a database solution, and mongoose as a preferred mongodb client, I thought that it might be a good idea to pull everything that has to do with connecting to mongoose into a single js file that is the index.js file of a stand alone module that I might publish to npm, or a js file that I just store in a lib folder of some kind that is just part of a projects source code.
In other worlds something like this:
When using this I have the option to use callbacks, or promises. In the event that all goes well it will give me a reference to mongoose that I can then use so I do not have to bring it in with require each time. With some projects it might be necessary to hack over this a little, but in any case I give the option to just directly set the connection string if need be.