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-config

v1.0.4

Published

Configuration component for NestJs.

Downloads

129

Readme

Features

  • Automatically load all configuration files of src/config directory.
  • Automatically read all environment variables of env/ directory.
  • Change and Load configuration at runtime

Installation

Yarn

yarn add @jiaxinjiang/nest-config

NPM

npm install @jiaxinjiang/nest-config --save

Getting Started

Let's imagine that we have a folder called src/config in our project that contains several configuration files.

├── env
│   ├── env
│   ├── env.dev
│   ├── env.prod
│   ├── env.test
├── src
│   ├── app.module.ts
│   ├── config
│       ├── logger.config.ts
│       ├── database.config.ts
│       ├── global.config.ts
│       ├── rabbitmq.config.ts
│       └── redis.config.ts

Some file examples.

// env.dev

NEST_APPLICATION_PORT=3000

NEST_DATABASE_HOST=192.168.0.123

NEST_DATABASE_PORT=5432
// database.config.ts

const { NEST_DATABASE_HOST, NEST_DATABASE_PORT } = process.env;

export default {
  database1: {
    name: 'database1',
    type: 'postgres',
    host: NEST_DATABASE_HOST,
    port: NEST_DATABASE_PORT,
    username: 'postgres',
    password: '123456',
    database: 'database_name',
    migrationsRun: false,
    synchronize: false,
    logging: 'all',
    maxQueryExecutionTime: 1500, // 慢查询记录
    entityPrefix: '',
    extra: {
      connectionLimit: 10,
    },
  },
};

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

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

@Module({
    imports: [
        ConfigModule,
    ],
})
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 rabbitmqConfig;
    constructor(private readonly config: ConfigService) {
        this.databaseConfig = config.get('database'); // src/config/database.config.ts
        this.rabbitmqConfig = config.get('rabbitmq'); // src/config/rabbitmq.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) {}
}