Wednesday 28 November 2018

Does this method of connecting to mongodb create any memory leak?

So this is how I am connecting my nodejs app to mongo db,const mongoose = require('mongoose');const options = {useNewUrlParser: true, useCreateIndex: true};mongoose.connect('mongodb://localhost:27017/logindb', options);And I just imported it in app.js and all models which seem to be the only two places that it is required. By having it like this, am I creating too many connections or something everytime I import it? Because I recently saw a medium article writing the connection object as singleton and exporting it. So this question popped up although the app is working fine.The snippet from the article below.let mongoose = require('mongoose');const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVERconst database = 'fcc-Mail'; // REPLACE WITH YOUR DB NAMEclass Database {constructor() {this._connect()}_connect() {mongoose.connect('mongodb://' + server '/' + database}').then(() => {console.log('Database connection successful')}).catch(err => {console.error('Database connection error')})}}module.exports = new Database()

Submitted November 28, 2018 at 12:14PM by ethoetho

No comments:

Post a Comment