pragma-config

High-level configuration system for node.js application

npm install pragma-config
2 downloads in the last week
8 downloads in the last month

Pragma Config Build Status NPM version

This is a high-level configuration system for your application. You just need to create a default configuration file defaults.json and put it near initial script. Then all you need to do is to create instance of Pragma Config constructor and it will already have all default values inside. If you want to apply current configuration just pass path to configuration file or config object itself in constructor. Specified configuration and default configuration will be automatically merged including all nested levels.

Pragma Config has a few useful features:

  • Two-phase configuration: default and current values
  • Pragma Config instance merges default and current values into one consolidated configuration including all nested levels
  • Default values are loaded from defaults.json placed near you initial script or you can specify other in Pragma Config constructor
  • You can specify object or path to file as current or default configuration into Pragma Config constructor
  • You can also specify directory path as current or default configuration into Pragma Config constructor, and all JSON files in directory will become a config sections like 'root'
  • If you specify path to file into Pragma Config constructor then all file changes will be applied to configuration in runtime
  • All configuration starts with 'root' property of Pragma Config and it is read only including all nested levels (changing only by file changes)
  • You can create many instances of Pragma Config, there are no conflicts

Usage

To install just use NPM

npm install pragma-config

Put defaults.json near your initial script

{
    "howManyLegsHasACat": 4,
    "cat":{
        "eyes": "suspicious",
        "mind": "wild",
        "color":"white"    
    }
}

Then use the force of node.js

var PragmaConfig = require('pragma-config'),

    // you can specify a config object or path to json file in constructor
    config = new PragmaConfig({
    superParameter: 'superValue!',
    cat: {
        eyes: 'happy',
        mind: 'calm'
    }
});

// here is config file change event
// (to try this, please start main.js and edit defaults.json file)
config.on('change', function () {
    // fast way is to use root section getter
    console.log(config.root.superParameter);
    console.log(config.root.howManyLegsHasACat);
    // $$ is a short root alias
    console.log(config.$$.cat.color);
    console.log(config.$$.cat.mind);
});

// another exception-free value getter
console.log(config.$('superParameter')); // superValue
console.log(config.$('howManyLegsHasACat')); // 4
console.log(config.$('cat.color')); // white
console.log(config.$('cat.mind')); // calm

// here example for whole directory 'currents' with files 'section1.json' and 'section2.json'
var directoryTest = new PragmaConfig('./currents');

// if 'section1.json' is changed this event will tell you about
directoryTest.on('section1Change',function(){
    console.log(directoryTest.section1.someValue);
});

// lets output some value from 'section2.json'
console.log(directoryTest.section2.someValue);

// and of course you can specify directory as default values too
var directoryTest2 = new PragmaConfig('./currents', './defaults');
// it will merge sections accordingly with file names in these directories

Pragma Dudes wish you live long and prosper.

info@pragma-dudes.org

npm loves you