-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The @nodeflip/nest-env-config
module provides a convenient way to manage environment configuration in your NestJS applications. By using decorators, you can define configuration properties that are automatically populated from environment variables with optional default values. This module ensures that your configuration is strongly typed and easily accessible throughout your application.
To install the module, run the following command:
npm install @nodeflip/nest-env-config
First, import the EnvironmentConfigModule in your application's module and call the forRoot method with your configuration class.
import { Module } from '@nestjs/common';
import { EnvironmentConfigModule } from '@nodeflip/nest-env-config';
import { AppConfig } from './app-config';
import { DBConfig } from './db-config';
@Module({
imports: [
EnvironmentConfigModule.forRoot({configClass: AppConfig}),
EnvironmentConfigModule.forRoot({configClass: DBConfig, serviceName: 'DB_CONFIG_SERVICE'})
],
})
export class AppModule {}
Define a configuration class and use the @Prop decorator to specify the environment variables and their default values.
import { Prop } from '@nodeflip/nest-env-config';
export class AppConfig {
@Prop('APP_PORT', 3000)
public port: number;
@Prop('APP_ENV', 'development')
public env: string;
@Prop('DATABASE_URL')
public databaseUrl: string;
}
Inject the EnvironmentConfigService into your services to access the configuration properties.
import { Injectable } from '@nestjs/common';
import { EnvironmentConfigService } from '@nodeflip/nest-env-config';
import { AppConfig } from './app-config';
import { DBConfig } from './db-config';
@Injectable()
export class AppService {
constructor(
private readonly appConfigService: EnvironmentConfigService<AppConfig>,
@Inject('DB_CONFIG_SERVICE') private readonly dbConfigService: EnvironmentConfigService<DBConfig>
) {
console.log(this.appConfigService.config);
console.log(this.dbConfigService.config);
}
getPort(): number {
return this.dbConfigService.config.port;
}
}
The EnvironmentConfigModule can be easily tested using the NestJS testing utilities. Below is an example of how to set up and test the configuration service.
import { Prop } from '@nodeflip/nest-env-config';
export class DBConfig {
@Prop('DB_NAME', 'test_db')
name: string;
@Prop('DB_USERNAME', 'test_user')
username: string;
@Prop('DB_PASSWORD', 'test_password')
password: string;
@Prop('DB_HOST', 'test_host')
host: string;
@Prop('DB_PORT', 5432)
port: number;
@Prop('ENABLE_LOGGING', false)
logging: boolean;
}
import { Test, TestingModule } from '@nestjs/testing';
import { EnvironmentConfigModule } from '@nodeflip/nest-env-config';
import { EnvironmentConfigService } from '@nodeflip/nest-env-config';
import { DBConfig } from './db-config';
describe('EnvironmentConfigService', () => {
let service: EnvironmentConfigService<DBConfig>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [EnvironmentConfigModule.forRoot({configClass: DBConfig})],
}).compile();
service = module.get<EnvironmentConfigService<DBConfig>>(EnvironmentConfigService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should retrieve configuration properties', () => {
const config = service.config;
expect(config).toBeDefined();
expect(config.name).toBe('test_db');
expect(config.username).toBe('test_user');
expect(config.password).toBe('test_password');
expect(config.host).toBe('test_host');
expect(config.port).toBe(5432);
expect(config.logging).toBe(false);
});
});
The module includes the following main components:
- EnvironmentConfigModule: The module to be imported into your application module.
- EnvironmentConfigService: A service that provides access to the configuration properties.
- Prop Decorator: A decorator for defining configuration properties.
The EnvironmentConfigModule
is a dynamic module that registers the EnvironmentConfigService
with the specified configuration class.
The EnvironmentConfigService
retrieves the configuration values from the environment variables and maps them to the properties defined in the configuration class.
The Prop
decorator is used to define class properties that will be populated with values from the environment variables.
Contributions are welcome! Please follow the standard GitHub workflow to contribute to this project.
- Fork the repository.
- Create a new branch.
- Make your changes.
- Open a pull request.
This project is licensed under the MIT License.
@nestjs/common
@nestjs/config
@nestjs/core
reflect-metadata
Enjoy using @nodeflip/nest-env-config
!