- Notifications
You must be signed in to change notification settings - Fork4
namshi/reconfig
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
JavaScript configurations as they're meant to be. Kinda.
Reconfig helps you by keeping your configuration clean,decoupled and smart. Don't believe us? Read on.
How do write declarative configuration in JavaScript?
Easy, simply create a JS object:
varconfig={protocol:'https',domain:'example.org',};varurl=config.protocol+'://'+config.domain;// https://example.org
Easy andugly as hell. How can we make the config more elegant?
Enter Reconfig.
varreconfig=require('reconfig');varconfig=newreconfig({protocol:'https',domain:'example.org',});varurl=config.get('protocol')+'://'+config.get('domain');// https://example.org
Blah, this is just some more code...but reconfig is more than that.
With this library you can self-reference config values:
varreconfig=require('reconfig');varconfig=newreconfig({protocol:'https',domain:'example.org',url:'{{ protocol }}://{{ domain }}',});varurl=config.get('url');// https://example.org
Pretty nice right? Even better, reconfig is smart enough to beable to reference parameters at any level of the config:
varreconfig=require('reconfig');varconfig=newreconfig({credentials:{admin:{read:true,write:true},reader:{read:true,write:false}},users:{someImportantDude:{username:'him',password:'...',credentials:'{{ credentials.admin }}'}}});varcanTheImportantDudeWrite=config.get('users.someImportantDude.credentials').write;// true
Nice, as you saw we self-referenced a nested value in the config (credentials.admin
)and we used the same dot-notation to retrieve the credentials of a specific user(users.someImportantDude.credentials
).
It's not over: you can also pass parameters to your configurations!
varreconfig=require('reconfig');varconfig=newreconfig({greet:'Hello :who!'});vargreetJohn=config.get('greet',{who:'John'});// Hello, John!
By default, reconfig uses:
to interpolate the parameters. For example, in the above code,:who
gets replaced by the parameter value.You can specify your own interpolation for resolving the parameters by setting theparamsInterpolation
option.
varreconfig=require('reconfig');varconfig=newreconfig({greet:'Hello {who}!'},{paramsInterpolation:['{','}']});vargreetJohn=config.get('greet',{who:'John'});// Hello, John!
Last but not least, reconfig lets you specify a default value toreturn if a config value you're trying to access doesn't exist:
varreconfig=require('reconfig');varconfig=newreconfig({a:1});config.get('b',{},2);// 2
** Setting you config object **If you wish to change parts of your config on an already existent Reconfig instance:
varreconfig=require('reconfig');varconfig=newreconfig({a:1,b:2,c:'{{a}}_{{b}}'});config.get('c')// 1_2;config.set({b:3});config.get('c')// 1_3;
ENV overriders
So you're using reconfig in your node.js app and you'd like your sys admins to changevalues without touching too much JSON? How about the always familiar ENV vars?
Here's how you do it:
just tell reconfig your env vars prefix:
varreconfig=require('reconfig');varconfigValues={a:1,b:[2]};varconfig=newreconfig(configValues,{envPrefix:'MYPREFIX'});
and you'll be able to modify or add new values directly from your shell!
export or add your vars before runnig your node app:
MYPREFIX_a=3 MYPREFIX_b_0=4 MYPREFIX_c=5 node app.js
and here's what you'll get:
config.get('a')// 3config.get('b')[0]// 4 <-- Pay attention!! It works with arrays too! :Dconfig.get('c')// 5
The default properties separator is:_
(1 underscore).You can use your custom separator passing it to the constructor as 3rd parameter:
varconfig=newreconfig(configValues,{envPrefix:'MYPREFIX',separator:'__'});
Install this library viaNPM:
npm install reconfig
If you need it on the client side we highly recommendbrowserify.
This library is tested through mocha, simply run either
mocha
or
./node_modules/mocha/bin/mocha
if you don't have mocha installed globally.
About
JavaScript configurations as they're meant to be. Kinda.