1

I have a module for connecting to my DB and perform actions.

Then in my main script (app.js) I instantiate it like

var DBConn = require('./common/DBConn');

And from that script it works fine. But then I've got some other scripts that handle routes, and I want to perform some DB stuff on those, but if I use DBConn it returns an error saying "DBConn is not defined".

Then I can just instantiate another DBConn in these other js files, but this would mean I am creating a connection for each file, right? But I want these other scripts to use the DBConn object from the app.js, so that I'm not constantly establishing a connection to the DB and then closing it... (unless this is a good idea, but to me it makes more sense to have just one "global" object dealing with the connection over all the app and that's it).

(BTW: I'm using Express)

2 Answers2

2

You want to require() your module in each file. Node will cache the module.

Typically, the context of a DB connection is abstracted away behind stores or repositories and your other modules interact with those. In cases where people are directly requiring modules like mongoose, they'll require mongoose everywhere but only call the connection code within their main application entry point (app.js/server.js/whatever).

https://nodejs.org/api/modules.html#modules_caching

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

If you want to have a module execute code multiple times, then export a function, and call that function.

Michael Goin
  • 416
  • 6
  • 11
0

You could use a singleton to solve this issue for you. Please remember however that singletons come with their own set of problems (a good discussuon on singletons can be found here What is so bad about singletons?).

That said once you take into consideration the pro's and cons of singletons they can be a fantastic solution to exactly this problem.

Example:

// database.js
var singleton = function singleton() {

    this.DbConnection = {};

    function setup(database, username, password) {
        this.DbConnection = MakeDbConnection()
    };

};

singleton.instance = null;

singleton.getInstance = function(){
    if(this.instance === null){
        this.instance = new singleton();
    }
    return this.instance;
};

module.exports = singleton.getInstance();

This can then be called elsewhere in your application like so...

// main.js
var db     = require("./database");

db.setup('db', 'root', 'pass');

db.DbConnection();

Once you have called db.setup() the first time, the db connection will be available just with the following:

// elsewhere.js
var db     = require("./database");

db.DbConnection();
Community
  • 1
  • 1
Kinetic
  • 1,714
  • 1
  • 13
  • 38