open-index/open-npm huggingface dataset:
npm package: @jiaxinjiang/nest-config
Description: Configuration component for NestJs.
Latest version: 1.0.4

<h1 align="center">Nestjs Config</h1>

<p align="center">Configuration component for NestJs.</p>

## 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**
```bash
yarn add @jiaxinjiang/nest-config
```

**NPM**
```bash
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.

```bash
├── 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.

```bash
// env.dev

NEST_APPLICATION_PORT=3000

NEST_DATABASE_HOST=192.168.0.123

NEST_DATABASE_PORT=5432
```

```ts
// 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


```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.

```ts
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:

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

@Injectable()
class SomeService {
    constructor(@InjectConfig() private readonly config: ConfigService) {}
}
```
npm package: @jiaxinjiang/nest-logger
Description: Log component for NestJs.
Latest version: 2.0.2

<h1 align="center">Nestjs Logger</h1>

<p align="center">Log component for NestJs.</p>

## Features

- Nestjs log component encapsulated with Winston.
- Free and flexible to define context.
- Support print request context（Fastify only）.

### Installation

**Yarn**
```bash
yarn add @jiaxinjiang/nest-logger
```

**NPM**
```bash
npm install @jiaxinjiang/nest-logger --save
```

### Getting Started

First, you need to define the log configuration file. You must rely on the `@jiaxinjiang/nest-config` module.

Directory structure:

```bash
├── env
│   ├── env
│   ├── env.dev
│   ├── env.prod
│   ├── env.test
├── src
│   ├── app.module.ts
│   ├── config
│       ├── logger.config.ts
```

Logger configuration file:

```ts
// logger.config.ts

import * as path from 'path';
import * as color from 'cli-color';
import { format, transports, LoggerOptions } from 'winston';
import DailyRotateFile = require('winston-daily-rotate-file');

const logDir = path.resolve(globalConfig.rootDir, '../logs');

export default {
  logDir,
  level: process.env.NEST_LOGGER_LEVEL || 'info',
  format: format.combine(
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
    format.printf(info => {
      // @ts-ignore
      if (info.message instanceof Error) {
        info.message = info.message.stack;
      }
      let levelMessage = `[${info.level.toLocaleUpperCase()}]`;
      switch (info.level) {
        case 'warn':
          levelMessage = color.yellow(levelMessage);
          break;
        case 'error':
          levelMessage = color.red(levelMessage);
          break;
        default:
          levelMessage = color.blue(levelMessage);
          break;
      }
      const pidMessage = color.green(`[Nest] ${process.pid}`);
      const ctxMessage = color.yellow(`[${info.context || 'DefaultContext'}]`);
      return `${pidMessage}   - ${info.timestamp}  ${ctxMessage} ${levelMessage} ${info.message}`;
    }),
  ),
  transports: [
    new DailyRotateFile({
      dirname: logDir,
      filename: `application.%DATE%.log`,
      datePattern: 'YYYYMMDD',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '14d',
    }),
    new transports.Console(),
    new transports.File({
      dirname: logDir,
      filename: 'error.log',
      level: 'error',
    }),
  ],
} as LoggerOptions;

```

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


```ts
import { Module } from '@nestjs/common';
import { LoggerModule } from '@jiaxinjiang/nest-logger';

@Module({
    imports: [
        LoggerModule.forRoot(),
    ],
})
export class AppModule {}
```

You can use @InjectLogger decorator to inject the logger instance;

```ts
import { InjectLogger, LoggerProvider } from '@jiaxinjiang/nest-logger';

@Injectable()
class SomeService {
    constructor(
      @InjectLogger(SomeService) private readonly logger: LoggerProvider
    ) {}

    test() {
      this.logger.log('hello world!')
    }
}
```