Skip to content

JavaScript configurations as they're meant to be. Kinda.

Notifications You must be signed in to change notification settings

namshi/reconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reconfig

Build Status

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.

The problem

How do write declarative configuration in JavaScript?

Easy, simply create a JS object:

var config = {
    protocol:   'https',
    domain:     'example.org',
};

var url = config.protocol + ':https://' + config.domain; // https://example.org

Easy and ugly as hell. How can we make the config more elegant?

The Solution

Enter Reconfig.

var reconfig = require('reconfig');

var config =  new reconfig({
    protocol:   'https',
    domain:     'example.org',
});

var url = config.get('protocol') + ':https://' + 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:

var reconfig = require('reconfig');

var config =  new reconfig({
    protocol:   'https',
    domain:     'example.org',
    url:        '{{ protocol }}:https://{{ domain }}',
});

var url = config.get('url'); // https://example.org

Pretty nice right? Even better, reconfig is smart enough to be able to reference parameters at any level of the config:

var reconfig = require('reconfig');

var config =  new reconfig({
    credentials: {
        admin: {
            read:   true,
            write:  true
        },
        reader: {
            read:   true,
            write:  false
        }
    },
    users: {
        someImportantDude: {
            username:       'him',
            password:       '...',
            credentials:    '{{ credentials.admin }}'
        }
    }
});

var canTheImportantDudeWrite = 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!

var reconfig = require('reconfig');

var config =  new reconfig({
    greet: 'Hello :who!'
});

var greetJohn = 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 the paramsInterpolation option.

var reconfig = require('reconfig');

var config =  new reconfig({
    greet: 'Hello {who}!'
}, { 
    paramsInterpolation: ['{', '}'] 
});

var greetJohn = config.get('greet', {who: 'John'}); // Hello, John!

Last but not least, reconfig lets you specify a default value to return if a config value you're trying to access doesn't exist:

var reconfig = require('reconfig');

var config =  new reconfig({
    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:

var reconfig = require('reconfig');

var config =  new reconfig({
    a: 1,
    b: 2,
    c: '{{a}}_{{b}}'
});

config.get('c') // 1_2;

config.set({b: 3});

config.get('c') // 1_3;

Nodejs specifics

ENV overriders

So you're using reconfig in your node.js app and you'd like your sys admins to change values 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:

var reconfig = require('reconfig');

var configValues = {
    a: 1,
    b: [2]
};

var config =  new reconfig(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')     // 3
config.get('b')[0]  // 4  <-- Pay attention!! It works with arrays too! :D
config.get('c')     // 5

The default properties separator is: _ (1 underscore). You can use your custom separator passing it to the constructor as 3rd parameter:

var config =  new reconfig(configValues, { envPrefix: 'MYPREFIX', separator: '__' });

Installation

Install this library via NPM:

npm install reconfig

If you need it on the client side we highly recommend browserify.

Tests

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.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •