Exploring the official Nest documentation on modules, I came across information about global modules and dynamic modules. It got me thinking, is there a way to combine these two patterns?
In my scenario, I have a dynamic config module:
export class ConfigModule {
static forRoot(baseConfigPath: string): DynamicModule {
const providers = [{ provide: 'Config', useValue: configFactory(baseConfigPath) }];
return {
module: ConfigModule,
providers,
exports: providers,
};
}
}
This setup allows the config module to adapt based on the provided base configuration path. When importing this module into the main app module, it looks like this:
@Module({
imports: [ConfigModule.forRoot(path.resolve(__dirname, '../config'))],
controllers: [AppController],
providers: [AppService],
})
export class AppModule implements NestModule {}
which works well. However, I also have several other modules (child modules of the app module and siblings to the config module) where I need the same instance of the dynamic config module to be injectable. Is there a way to designate the dynamic ConfigModule
as global or any other approach that could help achieve this?
I attempted to make the ConfigModule
global using @Global
, but it didn't yield the desired results. For a simplified example, I created a repository based on the nest starter template generated by nest new
: https://github.com/DeX3/nest-di-playground