I am transforming my Node MicroServices in npm packages
, so that I can run several of them in a single server. They exchanges RabbitMQ messages, so they are independent from the listening server.
My problem is that each Ms used global.variables
, like the DB connection for example. Now that they are Packages, and so I can have different Mss in one server, I can't share a global.db_connection
because each Ms may use a different db.
Is there a way so that I can use a global.db_connection
just inside a Package
, and that var is global just inside that Package?
==== UPDATE1
Before, I wrongly referred to Modules. I mean npm package
. Each MS is a separated npm package, that I install in the server. I've corrected my question now, using package instead of module.
=== EXAMPLE
I know there is a better way to use a DB connection among the code. I am using 'db_connection' just as example of a global object.
-- packageA.js
@module MS_A
global.db_connection = db.connect(URL_1);
class MS_A
{
...
}
module.exports = MS_A;
-- packageB.js
@module MS_B
global.db_connection = db.connect(URL_2);
class MS_B
{
...
}
module.exports = MS_B;
-- server.js
global.needed_variable = 3;
var MS_A = new require("./packageA.js")().start();
var MS_B = new require("./packageB.js")().start();
/**
"needed_variable" should be visible inside both packages.
"db_connection" should be not visible outside each package
*/
MS_B should use its own db_connection
, and be not aware of the db_connection
of MS_A (and vice versa).