This package has been deprecated

Author message:

disabling package

config-as-code
TypeScript icon, indicating that this package has built-in type declarations

2.6.0 • Public • Published

CONFIG-AS-CODE

The philosophy which inspire this package is configuration as code. With it you can create configuration classes to set up your express application, your database connection or others packages. Besides that, config-as-code uses nconf, enabling the use of enviroment files and access to process.env variables.

INSTALATION

$ npm install --save config-as-code

BASIC EXAMPLE

Let's take an express configuration. Create the configuration module first:

import { ConfigModule, OnInitConfig } from 'config-as-code';

@ConfigModule('express')
export class ExpressConfigModule implements OnInitConfig {
  init(app: Express) {
    app.use(bodyParser.json());
  }
}

The string passed to the decorator is the module ID, which should be unique and easy to type, because it will be used later.

Now, let's create the application configurator:

import { Configurator, AbstractConfigurator } from 'config-as-code';
import { ExpressConfigModule } from './express.config';

import * as express from 'express';

@Configurator({
  configModules: [ ExpressConfigModule ],
  enviromentFilesPath: 'configuration/enviroments'
})
export class ApplicationConfigurator extends AbstractConfigurator {}

In this, you first set the Configurator decorator, which defines:

  1. configModules: this sets which configuration modules will be executed and in which order.
  2. enviromentFilesPath: the path, relative to the root of the project, to the enviroments folder.

This class implements the AbstractConfigurator that provides behavior for initializing the application and receive an object in which the key should be the configuration module ID and the value an array that will be passed to the init method in configuration module. Additionally, you can pass parameters with the __all ID, which will be passed to every configuration module.

Then, in your startup scprit (e.g. index.ts, server.ts):

import { ApplicationConfigurator } from 'config-as-code';
const config = new ApplicationConfigurator();
config.initApp({
  express: [ app ],
  __all: [ "Application name" ]
});

ENVIROMENTS

Using nconf, you are able to use enviroment files for development, test and production. By default, the package will look for this files in the enviroments subfolder inside the project root folder.

VARIABLES

The Variables store is an interface to the nconf package, allowing access to variables from:

  • process.env
  • enviroment file
  • defaults (the only default variable set is NODE_ENV as development, and it is override if is set in process.env)
  • memory

However, it is only possible to set variables in the memory store, to maintaing consistency in the others stores and to allow objects to be saved.

P.S.: the first three stores will only have data after the application been initalized through Configurator.

The use is simples:

import { Variables } from 'config-as-code';

Variables.set('message', 'Hello World');

console.log(Variables.get('NODE_ENV'));
// By default: development

console.log(Variables.get('message'));
// Hello World

TESTS

Feel free to add them.

TO DO

  • Create Variables class to expose nconf memory store, which support both key/value and object store.
  • Support to set the enviroments file location.
  • Add option to use a json file instead of an array to define load sequence.
  • Enable to config modules to be called individually, allowing more flexibilty on the order where configuration code should run.

Package Sidebar

Install

npm i config-as-code

Weekly Downloads

1

Version

2.6.0

License

MIT

Last publish

Collaborators

  • caiorcferreira