# @jiaxinjiang/nest-config

> Configuration component for NestJs.

Latest version **1.0.4** (published 2020-06-29) · 0 weekly downloads

## Install

```sh
npm install @jiaxinjiang/nest-config
pnpm add @jiaxinjiang/nest-config
yarn add @jiaxinjiang/nest-config
bun add @jiaxinjiang/nest-config
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-06-29 |
| First published | 2020-06-24 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.9.0 |
| Dependencies | 7 |
| Unpacked size | 610.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | jiaxinjiang |
| Keywords | nest, nest-config |

## Links

- npm: https://www.npmjs.com/package/@jiaxinjiang/nest-config
- npm.io page: https://npm.io/package/@jiaxinjiang/nest-config

## Dependencies (7)

- [glob](https://npm.io/package/glob.md) ^7.1.2
- [rxjs](https://npm.io/package/rxjs.md) ^6.0.0
- [dotenv](https://npm.io/package/dotenv.md) ^8.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2020-06-29
- 1.0.3 — 2020-06-29
- 1.0.2 — 2020-06-28
- 1.0.1 — 2020-06-28
- 1.0.0 — 2020-06-24

## README

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

---
_Source: https://npm.io/package/@jiaxinjiang/nest-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
