cfgapp
v1.0.5
Published
Application config management library
Downloads
2
Readme
NodeJS config repository
Dotenv wrapper
Set up config
Configuration singleton is created in the main file. If defaultsPath property is provided defaults are red at the moment of config creation. Defaults must be a JSON file.
import {createAppConfig} from 'cfgapp';
createAppConfig([{type: 'json', source: './constants.json'}]);
Use config
Config can be accessed anywhere in application right after createAppConfig()
call.
Options from .env
file overrides defaults (if any are loaded). For example if option exists in both .env
and
defaults file, value from .env
will be returned by get()
call.
In cases when option itself must persist in .env
for documentation purposes but contain default value, option's value
can be set to "~" (tilde).
import appCfg from 'cfgapp';
const result = appCfg('MY_OPTION');
console.log(result);
Advanced usage
Sometimes it is not enough to have option value only as string. For example you might need to load structured config into some factory object. In those cases you can create option with object structure in defaults file. And override only environment specific parts.
defaults.json
{
"COMPLEX_OPT": {
"user": "johndoe",
"pass": "%PASS%"
}
}
.env
PASS=TheVerySecretPass
import {createAppConfig, appCfgObject} from 'cfgapp';
createAppConfig([{type: 'json', source: './constants.json'}]);
const result = appCfgObject('COMPLEX_OPT');
console.log(result['pass']); // outputs: TheVerySecretPass