NestCloud component for getting local configurations and environment values when the app bootstrap.
$ npm i --save @nestcloud/boot
import { Module } from '@nestjs/common';
import { BootModule } from '@nestcloud/boot';
import * as path from 'path';
@Module({
imports: [
BootModule.forRoot({
filePath: path.resolve(__dirname, 'config.yaml'),
}),
],
})
export class AppModule {}
Boot module will load config.yaml
, config.${env}.yaml
two files.
web:
name: example-service
port: 3000
There are two ways to get your config data,
- Inject Boot instance:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectBoot, Boot } from '@nestcloud/boot';
@Injectable()
export class ConfigService implements OnModuleInit {
constructor(
@InjectBoot() private readonly boot: Boot
) {}
onModuleInit() {
const port = this.boot.get<number>('service.port', 3000);
}
}
- Inject value:
import { Injectable } from '@nestjs/common';
import { BootValue } from '@nestcloud/boot';
@Injectable()
export class ConfigService {
@BootValue('service.port', 3000)
private readonly port: number;
}
Dependency handlebars.js.
template:
process.env.SERVICE_ID = 'your-service-id';
process.env.SERVICE_NAME = 'your-service-name';
service:
id: ${{ SERVICE_ID }}
name: ${{ SERVICE_NAME }}
port: 3000
address: http://${{ service.name }}:${{ service.port }}
result:
service:
id: your-service-id
name: your-service-name
port: 3000
address: http://your-service-name:3000
Register boot module.
field | type | description |
---|---|---|
options.filePath | string | the config file path |
Get configurations
field | type | description |
---|---|---|
path | string | path of configurations |
defaults | any | default value if the specific configuration is not exist |
Inject Boot instance.
Inject configuration to class attribute.
- Author - NestCloud
NestCloud is MIT licensed.