I understand how to use register to pass static values into a dynamic module so i can conditionally load modules or providers. What I cannot figure out is how to pass the config service into registerAsync as it resolves the factory function as part of the dynamic module being loaded. Essentially our dynamic module is a wrapper for conditionally loading the nest task scheduler module (we only want our task scheduler to run on one server, so this is based on an environment value). We can use register to do this by getting the value from our process.env simply enough but am curious how we can leverage the framework to conditionally load a module based on some condition. Seems very hard to do. Thanks!
#Conditionally loading module based on Config Service
1 messages · Page 1 of 1 (latest)
@elfin lake - Lazy loading is possible.
https://docs.nestjs.com/fundamentals/lazy-loading-modules#lazy-loading-modules
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
But how would you access the config service to do a condition check?
@elfin lake - I'm not sure I follow. The config module is already loaded. So, it should also be available for when you lazy load the module you want conditionally loaded. From the docs:
Also, "lazy-loaded" modules share the same modules graph as those eagerly loaded on the application bootstrap as well as any other lazy modules registered later in your app.
Hmm.. I think I misunderstood your use case. This might help: https://stackoverflow.com/questions/65355892/can-you-import-a-nestjs-module-on-condition
Or, you can inject the config service into your registerAsync and use any config settings in that method accordingly.
Hey thanks. Yea we tried injecting the config service via useFactory but that only gets instantiated to be used by providers in the dynamic module. I'll share how we use register and I'm just not sure how to get the config service to use in the module decorator to like you say pass it or a value from it in the registerAsync