Reuse of connections in Nodejs + Mongoose

Asked

Viewed 516 times

1

I’m starting now with Node and used Mongoose to make my connections to the database in Mongodb. I put 1 log in my application, for every time I open the connection it print in this log.

What happens to you: Every time someone calls my 1 URL, my Node application opens a connection to Mongodb, is that normal and correct? That is, if 1,000 access my site, will there be 1,000 open connections? Is there any way to improve that and avoid problems, or is that normal for this environment?

For each model of mine, I have 1 code of this type:

var mongoose = require('mongoose');
var database = require('../config/database');
var mongoOptions = { db: { safe: true }};
mongoOptions.user = database.user;
mongoOptions.pass = database.pass;

console.log('Running mongoose version %s', mongoose.version);

mongoose.connect(database.url, mongoOptions, function (err, res) {
  if (err) { 
    console.log ('ERROR connecting to: ' + database.url + '. ' + err);
  } else {
    console.log ('Successfully connected to: ' + database.url);
  }
});

var Schema = mongoose.Schema;
var citySchema = new Schema({
    name: String,
    url: String,
    uf: String,
    dtRequest: Date,
    active: Boolean,
    loc: {
        type: [Number],
        index: '2dsphere'
    }
});

module.exports = mongoose.model('City', citySchema);

var db = mongoose.connection;
db.on('error', console.error);

process.on('SIGINT', function() {
  db.close(function () {
    console.log('Mongoose default connection disconnected through app termination');
    process.exit(0);
  });
});
  • You can put your code?

  • Updated with 1 of my entities/model.

1 answer

1


The connection to the database should be in the main project file, the one that Node will run, so the connection will only be made once.

For example, the code below is your main file, the app.js, responsible for the project’s routes and settings:

const express = require('express');
const database = require('../config/database');
const mongoose = require('mongoose');

const app = express();

const mongoOptions = { 
    db: { safe: true },
    user: database.user,
    pass: database.pass
};

mongoose.connect(database.url, mongoOptions, function (err, res) {
    if (err) { 
        console.log ('ERROR connecting to: ' + database.url + '. ' + err);
    } else {
        console.log ('Successfully connected to: ' + database.url);
    }
});

app.listen(8080);

The code below is your model:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const citySchema = new Schema({
    name: String,
    url: String,
    uf: String,
    dtRequest: Date,
    active: Boolean,
    loc: {
        type: [Number],
        index: '2dsphere'
    }
});

module.exports = mongoose.model('City', citySchema);

When you run a Node app.js the connection will be started only once, and will remain so until the process is completed or an error occurs.

  • Thanks for the help, apparently everything worked out. I’ll do more tests here. A doubt, because you use const instead of var?

  • 1

    I know it has been a while since the above comment was published, but I will respond to help other people who might have the same question. Marlon, use const instead of var because in the latest version of Javascript (ES6), const and Let appeared. The const is used for constants and the Let for variables. The Let is very similar to the var, but with some differences in scope. I use const because that’s what the Eslint pattern determines in prefer-const. For more information, search for ES6 and Eslint. Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.