Class FileConfig

Description

Module for configuring the application using files, extends Config base class.

Features:

  • default configuration in config.json file
  • configuration depending on the NODE_ENV environment variable in config.{NODE_ENV}.json file
  • package.json data access in "package" namespace

How to use:

In cwd of you app, near package.json, create 2 files:

# config.json
{
"app": "test"
}

# config.debug.json:
{
"title": "My awesome app"
}

Example

import { Core, injectable, inject, container, Types } from '@biorate/inversion';
import { IConfig } from '@biorate/config';
import { FileConfig } from '@biorate/file-config';

@injectable()
class Root extends Core() {
@inject(Types.Config) public config: IConfig;
}

container.bind<IConfig>(Types.Config)
.to(FileConfig)
.inSingletonScope();

container.bind<Root>(Root).toSelf().inSingletonScope();

(async () => {
const root = await container.get(Root).$run();

console.log(root.config.get('package'));
// {
// "name": "file-config-test",
// "version": "0.0.0",
// "description": "Test package.json",
// "keywords": [],
// "author": "llevkin",
// "license": "MIT"
// }

console.log(root.config.get('app')); // test
console.log(root.config.get('title')); // My awesome app
})();

Hierarchy

Constructors

Properties

data: {} = {}

Type declaration

    Description

    Data storage

    _root: string = ...

    Methods

    • Type Parameters

      • T = unknown

      Parameters

      • path: PropertyPath

        data path

      • Optional def: T

        default value

        Optional

      Returns T

      Description

      Get config property by path

      Example

      import { Config } from '@biorate/config';

      const config = new Config();

      config.set('a', 1);

      console.log(config.get('a')); // 1
      console.log(config.get('b', 2)); // 2
      console.log(config.get('b')); // UndefinedConfigPathError: Undefined config path [b]
      // at Config.get (src/index.ts:2:1608)
      // at Context.<anonymous> (tests/index.spec.ts:19:24)
      // at processImmediate (node:internal/timers:464:21)
    • Parameters

      • path: PropertyPath

        data path

      Returns boolean

      Description

      Has config property by path

      Example

      import { Config } from '@biorate/config';

      const config = new Config();

      config.set('a', 1);

      console.log(config.has('a')); // true
    • Parameters

      • data: unknown

        data object

      Returns void

      Description

      Merge config data

      Example

      import { Config } from '@biorate/config';

      const config = new Config();

      config.merge({
      a: { b: { c: 1 } },
      });

      config.merge({
      a: { b: { d: 2 } },
      });

      console.log(config.has('a')); // true
      console.log(config.has('a.b')); // true
      console.log(config.get('a.b.c')); // 1
      console.log(config.get('a.b.d')); // 2
    • Parameters

      • path: PropertyPath

        data path

      • value: unknown

        data value

      Returns void

      Description

      Set config property by path

      Example

      import { Config } from '@biorate/config';

      const config = new Config();

      config.set('a', 1);

      console.log(config.get('a')); // 1
    • Parameters

      • path: string

      Returns typeof FileConfig

      Description

      Change application root. Application root is equal process.cwd() by default

      Example

      FileConfig.root('/www/my-app/');
      

      Example

      container.bind<IConfig>(Types.Config)
      .to(FileConfig.root('/www/my-app/'))
      .inSingletonScope();

    Generated using TypeDoc