boldr-config v0.1.4
boldr-config
Boldr Config is a tool for configuring your JavaScript applications. Sharing similarities, yet expanding upon the shortfalls of other configuration tools.
Configuration values can be loaded from argv flags, environment variables, JSON config files, and YAML files.
Files and Loading Priority
Boldr Config crawls multiple paths searching for configuration files. Once found, the values are merged based on a hierarchy (explained below), placed into a single object, and loaded into your application.
- argv flags passed through the command line.
--db--host value
translates toconfig.db.host === 'value'
- Environment variables
defined as
MY_APP__DB__HOST_NAME=value
translates toconfig.db.hostName === 'value'
Anything below can be json5 or yaml (requires installing js-yaml) - ~/.appNamerc
- ~/.appName/config
- ~/.config/appName
- ~/.config/appName/config
- /etc/appNamerc
- /etc/appName/config
- /usr/local/etc/appNamerc
- /usr/local/etc/appName/config
- ./.appNamerc
- ../.appNamerc
- ../../.appNamerc
- ...
Usage
Create a new instance of BoldrConfig, pass it your application name 'boldr' followed by an object containing keys and values for your application. This is the default config.
### config.js
import BoldrConfig from 'boldr-config';
// new instance 'name', keys/values
const boldrConfig = new BoldrConfig('boldr', {
server: {
port: 2121,
host: '127.0.0.1',
apiPrefix: '/api/v1',
siteUrl: 'http://localhost:3000',
},
logging: {
level: 'debug',
file: {
enable: true,
dir: 'logs',
level: 'info',
filename: 'boldr.api',
},
},
db: {
url: 'postgres://postgres:password@127.0.0.1:5432/boldr',
name: 'boldr',
debug: false,
},
redis: {
url: 'redis://127.0.0.1:6379/0',
},
token: {
secret: 'v3ryS3cretT0k3n',
},
mail: {
host: 'smtp.example.com',
port: 465,
ssl: true,
user: 'email@email.com',
password: 'password',
from: 'email@email.com',
},
cors: {
whitelist: ['http://localhost:2121', 'http://localhost:3000'],
},
});
// create a constant config and tell your instance of boldrConfig to transform it to an object. Export config or whatever you called it and you can then import it anywhere in your app and retrieve values as normal.
// ex: const db = config.db.name
const config = boldrConfig.toObject();
export default config;
// What is this? See below on how you can use it.
export { boldrConfig };
Display Config
It's possible to print out your configuration to stdout. When the configuration is printed, the source where the value was loaded from is also displayed.
To take advantage of this feature, call the displayConfig()
method on your BoldrConfig instance.
...^
import BoldrConfig from 'boldr-config';
const boldrConfig = new BoldrConfig('boldr', { ... });
const config = boldrConfig.toObject();
export { boldrConfig };
## server.js
import config, { boldrConfig } from './config';
console.log(boldrConfig.displayConfig());
Executing the displayConfig method will print what you see below to stdout.
Displaying config for boldr
┌───────────────────────┬─────────────────────────────────────────────┬
│ Path │ Value │ Source │
│ server.port │ 2121 │ .boldrrc │
│ server.host │ "127.0.0.1" │ .boldrrc │
│ server.apiPrefix │ "/api/v1" │ defaults │
│ server.siteUrl │ "http://localhost:3000" │ defaults │
│ logging.level │ "debug" │ .boldrrc │
│ logging.file.enable │ true │ defaults │
│ db.debug │ false │ defaults │
│ redis.url │ "redis://redis:6379/0" │ environment │
└──────────────────┴───────────────────────────────────────────────────┴