#Conditionally loading module based on Config Service

1 messages · Page 1 of 1 (latest)

elfin lake
#

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!

sharp stirrup
#
elfin lake
#

But how would you access the config service to do a condition check?

sharp stirrup
#

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

#

Or, you can inject the config service into your registerAsync and use any config settings in that method accordingly.

elfin lake
#

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