config module for Nest
$ npm i --save @ddboot/config yaml lodash
- import
ConfigModule
in your root module
import { Module } from '@nestjs/common';
import { ConfigModule } from '@ddboot/config';
@Module({
imports: [
ConfigModule.forRootAsync({
filePath: '',
fileName: 'config.yaml',
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
- use
LoggerService
in your service
import { Injectable } from '@nestjs/common';
import { InjectConfig, ConfigService } from '@ddboot/config';
@Injectable()
export class AppService {
//must use @InjectConfig() to inject logger
constructor(@InjectConfig() private readonly config: ConfigService) {}
getHello(): string {
this.config.get('client.id');
return 'Hello World!';
}
}
- or use
@Value()
decorator in your service
import { Injectable } from '@nestjs/common';
import { Value } from '@ddboot/config';
@Injectable()
export class AppService {
@Value('client.id')
private clientId: string;
getHello(): string {
this.logger.info('getHello');
return 'Hello World!';
}
}
Note:
@Value()
decorator will injectLogger
instance to your service, so you can usethis.logger
to log. If you want to use@InjectLogger()
decorator, you must usethis.iLogger.getLogger()
to getLogger
instance.
Config
interface Config {
path?: string;
level?: string;
// file name
name?: string;
// console show log
console?: boolean;
}
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
@nsboot/logger is MIT licensed.