GithubHelp home page GithubHelp logo

nestjs-config's Introduction

Description


Configuration module for Nest based on: dotenv (to load process environment variables), YAML (to load .yml|.yaml configuration files);

Allows reading recursively a config folder and loading all configuration files to a unique object, creating a prototype chain for configuration properties inheritance;

Installation

yarn add nestjs-rconfig-module

Stay in touch


Usage


  1. Create configuration folder structure: Example
config
|   db.yml
|   .env
|-- users
|    |   db.yml
|    |   users.yml
|    |   .env

config/db.yml

dbname: db
port: 5432
host: db.host
username: user
password: pass
schemas:
  - public
  - config

config/.env

root=root
passw=root
port=3000

config/users/db.yml

dbname: users
schemas:
  - users

config/users/users.yml

strategies:
  - local
  - jwt

config/users/.env

CACHE=1
  1. Import ConfigModule into root application
import { ConfigModule } from 'nestjs-rconfig-module';
@Module({
  imports: [
    ConfigModule.forRoot({})
  ]
})
export class AppModule {}
  1. Import ConfigModule into another child module
@Module({
  imports: [
    /***
     * Allows pass a path ex: users.schema
     * */
    ConfigModule.forModule('users')
  ]
})
export class UsersModule {}
  1. Inject ConfigProvider into ModuleProvider
@Injectable()
export class UsersService {
  constructor(
    /***
     * Project general configuration 
     * (use only when its required, otherwise ignore injection)
     * */
    private project_config: ConfigService,
    /***
     * Module specific configuration using protoype chaining
     * */
    @Inject('users') private module_config: ModuleConfigService,
  ) {}
  getHello(): string {
    const dbname = this.module_config.config.dbname;
    return 'Hello World!'; //
  }
}

  1. Result
console.log(this.module_config.config)

Output

{
    "strategies": ["local", "jwt"],
    "CACHE": "1",
    "db":{
        "dbname": "users",
        "schemas": ["public", "config", "users"],
        "__proto__": {
            "dbname": "db",
            "port": 5432,
            "host": "db.host",
            "username": "user",
            "password": "pass",
            "schemas": ["public", "config"]
        }
    },
    "__proto__": {
        "root": "root",
        "passw": "root",
        "port": 3000,
        "db": {
            "dbname": "db",
            "port": 5432,
            "host": "db.host",
            "username": "user",
            "password": "pass",
            "schemas": ["public", "config"]
        },
        "users": {...}
    }
}

nestjs-config's People

Contributors

john891226 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.