In the following code, are the “db” variables in session.js and user.js referencing to the same object in db.js, or are they copies of it (making separate connections to my db server)?
// db.js
var mongojs = require('mongojs');
var db = mongojs('test', ['users', 'sessions']);
module.exports.database = db;
// session.js
var db = require('../db.js').database;
......
// user.js
var db = require('../db.js').database;
......
Thanks!
Every call to
require('../db.js')returns the same object, so in your case there would just be a singledatabaseconnection pool created.Note that
databaseis actually a pool of connections (5 by default) that can be freely shared across your code so this is likely what you want.See the docs here.