@alphaapps/nestjs-config
v3.0.31
Published
> TODO: description
Downloads
143
Maintainers
Readme
About
This module uses the config package along with Joi to validate the config
Installation
npm install @alphaapps/nestjs-config
The module uses @nestjs/common
along with config
& @hapi/joi
packages so you should install them too (if not installed already):
npm install @nestjs/common config @hapi/joi
Usage
Import the config module and call the register
method in the imports
array in the app.module.ts
file.
import { Module } from '@nestjs/common';
import { ConfigModule } from '@alphaapps/nestjs-config';
import schema from './configSchema';
@Module({
imports: [
ConfigModule.register({ schema }), // pass a Joi schema to validate the config against
ConfigModule.register() // don't pass anything if you don't want any validation
],
})
export class AppModule {}
Refer to config docs for more on how to write and include config files in the project.
A Joi
schema example:
import Joi from '@hapi/joi';
const envVarsSchema: any = {
server: {
port: Joi.number().min(1024)
},
db: {
dialect: Joi.string().allow('postgres', 'mysql'),
host: Joi.string(),
port: Joi.number(),
username: Joi.string(),
password: Joi.string(),
database: Joi.string(),
define: {
underscored: Joi.bool(),
paranoid: Joi.bool(),
timestamps: Joi.bool(),
freezeTableName: Joi.bool()
}
},
JWT: {
secretKey: Joi.string().required()
},
};
export default envVarsSchema;
Configuration Service:
A service to store and retrieve config values. The main purpose of this is to store config values in db if clients wants to change them.
When calling ConfigService.get
method it checks the db first for this config, if not found then retrieve it from app config.