var manager = function(db) {
this.db = db;
}
manager.prototype = {
doSth: function(cb) {
this.db.findOne({}, function(err, doc) {
// more logic here
cb(doc);
};
}
}
module.exports = manager;
manager = require('manager');
var db = require('mongo');
db.connect(); //forget the details here
var M = new manager(db);
M.doSth();
manager = require('manager');
var M = new manager(app.get('db')); //ask the app for a resource or connect db again
M.doSth();
var manager = function() {
this.db = null;
}
manager.prototype = {
connect: function(db) {
if (this.db == null)
this.db = db;
}
doSth: function(cb) {
this.db.findOne({}, function(err, doc) {
// more logic here
cb(doc);
};
}
}
module.exports = manager;
M = require('manager'); //is already an instance
var db = require('mongo').connect();
M.connect(db)
M.doSth();
M = require('manager');
M.doSth(); //will work since manager has been connected by app.js