0

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).

DeLac
  • 1,068
  • 13
  • 43
  • Its not a good idea to put things in a global object. Try to create a database module which accepts some configurations and export it properly. Now, in your different modules you can import what you need. – Vahid Oct 20 '21 at 14:09
  • Hi. Yes, the Database is just as example. I need a way to make some Object available from wherever in a @Module (but not outside). This to replace the `global.var` way – DeLac Oct 20 '21 at 14:14
  • For instance, If a @module is responsible to manage a mongoose collection `Docs`, I want to define `Doc` just one time, and use it from different places making my Doc.find(), Doc.save() etc... but Doc should be not available outside the @module – DeLac Oct 20 '21 at 14:17
  • Each one of your `.js` files is a module and what you define in it will be scoped to that module only. If your module needs to share something with to the outside world it needs to export it explicitly using `module.export`. This the case if your project is using `CommonJS's` module specification. So if you don't want any of your modules to see the `db_connection`object of each other then, define it in each module separately. – Vahid Oct 20 '21 at 17:22
  • I may wrongly named a `module`. Each of my MS now is a npm package, that I install in my server. Is there a way to make a global.var visible just in that package? If export a var with module.export, or global.var, it will be visibile also by other packages of the server – DeLac Oct 20 '21 at 19:35

0 Answers0