@jiaxinjiang/nest-config

1.0.4 • Public • Published

Nestjs Config

Configuration component for NestJs.

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) {}
}

/@jiaxinjiang/nest-config/

    Package Sidebar

    Install

    npm i @jiaxinjiang/nest-config

    Weekly Downloads

    27

    Version

    1.0.4

    License

    none

    Unpacked Size

    625 kB

    Total Files

    27

    Last publish

    Collaborators

    • jiaxinjiang