@redhare/config
v0.0.2
Published
# Background Currently configuration module code is hardcoded into the template code which will make future update or bug fix infeasible. The configuration module code should be extracted into an independent nestjs module. In this way developer can not o
Downloads
3
Keywords
Readme
@infra-node-kit/config
Background
Currently configuration module code is hardcoded into the template code which will make future update or bug fix infeasible. The configuration module code should be extracted into an independent nestjs module. In this way developer can not only update the corresponding package when new features are released, but also have an option to choose whether they want to use our module in their project.
Features
- Compatible to nestjs official configuration module.
- Watch the configuration file and hot-reload it when the file content change(Except ts config files).
- Can depend the environment variable value and user set directory, merge the config result from diffrent file.
How to use
Install
yarn install @infra-node-kit/config
API
It will share the same API with @nestjs/config
. The API of @nestjs/config
can be found at https://docs.nestjs.com/techniques/configuration
Basic use
It is recommended to refer to the usage example and path of the unit test part of the source code:
/packages/config/src/__test__/testModule
import { Module } from '@nestjs/common'
import { join } from 'path'
import { AppController } from './app.controller'
import { AppService } from './app.service'
// import { ConfigModule } from '@nestjs/config'
// change the line above to the line below
import { ConfigModule } from '@infra-node-kit/config'
import { PfbModule } from './pfb/pfb.module'
const configMap = [
{
namespace: 'ns1',
filePath: join(process.cwd(), 'config', 'ymlConfig.yaml')
},
{
namespace: 'ns2',
filePath: join(process.cwd(), 'config', 'jsonConfig.json')
},
{
namespace: 'ns3',
filePath: join(process.cwd(), 'config', 'tsConfig.ts')
}
]
@Module({
imports: [PfbModule, ConfigModule.forRoot({ configMap, isGlobal: true })],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
loadEnvConfig
Usage
import { Module } from '@nestjs/common'
import { ConfigModule } from '@infra-node-kit/config'
@Module({
imports: [ConfigModule.forRoot({
isGlobal: true,
loadEnvConfig: {
enable: true,
}
})],
})
export class AppModule {}
params
loadEnvConfig
is a param in ConfigModule.forRoot
, It is an another way to load config file beside the configMap
.
The structure is below:
export interface IEnvConfig {
enable?: boolean
dir?: string
env?: string
}
enable
is to enable the load function.
dir
is an absolute directory which decide the directory of config files. default value is join(process.cwd(), 'dist/configs')
env
is a variable which sign the environment, default value is join(process.cwd(), 'dist/configs')
default dir example:
- src
- - - configs
- - - - config.default.ts
- - - - config.local.ts
- - - - config.test.ts
- - - - config.live.ts
Nest project default will compile src
directory to dist
directory, so when we set the dir
params,we need pay attention to that. Finally path is dist
not src
.
Naming rule and Merge rule
Naming rules of env config filename is config.${env}
and all env default filename is config.default
. The final result is the merge of config.default
and config.${env}
.
Object item will recursive merge.
The merge example
config.default.ts
export default {
env: 'default',
envObject: {
env: 'default',
save: true,
},
}
config.live.ts
export default {
env: 'live',
envObject: {
env: 'live',
},
}
if the env
value is live
, then the config result is:
{
env: 'live',
envObject: {
env: 'live',
save: true
},
}
Types
export interface IConfigSpaceOption {
namespace: string
filePath: string
fileType?: CONFIG_FILE_TYPE
fileEncoding?: BufferEncoding
}
export interface IConfigModuleOptions extends NestjsConfigModuleOptions {
configMap?: IConfigSpaceOption[]
}