0.7.1 • Published 4 years ago

@dimensionfourcloud/config v0.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

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.

fieldtypedescription
options.dependenciesstring[]NEST_BOOT, NEST_CONSUL, NEST_KUBERNETES
options.keykey of the consul kv or name of the kubernetes configMap
options.namespacethe kubernetes namespace
options.paththe path of the kubernetes configMap

class Config

get(path?: string, defaults?: any): any

Get configuration from consul kv.

fieldtypedescription
pathstringthe path of the configuration
defaultsanydefault 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.

fieldtypedescription
callback(configs) => voidcallback function

async set(path: string, value: any): void

Update configuration.

fieldtypedescription
pathstringthe path of the configuration
valueanythe 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

License

NestCloud is MIT licensed.