Multitenant configuration data for the SDK Library

2019-09-11 12:08发布

问题:

This question builds on an answer to a previous question, where I was looking for a solution to share configuration data between actors (nodejs modules) of a system (SDK) without using Factory pattern.

This time I need to make the SDK multi-tenant by providing different configuration data for distinct SDK instances.

app.js

var SDKAcctA = require("./sdk");
SDKAcctA.config = {key: "key", secret: "secret"};

var SDKAcctB = require("./sdk");
SDKAcctB.config = {key: "key2", secret: "secret2"};

// use both of them concurrently without each one overriding the config data of the other

var mA = new SDKAcctA.M1();
mA.doSomething();

var mB = new SDKAcctB.M1();
mB.doSomething();

sdk.js

var SDK = function(){};

SDK.config = {key: null, secret: null};

module.exports = SDK;

require("./m1"); //executing other modules to make it work

m1.js

var SDK = require("./sdk");

var M1 = function(){};

M1.prototype.doSomething = function(){
    var config = SDK.config //getting access to main module's config data
};

SDK.M1 = M1;

Question:

Right now the configuration data is getting overriden by the other instance and that's what the issue is. Please advise.