0.7.1 • Published 4 years ago
@dimensionfourcloud/config v0.7.1
NestCloud - Config
Description
A NestCloud component for getting and watching configurations from consul kv or kubernetes configmaps.
Installation
# consul backend
$ npm i --save @dimensionfourcloud/consul consul @dimensionfourcloud/config
# etcd backend
$ npm i --save @dimensionfourcloud/etcd etcd3 @dimensionfourcloud/config
# kubernetes backend
$ npm i --save @dimensionfourcloud/config
Quick Start
Import Module
Consul Backend
import { Module } from '@nestjs/common';
import { ConsulModule } from '@dimensionfourcloud/consul';
import { ConfigModule } from '@dimensionfourcloud/config';
import { BootModule } from '@dimensionfourcloud/boot';
import { NEST_BOOT, NEST_CONSUL } from '@dimensionfourcloud/common';
@Module({
imports: [
ConsulModule.register({dependencies: [NEST_BOOT]}),
BootModule.register(__dirname, 'bootstrap.yml'),
ConfigModule.register({dependencies: [NEST_BOOT, NEST_CONSUL]})
],
})
export class ApplicationModule {}
Kubernetes Backend
import { Module } from '@nestjs/common';
import { ConfigModule } from '@dimensionfourcloud/config';
import { BootModule } from '@dimensionfourcloud/boot';
import { NEST_BOOT, NEST_KUBERNETES } from '@dimensionfourcloud/common';
@Module({
imports: [
BootModule.register(__dirname, 'bootstrap.yml'),
ConfigModule.register({dependencies: [NEST_BOOT, NEST_KUBERNETES]})
],
})
export class ApplicationModule {}
Etcd Backend
import { Module } from '@nestjs/common';
import { ConfigModule } from '@dimensionfourcloud/config';
import { EtcdModule } from '@dimensionfourcloud/etcd';
import { BootModule } from '@dimensionfourcloud/boot';
import { NEST_BOOT, NEST_ETCD } from '@dimensionfourcloud/common';
@Module({
imports: [
BootModule.register(__dirname, 'bootstrap.yml'),
EtcdModule.register({dependencies: [NEST_BOOT]}),
ConfigModule.register({dependencies: [NEST_BOOT, NEST_ETCD]})
],
})
export class ApplicationModule {}
Configurations
config.key
is available for consul backend and etcd backend.
config.key
, config.namespace
, config.path
are only available for kubernetes configMap.
config:
key: dimensionfourcloud-conf
namespace: default
path: config.yaml
Configurations In Consul KV Or Etcd
user:
info:
name: 'test'
Configurations In Kubernetes ConfigMap
apiVersion: v1
data:
config.yaml: |-
user:
info:
name: 'test'
kind: ConfigMap
metadata:
name: dimensionfourcloud-conf
namespace: default
Inject Config Client
import { Injectable } from '@nestjs/common';
import { InjectConfig, Config } from '@dimensionfourcloud/config';
@Injectable()
export class TestService {
constructor(
@InjectConfig() private readonly config: Config
) {}
getUserInfo() {
const userInfo = this.config.get('user.info', {name: 'judi'});
console.log(userInfo);
}
}
Inject value
import { Injectable } from '@nestjs/common';
import { ConfigValue } from '@dimensionfourcloud/config';
@Injectable()
export class TestService {
@ConfigValue('user.info', {name: 'judi'})
private readonly userInfo;
getUserInfo() {
return this.userInfo;
}
}
API
class ConfigModule
static register(options): DynamicModule
Register consul config module.
field | type | description |
---|---|---|
options.dependencies | string[] | NEST_BOOT, NEST_CONSUL, NEST_KUBERNETES |
options.key | key of the consul kv or name of the kubernetes configMap | |
options.namespace | the kubernetes namespace | |
options.path | the path of the kubernetes configMap |
class Config
get(path?: string, defaults?: any): any
Get configuration from consul kv.
field | type | description |
---|---|---|
path | string | the path of the configuration |
defaults | any | default value if the specific configuration is not exist |
getKey(): string
Get the current key.
watch(path: string, callback: (configs: any) => void): void
Watch the configurations.
field | type | description |
---|---|---|
callback | (configs) => void | callback function |
async set(path: string, value: any): void
Update configuration.
field | type | description |
---|---|---|
path | string | the path of the configuration |
value | any | the configuration |
Decorators
ConfigValue(path?: string, defaultValue?: any): PropertyDecorator
Inject configuration to attribute. It will change realtime when the value changed in consul kv.
Stay in touch
- Author - NestCloud
License
NestCloud is MIT licensed.
0.7.1
4 years ago