I want to use @nestjs to manage configuration files.
Now I have used the following ways to connect mysql and redis.
// app.module.ts
import { Module } from '@nestjs/common'
import { SequelizeModule } from '@nestjs/sequelize'
import { RedisModule } from 'nestjs-redis'
import { ConfigModule } from '@nestjs/config'
import { ExampleModule } from './example/example.module'
import { UserModule } from './user/user.module'
import config from './config' // I want to use @nestjs/config instead it
@Module({
imports: [
SequelizeModule.forRoot(config.DATABASE),
RedisModule.register(config.REDIS),
UserModule,
ExampleModule,
ConfigModule.forRoot()
],
})
export class AppModule {}
Can I get their configuration information in appmodule via @ nestjs / config
You need to use the asynchronous registration methods forRootAsync
and registerAsync
. The easiest way to do this is to have a setup like so:
@Module({
imports: [
SequelizeModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
url: config.get('DATABASE'),
}),
}),
RedisModule.registerAsync({
inject: [ConfigService],
useFacotry: (config: ConfigService) => ({
url: config.get('REDIS')
})
}),
UserModule,
ExampleModule,
ConfigModule.forRoot({
isGlobal: true
})
],
})
export class AppModule {}
Some of the options and typings may be off, but this should head you in the right direction.