npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jiaxinjiang/nest-remote-config

v1.0.9

Published

Nacos config component for NestJs.

Downloads

9

Readme

Features

  • Rely on @jiaxinjiang/nest-config and @jiaxinjiang/nest-nacos module development.
  • Listen for Nacos configuration changes and automatically modify the configuration in the @jiaxinjiang/nest-config package.
  • Configuration format currently only supports YML.

Installation

Yarn

yarn add @jiaxinjiang/nest-remote-config

NPM

npm install @jiaxinjiang/nest-remote-config --save

Getting Started

Directory structure:

├── env
│   ├── env
│   ├── env.dev
│   ├── env.prod
│   ├── env.test
├── src
│   ├── app.module.ts
│   ├── config
│       ├── nacos.config.ts
│       ├── redis.config.ts

Some file examples.

// .env.dev

NEST_NACOS_SERVER_LIST=192.168.0.102:8848

NEST_NACOS_NAMESPACE=d8bcbaad-f3d1-40d8-9d55-a03cafea5299

NEST_NACOS_GROUP_NAME=DEV_GROUP

NEST_NACOS_CONFIG_ID_BASIC=basic.config.yml
// nacos.config.ts

import {
  NestNacosConfig,
  ClientOptions,
  ConfigOptions,
  NacosInstanceOptions,
  NacosNamingOptions,
  NacosSubscribeOptions,
} from '@jiaxinjiang/nacos';

const {
  NEST_NACOS_NAMESPACE,
  NEST_NACOS_GROUP_NAME,
  NEST_NACOS_SERVER_LIST,
  NEST_NACOS_CONFIG_ID_BASIC,
} = process.env;

export default {
  naming: {
    serverList: NEST_NACOS_SERVER_LIST,
    namespace: NEST_NACOS_NAMESPACE,
  } as NacosNamingOptions,
  instance: {
    groupName: NEST_NACOS_GROUP_NAME,
  } as NacosInstanceOptions,
  configClient: {
    leaderPort: Number(`45${Math.floor(Math.random() * 900) + 100}`),
    serverAddr: NEST_NACOS_SERVER_LIST.split(',')[0],
  } as ClientOptions,
  configs: [{
      dataId: NEST_NACOS_CONFIG_ID_BASIC,
      groupName: NEST_NACOS_GROUP_NAME,
  }] as ConfigOptions[],
  subscribers: [
    // Services to be monitored
    {
      serviceName: 'service-1',
    },
    {
      serviceName: 'service-2',
    },
  ] as NacosSubscribeOptions[],
} as NestNacosConfig;
// redis.config

import { ClientOpts } from 'redis';

// @ts-ignore
export default {
  port: '${redis.port}', // Get from Nacos
  host: '${redis.host}', // Get from Nacos
  password: '${redis.password}', // Get from Nacos
  db: 1,
  ttl: null,
  defaultCacheTTL: 60 * 60 * 24,
} as ClientOpts;
# Nacos sample configuration

redis:
  port: 8875
  host: 127.0.0.1
  password: 123456

Let's register the config module in app.module.ts

import { Module } from '@nestjs/common';
import { NacosModule } from '@jiaxinjiang/nest-nacos';
import { ConfigModule } from '@jiaxinjiang/nest-config';
import { RemoteConfigModule } from '@jiaxinjiang/nest-remote-config';

@Module({
    imports: [
        ConfigModule,
        NacosModule,
        RemoteConfigModule.forRoot(),
    ],
})
export class AppModule {}

Now we are ready to inject our ConfigService anywhere we'd like.

import { ConfigService } from '@jiaxinjiang/nest-config';

@Injectable()
class SomeService {
    private databaseConfig;
    private redisConfig;
    constructor(private readonly config: ConfigService) {
        this.databaseConfig = config.get('database'); // src/config/database.config.ts
        this.redisConfig = config.get('redis'); // src/config/redis.config.ts
    }
    
    get databaseHost() {
        return this.databaseConfig.host;
    }
}

You may also use the @InjectConfig decorator as following:

import { InjectConfig, ConfigService } from '@jiaxinjiang/nest-config';

@Injectable()
class SomeService {
    constructor(@InjectConfig() private readonly config: ConfigService) {}
}