Effortless, dynamic configuration management for NestJS, empowering modular and scalable applications.
MIT License
ConfiguratorLib is a NestJS module designed to facilitate the independent injection and loading of configurations for each service. It promotes clean, modular, and scalable code by enabling services to manage their configurations independently without tight coupling.
In complex NestJS applications, managing configuration settings for different services can lead to tightly coupled code and difficulties in maintaining or extending functionality. As your application grows, the need for a modular approach to configuration management becomes more apparent.
ConfiguratorLib addresses these challenges by providing a decentralized way to manage configuration settings, allowing services to load and inject their configurations independently. This reduces boilerplate code, simplifies service interactions, and supports independent development and scaling.
You can install ConfiguratorLib using npm, yarn, or pnpm:
# npm
npm install @diexpkg/configurator
# yarn
yarn add @diexpkg/configurator
# pnpm
pnpm install @diexpkg/configurator
To start using ConfiguratorLib in your NestJS application, follow these steps:
Import the ConfiguratorModule: Include ConfiguratorModule
in your AppModule
. It's recommended to keep this import in the AppModule
because ConfiguratorModule
is a global module.
import { Module } from '@nestjs/common';
import { ConfiguratorModule } from '@diexpkg/configurator';
@Module({
imports: [ConfiguratorModule.forRoot()],
})
export class AppModule {}
Define a ConfigLoader: The ConfigLoader
is responsible for fetching the configuration based on namespaces. Define a loader that suits your configuration source (e.g., environment variables, external APIs).
const load = (namespace: string[]) => {
const fullEnv = namespace.map(ns => ns.toUpperCase()).join('_');
return process.env[fullEnv];
};
export const envLoader: ConfigLoader = {
load,
loadAsync: async (nm) => load(nm),
};
Annotate Modules and Services with Namespaces: Use the @ConfigNamespace
decorator to define a unique namespace for your modules and services. This allows the loader to correctly identify and fetch the relevant configuration.
import { Injectable, Module } from '@nestjs/common';
import { ConfigNamespace, InjectConfig, ConfigContainer } from '@diexpkg/configurator';
@ConfigNamespace("Role")
@Module({
providers: [UserService, AdminService],
})
export class RoleModule {}
@ConfigNamespace("User")
@Injectable()
class UserService {
constructor(@InjectConfig() private readonly _userConfig: ConfigContainer) {}
}
@ConfigNamespace("Admin")
@Injectable()
class AdminService {
constructor(@InjectConfig() private readonly _adminConfig: ConfigContainer) {}
}
Inject Configuration in Services: Use the @InjectConfig()
decorator to inject the configuration container into your service. The configuration will be automatically loaded based on the services namespace.
import { Injectable } from '@nestjs/common';
import { InjectConfig, ConfigContainer } from '@diexpkg/configurator';
@ConfigNamespace('User')
@Injectable()
class UserService {
constructor(@InjectConfig() private readonly _userConfig: ConfigContainer) {}
}
Both ConfiguratorLib
and @nestjs/config
serve the purpose of managing configuration settings within NestJS applications, but they differ significantly in their approach and use cases.
.env
files. It offers a straightforward and familiar approach for most Node.js applications.Use @nestjs/config
if:
.env
files.Use ConfiguratorLib
if:
ConfiguratorLib is compatible with NestJS versions X.X.X and above. Ensure your project is using a compatible version of NestJS to avoid potential issues. It has been tested with the following versions:
Contributions are welcome! If you'd like to contribute, please follow the guidelines in the CONTRIBUTING.md file. Feel free to open issues for any bugs or feature requests.