config-getter
v0.3.0
Published
JSON config reader with different ways of variable overrides and placeholders!
Downloads
26
Maintainers
Readme
config-getter
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
Using with import:
// @ts-ignore
import configGetter from 'config-getter';
import defaultConfig from './default.js';
export default configGetter.extendConfig(defaultConfig, {
// ... parameters ... //
};
Old way:
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 ----- */
// function to get log messages
log: console.log,
// 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
$ npm install
$ npm test
Author
License
MIT