How to reuse mongodb connection in node.js -


i'm using node-mongodb-native driver mongodb write website.

i have question how open mongodb connection once, use in collection name users in user.js , collection name posts in comment.js

i want open db connection in db.js insert / save data users , posts collection

currently code,my db.js

var db = require('mongodb').db,     connection = require('mongodb').connection,     server = require('mongodb').server; module.exports = new db(     'blog',      new server('localhost', connection.default_port, {auto_reconnect: true}) ); 

i used db.js in user.js follow

var mongodb = require('./db');  function user(user){   this.name = user.name;   this.password = user.password;   this.email = user.email; };  module.exports = user;  user.prototype.save = function(callback) {//save user information   //document save in db   var user = {       name: this.name,       password: this.password,       email: this.email   };   mongodb.close();   //open mongodb database   mongodb.open(function(err, db){     if(err){       return callback(err);     }     //read users collection     db.collection('users', function(err, collection){       if(err){         mongodb.close();         return callback(err);       }       //insert data users collections       collection.insert(user,{safe: true}, function(err, user){         mongodb.close();         callback(err, user);//success return inserted user information       });     });   }); }; 

and comment.js

var mongodb = require('./db');  function comment(name, day, title, comment) {   this.name = name;   this.day = day;   this.title = title;   this.comment = comment; }  module.exports = comment;  comment.prototype.save = function(callback) {   var name = this.name,       day = this.day,       title = this.title,       comment = this.comment;   mongodb.open(function (err, db) {     if (err) {       return callback(err);     }     db.collection('posts', function (err, collection) {       if (err) {         mongodb.close();         return callback(err);       }       //depend on name time , title add comment       collection.findandmodify({"name":name,"time.day":day,"title":title}       , [ ['time',-1] ]       , {$push:{"comments":comment}}       , {new: true}       , function (err,comment) {           mongodb.close();           callback(null);       });        });   }); }; 

you can connect once, , reuse many times want:

var mongodb = require('mongodb'); var events = require('events'); var event = new events.eventemitter(); var access = new mongodb.server(host, port, { }); var client = null;  new mongodb.db('your database', access, { safe: true, auto_reconnect: true }).open(function (err, c) {   if (!err) {     client = c;     console.log('database connected');     event.emit('connect');   } else {     console.log('database connection error', err);     event.emit('error');   } });  exports.get = function(fn) {   if(client) {     fn(client);   } else {     event.on('connect', function() {       fn(client);     });   } }; 

and reuse it:

var db = require('./db'); var items; db.get(function(client) {   items = new mongodb.collection(client, 'collection'); });  // anywhere in code db.get(function() {   // items.find({ ... }); 

Comments