-
One more question about migrating to TypedConfigModule. Injecting config into a Service is simple and well-documented. However, how does one inject values into the module setup process for other modules? Here is an example where I was setting up Redis with the built-in ConfigModule. How should we get the data from the TypedConfigModule-created configuration object instead?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just inject the config model, as @Module({
imports: [
TypedConfigModule.forRoot({
schema: RootConfig,
load: dotenvLoader(),
normalize: (config) => configuration((config))
}),
RedisModule.forRootAsync({
useFactory: (config: RedisConfig) => ({
config: {
host: config.endpoint,
password: config.password,
port: config.port,
},
commonOptions: {
retryStrategy: times => Math.max(times * 100, 3000)
}
}),
// inject any config model
inject: [RedisConfig]
})
],
controllers: [AppController],
providers: [
AppService
],
})
export class AppModule { } If you prefer to import dependent modules manually, you'll need to refactor the import statement to a local variable to avoid copy pasting: // Maybe import from somewhere else
const DynamicConfigModule = TypedConfigModule.forRoot({
schema: RootConfig,
load: dotenvLoader(),
normalize: (config) => configuration((config))
});
@Module({
imports: [
DynamicConfigModule,
RedisModule.forRootAsync({
// you can remove the imports array if you register
// TypedConfigModule as global module, which is true by default.
imports: [DynamicConfigModule],
useFactory: (config: RedisConfig) => ({
config: {
host: config.endpoint,
password: config.password,
port: config.port,
},
commonOptions: {
retryStrategy: times => Math.max(times * 100, 3000)
}
}),
// inject any config model
inject: [RedisConfig]
})
],
controllers: [AppController],
providers: [
AppService
],
})
export class AppModule { } |
Beta Was this translation helpful? Give feedback.
Just inject the config model, as
TypedConfigModule
is registered as global module by default: