lundi 2 mai 2022

what is the best practice of services initialisations in nodejs monorepo project?

I am currently working on a large nodejs mono repo project using lerna to maintain packages.

I maintain all my reusable packages as a nodejs package for example redis-client , sqs , MySQL, etc.. in one repository let call it lib, and another repository for my app

I have an issue with my singleton services on the lib repository if I take for example redis-client

today the package that implements the redis-client export a singleton of that instance

export const redis = new RedisClient();

inside the app makes it very easy to use the package that way. but there is a coupling between the implementation and the initialization which also make it very hard to costume the configuration.

I want to decouple the implementation from the initialization and want to find out what is the best practice

  • approach 1 :

export only the class instance of redis-client from lib create another package of the Redis singleton initialization at lib consume the initialization package inside the app

in this approach, I have the benefits of importing the redis-client-singletone from everywhere in my app without using the relative path to the initialization dir the downside is that I have another Redis package that depends on the implementation class and need to be deployed on every Redis-client package update

  • approach 2 : export only the class instance of Redis-client from lib initialize the redis-client-singletone inside app

in this approach I need to use a relative path to use redis-client-singletone in every place i use in my app

my question is whice one of the aapproches is better? i didn't find any examples of how to solve this issue i am looking for a working solution or any insight for someone encounter this issue that would like to share thanks!

Aucun commentaire:

Enregistrer un commentaire