A simple JSON configuration loading utility.
- Look for a configuration file from a list of possible locations, and load the first one.
- Create a composite configuration using patches from another config file or from the environment.
- It allows you to compose your configuration from different sources in one block of code.
- It does not need any setups with any predefined directories. You're free.
- Though environment JSON prototyping exists on other libraries, config-discovery gives you the freedom on how to organize your prototypes.
This utility was built with containerizing backend NodeJS applications in mind. Configuration setups in containers almost always involve the use of K8s ConfigMaps and Secrets, which are mounted either as a file and/or as environment variables, config-discovery takes care of locating and compositing these configurations into a single json object for you.
- Stream lined interface for both patching, and find first method names.
- Added support for Environment and JSON objects in for* and or* method families.
- Added support for JSON objects in patching methods.
- Removed tests from npm package, make it smaller.
let Config = require('config-discovery');
...
let debugValues = {user: 'DebugDB', password: 'password'};
let configuration = new Config()
.fromFile('/other/env/directory/config.json')
.orObject(debugValues)
.get();
////// OR
let prototype = {user: 'DB_USERNAME', password: 'DB_PASSWORD'}
let configuration = new Config()
.fromFile('/other/env/directory/config.json')
.orEnv(prototype)
.get();
////// OR
let debugConfig = '/local/conf/myconfig.json'
let configuration = new Config()
.fromFile('/other/env/directory/config.json')
.orFile(debugConfig)
.get();
const knex = Knex(configuration)
let Config = require('config-discovery');
...
let configuration = new Config()
.fromFile('/configs/config.json')
.get();
const knex = Knex(configuration)
This will load the first configuration it finds, starting from fromFile().
let Config = require('config-discovery');
...
let configuration = new Config()
.fromFile('/configs/config.json')
.orFile('/configuration/config.json')
.orFile('/etc/my_configs/config.json')
.get();
const knex = Knex(configuration)
This happens when sensitive data are provided with K8S Secrets, which can be mounted as a set of environment variables. Wrangle them into a JSON with a prototype!
let Config = require('config-discovery');
...
let envPrototype = {connection: {user: 'SECRET_DB_USERNAME', password: 'SECRET_DB_PASSWORD'}};
let configuration = new Config()
.fromFile('/configs/config.json')
.orFile('/configuration/config.json')
.orFile('/etc/my_configs/config.json')
.thenPatchWith()
.env(prototype)
.get();
const knex = Knex(configuration)
Or simply patch with another file.
let Config = require('config-discovery');
...
let configuration = new Config()
.fromFile('/configs/config.json')
.orFile('/configuration/config.json')
.orFile('/etc/my_configs/config.json')
.thenPatchWith()
.configFile('/var/secrets.json')
.get();
const knex = Knex(configuration)