1.2.3 • Published 7 months ago

@martinpang/nestjs-config v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@martinpang/nestjs-config

Configure your Nest.js Applications with node-config lib,without configuration,just import and use,so easy and convenient.

This module is build on node-config,support nestjs DI system,very easy to use and same to config lib.

How to use

npm i @martinpang/nestjs-config
//or you can use yarn or pnpm
pnpm add @martinpang/nestjs-config

the config dir is default to node-config ,the workspace rootdir/config,and many times same to ${workspace}/config

The config file type is same to node-config lib,json,json5,yaml,yml,xml ,cson,ts and so on ,details:https://www.npmjs.com/package/config

//This is app.module.ts,and config path dirs in rootdir
//In this case,the config dirs are rootdir/config2,rootdir/config,rootdir/config3
//The config file name is same to config lib,such as default.EXT,development.EXT ...
//Note:path is based on rootdir,'./' means rootdir,config dir will join with rootdir
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from "@martinpang/nestjs-config"; //here

@Module({
  imports: [ConfigModule.forRoot({ path: ['config2', 'config', 'config3'] })],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

If you use default config dir,you can also...

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from "@martinpang/nestjs-config"; //here

@Module({
  imports: [ConfigModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

So easy to use in providers:

//This is app.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from "@martinpang/nestjs-config"; //here

@Injectable()
export class AppService {
  //Inject via contructor,and use anywhere
  constructor(private configService: ConfigService) {}
  
  getHello(): string {
    console.log(this.configService.get('Your config vars key'));
    return 'Hello World!';
  }
}

To use different env config file

//start your nestjs app in package.json script,config will merge NODE_ENV.EXT and default.EXT
"start:dev": "cross-env NODE_ENV=development nest start --watch",
"start:prod":"cross-env NODE_ENV=production nest start",
//PS.You need cross-env lib or manually set NODE_ENV on your system.

The ConfigMoudle can be used in global scope.You just need import once and use anywhere.

License

Nest is MIT licensed.

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.8

7 months ago

1.1.7

8 months ago

1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago