0.4.4 • Published 5 years ago

@nestcloud/consul-config v0.4.4

Weekly downloads
32
License
MIT
Repository
github
Last release
5 years ago

NestCloud - ConsulConfig

Description

A NestCloud component for getting and watching configurations from consul kv.

中文文档

Installation

$ npm i --save @nestcloud/consul consul @nestcloud/consul-config

Quick Start

Import Module

import { Module } from '@nestjs/common';
import { ConsulModule } from '@nestcloud/consul';
import { ConsulConfigModule } from '@nestcloud/consul-config';
import { BootModule } from '@nestcloud/boot';
import { NEST_BOOT } from '@nestcloud/common';

@Module({
  imports: [
      ConsulModule.register({dependencies: [NEST_BOOT]}),
      BootModule.register(__dirname, 'bootstrap.yml'),
      ConsulConfigModule.register({dependencies: [NEST_BOOT]})
  ],
})
export class ApplicationModule {}

Configurations

consul:
  host: localhost
  port: 8500
  service: 
    id: null
    name: example-service
  config:
    key: config__${{ consul.service.name }}__${{ NODE_ENV }}

How to get remote configurations

In consul kv, the key is "configexample-servicedevelopment".

user:
  info:
    name: 'test'

Inject Config Client

import { Injectable } from '@nestjs/common';
import { InjectConfig, ConsulConfig } from '@nestcloud/consul-config';

@Injectable()
export class TestService {
  constructor(
      @InjectConfig() private readonly config: ConsulConfig
  ) {}

  getUserInfo() {
      const userInfo = this.config.get('user.info', {name: 'judi'});
      console.log(userInfo);
  }
}

Inject value

import { Injectable } from '@nestjs/common';
import { ConfigValue } from '@nestcloud/consul-config';

@Injectable()
export class TestService {
  @ConfigValue('user.info', {name: 'judi'})
  private readonly userInfo;

  getUserInfo() {
      return this.userInfo;
  }
}

API

class ConsulConfigModule

static register(options): DynamicModule

Register consul config module.

fieldtypedescription
options.dependenciesstring[]if you are using @nestcloud/boot module, please set NEST_BOOT
options.retrynumberthe max retry count when get configuration fail

class ConsulConfig

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.

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago

0.3.16-0

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.14-2

5 years ago

0.3.14-1

5 years ago

0.3.14-0

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.9-0

5 years ago

0.3.8

5 years ago

0.3.8-5

5 years ago

0.3.8-4

5 years ago

0.3.8-3

5 years ago

0.3.8-2

5 years ago

0.3.8-1

5 years ago

0.3.8-0

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5-3

5 years ago

0.2.5-2

5 years ago

0.2.5-1

5 years ago

0.2.5-0

5 years ago

0.2.4

5 years ago

0.2.4-3

5 years ago

0.2.4-2

5 years ago

0.2.4-1

5 years ago

0.2.4-0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.1-5

5 years ago

0.2.1-4

5 years ago

0.2.1-3

5 years ago

0.2.1-2

5 years ago

0.2.1-1

5 years ago

0.2.1-0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago