MongoDB connections on require or by function - node.js

I am working on a single pager that writes to different mongodb databases through an API setup with express. To do this I have one file named db.js that is doing all of the work with the mongoose module and then exporting the two connections to my express file called app.js.
When I start running my app file with node, my mongo console shows the two connections being made.
My question is, should I be making the exports structured so that they are functions that only connect to the DB when the functions themselves are called? Is there anything bad about leaving the two connections open and waiting for people to use them?

It is better to have your database connections created on start of the application, and leaving them open. The other way to create connections when an API call is made is extremely inefficient, because the connection load increases wrt number of API calls, and also because the response time of the API increases.
In db.js export your objects.
In your app.js, you can directly require the appropriate connection and start using it
var db = require("../db").first;
db.find({}, function (err, res) {})

Related

Switch DB dynamically for NodeJS web application

I am trying to implement a feature which a user can decide on login to which DB to connect. As it is a web-app, running on a server which all the clients approach, how can I implement this feature without changing every client DB?
At our company we are using mongoose as the MongoDB API.
I read all the docs, and didn't notice any functionality for using multiple connections to different DB's on different hosts within the same App at once - without damaging other's client work.
The most valuable thing I have accomplished is to open few connections based on multiple mongoose instances, based on this post:
Mongoose and multiple database in single node.js project
I have created few files for example:
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://10.20.100.71:27017/DB_NAME');
module.exports = exports = mongoose;
And then I required them:
let stageAccess = require('./databsesConnections/stageAccess');
let prodAccess = require('./databsesConnections/prodAccess');
I debugged the files and checked the connections are establishing.
Further more I checked in the mongoose docs and concluded that I can choose which connection is the default connection, as the docs state:
"Mongoose creates a default connection when you call mongoose.connect(). You can access the default connection using mongoose.connection."
So I tried:
mongoose.connection = mongoose.connections[1];
And it works fine.
So the actual question is, what will happen if client 1 approach the app, select to connect dbNum1 and starts to work,
then client 2 approach the app and select to connect to dbNum2?

expressJS exporting other things in addition to app

In my web application (I'm using expressJS), there are many services (such as mongoDB connection, MQTT connection, etc.) that need to be executed once the whole application is executed (using npm start command). Therefore, I can make use of these services in my entire application. For example, I want to use my MQTT connection in different files.
My idea is to export the MQTT connection, MongoDB connection, etc. in addition to the app this way:
//app.js
module.exports = {
app: app,
mqttConnection: myMQTTConnection,
db: myMongoDB
};
However, we know that this approach doesn't work (I tested it and got an error saying: TypeError: app.set is not a function).
How can I export other things in addition to app from app.js file?
If my approach is not possible, what other approaches can I use? (considering the fact that many services (such as connecting to a server, etc.) are asynchronous)

Closing mongodb connections

I use native mongodb driver with Expressjs, I don't want to open and close connections on a regular basis in my routes. I want to open once use one connection in all my next() functions and then close when done.
I saw that after opening a connection I can pass the db object to next() functions in request object and use it.
When I try to close the connection like var db=req.db; db.close(); it throws an error.
Inorder to solve this issue I have decided to use settimeout function to close my connection.
I can pass around and use the db obj and send response and the after 1-2 seconds db obj is closed by settimeout.
I'm worried if the requests are more will the settimeout functions effect the servers performance. If I use this trick to manage my db connections.
Sorry, I forgot to do:
db=client.db('test')
and:
db.close();
Change in later versions can't directly open db without getting the client, using native mongodb-nodejs driver.

Connect only once to Mongo

UPDATE: While building an example, I found that the culprit seems to be restify-enroute. The package is messing with mongoose connection. I published the example to https://github.com/HeavyStorm/mongoose-enroute-conflict.
I had understood that mongoose.connect was how to establish a connection between mongoose and Mongo, and supposed that this was a singleton connection of sorts - once called, every module in my app would be able to call Mongo.
This is probably wrong.
My current scenario:
I'm calling connect:
mongoose.connect("mongodb://localhost/test");
My apps structure:
/
/app.js
/modules
./users
./module.js
In app.js I call mongoose.connect. I also load and expose a route from module.js. When I receive a http call, module.js code kicks in (I can see that from the debugger), but as soon as I call mongo (through Model.find() in this case), well, code skips, my callbacks are never called in the client is held on a waiting state.
However, if I add the mongoose.connect line to module.js, the Model.find() yields to the callback almost instantly and the client's receives the response.
TLDR: Do I have to call mongoose.connect on every module that access the database? Why is that?

How to properly structure database calls in an express/mongoose application?

What is the correct way to access a mongodb database from Express ?
Right now, I am including my database handler db.js which contains mongoose.connect( 'mongodb://localhost/db' ); every time I need to do a database call.
Should I use the same connection and passing my db object through callbacks or I can just include my db file every time ?
In other words, is mongoose.connect always re-using the same connection ?
Edit: my source code is public here, I am fairly new to nodejs/express applications and I am not sure if my application is structured properly...
You only need to connect to your database once. In your other files, you need to include your models and use them to read / write to your database collections.
Edit: Looking at your code -- why don't you move your connect into your initialization script, and then include db.js to access your models?

Resources