If a nodejs module exports an object to two other modules, do changes made to that object in one downstream module propagate to the other?

StackOverflow https://stackoverflow.com/questions/16937305

Question

I originally had a working application in nodejs which had all of its code in one file, split out into discrete chunks using constructors and IIFEs. In order to provide sensible decoupling and separation, I've been trying to refactor the program so that each chunk lives in a different file, but I've run into a problem.

The program is split into three core chunks:

A datastore constructor that initialises to connect to Redis and returns a redis client and dataStore object.

function CreateDataStore(storePort, storeUrl, passwd) {
    var client
    ,   datastore;

    //create the redis client
    //create the datastore

    //exports
    return {
        "redisClient": client,
        "dataObject": createSiteUpdater("")
    };
}

var dataStore = CreateDataStore("xxxx","xxxx","xxxx");
var client = dataStore.redisClient;
var testResults = dataStore.dataObject;

A web crawler constuctor which uses the redis client and stores data in the datastore object.

function CreateWebCrawler(){
    //do some stuff
    //initialise robots.txt parser

    return function() {
    //crawl a page
    //add page data to testResults object
    }
}

A web server Constructor which reads the same datastore object and sends the data out to a front-end client.

function CreateWebServer() {
    //do some stuff
    //initialise an express webserver
    //initialise socket.io
    //send testResults object out over a socket.
}

This all worked fine when they were in the same file, but now that I want to refactor them into three separate files, I'm confused about how they're going to talk to each other.

Under the new architecture both the web crawler module and the web server module "require" the data store module, which exports a dataStore object. What I need to happen is for any changes made by the web crawler module to its dataStore object to be replicated in the version of the dataStore object available to the web server module.

I've tried looking through the all the nodejs documentation, and also done a lot of googling, but I can't for the life of me work out if an object that is returned by a commonjs module's export statement is the same object on the heap when it's returned separately to two different other modules higher up the dependency graph. Can anyone explain to me if this is true, and ideally provide me a link to the correct documentation to understand the behaviour properly?

Was it helpful?

Solution

here is the way I do it:

var datastore = {};
module.exports = datastore;
var redis = require('redis');

var client = '';
var testResults = '';

datastore.CreateDataStore = function(storePort, storeUrl, passwd) {
    client = redis.createClient(storePort,storeUrl);
    redisClient.auth(passwd, function() {
        console.log("redisAuth Connected!");
    });
    testResults = createSiteUpdater("");
}

datastore.setdata = function(data, callback){
    client.set(data.key, data.value, callback);
}

the second function is accessor functions to my database. Not sure that this is the best way, but lets me swap out different databases easily if I need to and should save any changes to the datastore object. Instead of requiring it more than one module, pass the single object around

Here is a more complete example with several databases for an account manager module I wrote:

https://github.com/hortinstein/accountManager/tree/master/databaseMiddleware

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top