0.1.0 • Published 7 years ago

config-getter v0.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
7 years ago

config-getter

Build Status NPM version

Simple json configuration reader with file and env variables overrides. Can fetch / override variables using:

  • Local file
  • Env variables
  • Consul key-value

For 0.0.x versions documentation see older README.md

Installation

$ npm install config-getter

Usage

default.js

module.exports = {
    "db": {
        "host": "localhost",
        "port": "12345"
    }
}

overrides.json

{
    "db": {
        "host": "google.com"
    }
}

app.js

const getConfig = require('config-getter').getConfig;

//
// Obtain final configuration object.
//
// Important Notice: even that underlying fetchers use async IO,
// interface to getConfig is sync for usability purposes. 
// As a result, Application that use config-getter should do getConfig once at startup time.
//
let config = getConfig(__dirname + '/default.js', {

    /* ----- optional parameters ----- */

    // if true, replaces arrays values
    // If false, merge arrays
    replaceArrays: false,

    // if true, replaces objects values
    // If false, merge objects
    replaceObjects: false,

    // If true, ignore fetcher exceptions and continue work
    // If false, throw/propagate all exceptions
    ignoreFailedFetchers: false,

    // Optional fetchers
    // Values from these fetchers will be merged into default config
    // in order of their appear in the following array:
    fetchers: [

        /* ----- one or more of the following ----- */

        {
            type: 'consul',
            opts: {
                baseUrl: 'http://localhost:8500/v1',  // base url for Consul REST API
                key: 'config'                         // key to look for JSON value
            }
        },

        {
            type: 'file',
            opts: {
                path: process.env.PATH_TO_CONFIG       // path to json file
            }
        },

        {
            type: 'env',
            opts: {
                prefix: 'CONFIG_',                    // prefix of env variable used to override values
                ignore: '^PATH_TO_CONFIG$'            // regexp to ignore env variable if there is a match
            }
        }
    ]

});

console.log(config);

Simple config loading

$ node app.js

Override config with overrides.json values

$ PATH_TO_CONFIG=./overrides.json node app.js

Override config with overrides.json values and single key value

$ PATH_TO_CONFIG=./overrides.json CONFIG_db_port=7777 node app.js

Placeholders

It's possible to make a reference to previous defined value in some config string value:

{
   "who": "world",
   "phrase": "hello, $(who)!"
}

So phrase will become "hello, world!". It is possbile to make reference in inner objects too:

"$(some.obj.value)"

Placeholders also may be relative. Currently the same level, and parent level are supported.

{
    "a": {
        "a1": {
            "name": "Vasya",
            "phrase": "My name is $(.name)" // link to the same level var starts with single dot '.'
       },
       "a2": {
           "name": "$(..a1.name)" // link to upper level var starts with two dots '..'
       }
    }
}

Tests

$ sudo npm install nodeunit -g
$ npm test

Author

License

MIT

0.1.0

7 years ago

0.0.7

8 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago