Tree-config organizes hierarchical configurations for your app deployments.
It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).
Configurations are stored in configuration files within your application, and can be overridden and extended.
Installation is fairly straightforward, just install the npm module:
$ npm install tree-config
Create a configuration file for your application, JSON format.
config.json
{
"db": {
"mysql": {
"server": "localhost",
"port": 3306,
"database": "myapp",
"user": "root",
"password": ""
}
}
}
Use the config in your code:
var config = require('tree-config');
config.configure({
sources: [
{
type: 'json',
src: 'config.json'
}
]
});
// Get config value `db.mysql.server`
// `server` variable will have value `localhost`
var server = config.get('db.mysql.server');
To override the defaults, create a configuration file with with the desired values.
myconfig.json
{
"db": {
"mysql": {
"database": "mydbapp",
"user": "dbuser",
"password": "dbpass"
}
}
}
In your code:
var config = require('tree-config');
config.configure({
sources: [
{
type: 'json',
src: 'config.json'
},{
type: 'json',
src: 'myconfig.json'
}
]
});
// Get config value `db.mysql.database`
// `database` variable will have value `mydbapp`
var database = config.get('db.mysql.database');
You can set default values in the application.
In your code:
config.setDefaults({
db: {
mysql: {
server: 'localhost',
port: 3306,
...
}
}
});
In your code:
config.configure({
sources: [
{
cwd: process.cwd(),
key: 'mypackage',
type: 'json',
src: 'package.json'
}
]
});
var appName = config.get('mypackage.name');
Create a configuration file for your application module.
Application structure:
/
config.json
package.json
...
modules/
user/
config.json
package.json
...
In your code:
var config = require('tree-config');
config.configure({
sources: [
{
type: 'json',
src: 'config.json'
},{
type: 'json',
key: 'package',
src: 'myconfig.json'
}
]
});
config.setDefaults({
db: {
mysql: {
server: 'localhost',
port: 3306,
...
}
}
});
var cwdChildConfig = path.join(process.cwd(), 'modules', 'user');
var childConfig = config.children.create('user-module);
childConfig.configure({
sources: [
{
type: 'json',
cwd: cwdChildConfig,
src: 'config.json'
},{
type: 'json',
key: 'package',
cwd: cwdChildConfig,
src: 'myconfig.json'
}
]
});
childConfig.setDefaults({
db: {
mysql: {
database: 'userdb'
}
}
});
// `database` variable will have value `userdb`
var database = childConfig.get('db.mysql.database');
In your code:
...
var database = childConfig.get('^.db.mysql.database');
...
See more examples in the test folder