nestjs-config-module
v1.1.0
Published
Configuration module for Nest framework (node.js)
Downloads
69
Maintainers
Readme
Nest.js Config Module
Configuration module for Nest framework (node.js)
Installation
npm install --save nestjs-config-module
Usage
You have to import the Module ConfigModule and initialize it with the forRoot function, this fetches a Javascript object with the loader
function, then maps and validates that to the schema
This Module is not used as injection dependency directly, but rather its schema class and subclasses.
import { Module } from '@nestjs/common';
import { ConfigModule, envJsonLoaderFactory } from 'nestjs-config-module';
import { IsOptional, ValidateNested, IsArray, ArrayUnique, IsString, IsIn } from 'class-validator';
import { Type } from 'class-transformer';
import { LogLevel } from "@nestjs/common";
export class LogConfiguration {
@IsArray()
@ArrayUnique()
@IsString({
each: true
})
@IsIn(['log', 'error', 'warn', 'debug', 'verbose'], {
each: true
})
levels: LogLevel[] = ['log', 'error', 'warn'];
}
export class Configuration {
@IsOptional()
host?: string;
@ValidateNested() @Type(() => LogConfiguration)
log = new LogConfiguration();
}
@Module({
imports: [ConfigModule.forRoot({
isGlobal: true,
loader: envJsonLoaderFactory('DEMO_'),
schema: Configuration,
})],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { Configuration } from 'app.module';
@Injectable()
export class FooService {
constructor(
private configuration: Configuration,
) {}
}
import { Injectable } from '@nestjs/common';
import { LogConfiguration } from 'app.module';
@Injectable()
export class BarService {
constructor(
private logConfiguration: LogConfiguration,
) {}
}
Loaders
this module comes with 2 builtin loaders:
- envJsonLoaderFactory
- jsonFileLoaderFactory
For the documentation on how to use the envJsonLoader see: env-json-parser
It's also possible to implement your own loader, it just needs to be a function that returns a Promise which resolves an object
ConfigModule.forRoot({
isGlobal: true,
loader: (): Promise<object> => {
return Promise.resolve({
host: 'localhost'
})
},
schema: Configuration,
})