300. Configs
Setup Configs
- save configuration files (environment variables), inside
.env
files - load .env files using
@nest/config
library - use the variables using process.env.[variable_name]
npm i --save @nestjs/config
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DATABASE_HOST,
port: +process.env.DATABASE_PORT,
username: process.env.DATABASE_USER,
//...
}),
],
})
export class AppModule {}
Custom .env Path
- Configuration | NestJS - A progressive Node.js framework
- we can also pass custom env file
- by default
ConfigModule.forRoot()
looks for a.env
file in the root directory of the application. - To specify another path for the
.env
file, use theenvFilePath
property
- by default
ConfigModule.forRoot({
envFilePath: '.development.env',
});
- we can configure configmodule behavior by defining different options
- ignoreEnvFile
- this is useful to prevent configmodule from looking .env to load environment variables…this is useful for cases such as providing environment variables using hosting ui instead of .env
- ignoreEnvFile
ConfigModule.forRoot({
ignoreEnvFile: true
})
Using Config Service
- Configuration | NestJS - A progressive Node.js framework
- import the config module inside AppModule(so it can be available everywhere) using the .forRoot method
- inject config-service in the class you want to use use
@Module({
imports: [
ConfigModule.forRoot(),
],
})
export class AppModule {}
class CoffeeService{
constructor(private readonly configService: ConfigService){
const db_password = this.configService.get<string>('DATABASE_PASSWORD');
//CAN ALSO WORK WITH DEFAULT VALUE
const db_password = this.configService.get<string>('DATABASE_PASSWORD', 'password');
}
}
it's possible to define different configurations per section of your app, and use config module to load them flexibly
always prefer using configService.get('key') instead of using process.env.key
Cache Environment Variables
- Configuration | NestJS - A progressive Node.js framework
- As accessing
process.env
can be slow, you can set thecache
property of the options object passed toConfigModule.forRoot()
to increase the performance ofConfigService#get
method when it comes to variables stored inprocess.env
.
ConfigModule.forRoot({
cache: true,
});
Config Validation
- Configuration | NestJS - A progressive Node.js framework
- we can use
Joi
to validate config variables- GitHub - hapijs/joi: The most powerful data validation library for JS
- i.e is port a number, db connection string is required…
- if the configurations don't meet our criteria application will stop with meaning-full error.
npm install --save joi
ConfigModule.forRoot({
validationSchema: Joi.object({
DATABASE_HOST: Joi.required(),
DATABASE_PORT: Joi.number().default(5432)
})
}),
Using Config in main.ts
- Configuration | NestJS - A progressive Node.js framework
- While our config is a stored in a service, it can still be used in the
main.ts
file. - This way, you can use it to store variables such as the application port or the CORS host.
const configService = app.get(ConfigService);
const port = configService.get('PORT');