0.2.1 • Published 3 days ago

@softkit/config v0.2.1

Weekly downloads
-
License
-
Repository
-
Last release
3 days ago

Config Library

This library provides a set of general services and utilities for configuration. It can be used outside Softkit projects

This library is based on nestjs-config All main work is done in parent library, this one is just a wrapper to make it easier to use


Installation

yarn add @softkit/config

Usage

import { setupYamlBaseConfigModule } from '@softkit/config';


@Module({
  imports: [
    setupYamlBaseConfigModule(path.join(__dirname, './assets'), RootConfig),
  ]
})
export class YourAppModule {}

./assets - is a folder where you have your config files. Above example is for such code structure:

.
├── assets
│     ├── .env-test.yaml
│     └── .env.yaml
├── config
│     └── root.config.ts
└── your-app.module.ts

This wrapper has a few additions:

  • It has PROFILES feature, so you can have different configs for different environments.
    • NESTJS_PROFILES - is an environment variable, which is used to define which profile to use
    • By default, it's no profiles and only main provided file name will be used
    • The order of profiles is important it defines how to merge configs in case if there will be any conflicts
    • Example:
      • NESTJS_PROFILES=dev,local - will use .env-dev.yaml and .env-local.yaml files and base .env.yaml
      • NESTJS_PROFILES=dev - will use only .env-dev.yaml file and base .env.yaml
    • By default, we recommend to set in jest config a test profile. In jest.preset.js
       process.env.NESTJS_PROFILES = 'test';
  • Another adjustments is adding a general alias for RootConfig class, so we can reuse it across various apps in the same way.
    • To inject a config in another library you just need to use common token ROOT_CONFIG_ALIAS_TOKEN
    • We leverage NestJS DI use existing feature to provide a config in a common way
      {
        provide: ROOT_CONFIG_ALIAS_TOKEN,
        useExisting: rootSchemaClass,
      }
    • In your library you can expect this Provider be available globally, and you can force this config to implement your interface. So you will be able to decouple application very well, and declarative define what config you need to use in your library.
      • Example in your library:

        import { Inject, Injectable } from '@nestjs/common';
        import { ROOT_CONFIG_ALIAS_TOKEN, RootConfig } from '@softkit/config';
        import { YourConfigInterface } from './your-config.interface';
        
        @Injectable()
        export class YourService {
          constructor(
            @Inject(ROOT_CONFIG_ALIAS_TOKEN)
            private readonly config: YourConfigInterface,
          ) {}
        
          getYourConfig(): YourConfigInterface {
            return this.config.yourConfig;
          }
        }

Example config file structure

# .env.yaml
isAuthEnabled: ${AUTH_ENABLED:-true}
database:
  host: ${DATABASE_HOST:-127.0.0.1}
  port: ${DATABASE_PORT:-3000}
  table:
    name: ${TABLE_NAME:-users}

databaseAlias:
  host: ${database.host}
  port: ${database.port}
  table:
    name: ${database.table.name}

As you can see you can leverage ENV variables in your config files, or use a value to reference another value from your config file

Your .env.yaml file is usually huge and contains a lot of configs, so it's not very convenient to use it in your code. Where the profile files, just overriding some specific things for needed environment

Let's say that for tests we always will be connecting to port 12345.

You just need to create this file and it will be used for tests, when you set env var NESTJS_PROFILES=test:

# .env-test.yaml
database:
  port: 12345
0.2.1

3 days ago

0.2.0

4 months ago

0.1.3

4 months ago

0.1.2

4 months ago

0.1.1

6 months ago

0.1.0

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago