Configuration manager support merging configurations base on Node.js environment, references to other keys and using templates with objects for complex string values
JavaScript
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
documentation
img
src
test
.gitignore
LICENSE
README.md
gulpfile.js
index.js
package.json

README.md

Kea-logo

NPM version

kea-config

Configuration manager for Node.js applications with perfect performance and without dependencies. Main feature of this configuration manager is merging configuration files depending on Node.js environment, support references to other keys and using templates with objects for complex string values.

Video

[![Kea Config: Configuration Manager for node.js](./img/kea-config-first-video-slide.jpg)](https://www.youtube.com/watch?v=P6nTr5T8GVI)

Slides

[![Kea Config: Configuration Manager for node.js](./img/kea-config-presentation-title-slide.png)](http://slides.com/sergiidaniloff/deck/fullscreen)

Contents

Quick example

Initialization and usage

    configManager.setup('./config');
    // For process.env.NODE_ENV === 'development';
    configManager.get('web.port'); // 4343
    // For process.env.NODE_ENV === 'production';
    configManager.get('web.port'); // 7474

    // If you don't want to apply changes connected to environment
    // just use init method
    configManager.init('./config/main.conf.js');
    configManager.get('web.port'); // 3005

File ./config/main.conf.js

    var config = {}

    config.web = {
        port: 3005,
        sessionKey: '6ketaq3cgo315rk9',
        paging: {
            defaultPageSize: 25,
            numberVisiblePages: 10
        },
        mongoDb: {
            username: 'dbUser',
            password: 'strongPassword',
            host: 'localhost',
            port: 27101,
            db: 'database'
        },
        propertyReference: {
            $ref: 'web.paging.defaultPageSize'
        },
        templateReference: {
            $ref: 'web.mongoDb',
            $tmpl: 'mongodb://{username}:{password}@{host}:{port}/{db}'
        }
    };

    module.exports = config;
File ./config/development.conf.js
    var config = {}

    config.web = {
        port: 4343
    };

    module.exports = config;
File ./config/production.conf.js
    var config = {}

    config.web = {
        port: 7474
    };

    module.exports = config;

Usage

Install with npm:

npm install kea-config --save

In your code require the package:

var configManager = require('kea-config');

Setup configuration data

configManager.setup('./config');

This command will initialize configManager by main.conf.js in and then merge development.conf.js (if NODE_ENV === development)

Read (get) and write (set) config data:

    var sessionSecret = configManager.get('web.sessionKey');
    configManager.set('web.paging.defaultPageSize', 15);
    // also
    webConfig = configManager.get('web.paging');
    /*
        {
            defaultPageSize: 15,
            numberVisiblePages: 10
        }
    */

    // Usage references

    configManager.get('web.propertyReference'); // 25

    // Usage templates

    configManager.get('web.templateReference'); // 'mongodb://dbUser:strongPassword@localhost:27101/database' - string

Testing, coverage and benchmark

  • npm test or gulp mocha - run tests
  • gulp cover - check tests coverage
  • gulp benchmark - rum performance test
  • gulp readme - prepare README.md from documentation folder

Code coverage

Statements ................................... 100% ( 123/123 )
Branches ..................................... 96.67% ( 58/60 )
Functions .................................... 100% ( 20/20 )
Lines ........................................ 100% ( 123/123 )

Performance test

Get simple key ............................... 5,799,240 ops/sec +0.47%
Get deep key ................................. 5,488,370 ops/sec +0.31%
Get key with reference ....................... 1,508,501 ops/sec +0.23%
Get key with reference and template .......... 106,877 ops/sec +0.27%
Get key with deep references ................. 274,983 ops/sec +0.80%
Get key with deep references and template .... 108,919 ops/sec +0.25%
Set simple key ............................... 3,446,469 ops/sec +0.26%
Set deep key ................................. 3,135,072 ops/sec +1.82%

Road map

  • support .yml, .yaml, .coffee, .cson, .properties, .json, .json5, .hjson
  • adapters for different storages like (filesystem, DB, localstorage / sessionstorage, web api and etc.)
  • save current state of config
  • delete key

Inspired by

License

MIT

Image from thetartankiwi

API

kea-config

Configuration manager for Node.js applications.

kea-config.setup(dirPath)

Full init config based on environment variable NODE_ENV. If NODE_ENV not available use development as default. This method looking for two files main (name started from 'main' word) and file with name started from environment (like development, staging, production)

Kind: static method of kea-config

Param Type Description
dirPath string | object path to folder with configuration files, from project root

kea-config.init(path)

ConfigManager initialization by data in file. Not save previous configuration.

Kind: static method of kea-config

Param Type Description
path string path to CommonJs module with configuration, from project root

kea-config.update(path)

Update exist configuration. Merge new config to exist.

Kind: static method of kea-config

Param Type Description
path string path to CommonJs module with configuration, from project root

kea-config.setData(data, isMerge)

Set / merge data in configuration manager

Kind: static method of kea-config

Param Type Description
data object configuration data
isMerge boolean should manager merge data to exist configuration

kea-config.getData()

Get whole configuration as an object

Kind: static method of kea-config

kea-config.get(key) ⇒ *

Get 'value' of 'key'.

Kind: static method of kea-config
Returns: * - value - value of key. Can be primitive or javascript object. Objects not connected to original configuration. If value contain reference ({$ref: 'some.reference.to.other.key'}), then return reference value, if value contain reference with template({ $ref: 'some.reference', $tmpl: '{some}:{template}.{string}' }) and reference point to object then return string with populated placeholder in template (look example on top of page).

Param Type Description
key string key in configuration. Like 'simpleKey' or 'section.subsection.complex.key'. See config-managet-test.js

Example

Using deep references ```js // Configuration example { nameValue: 'loginName', dbParams: { username: { $ref: 'web.nameValue' }, password: '12345' }, dbConnection: { user: { $ref: 'web.dbParams' }, key: { $ref: 'web.sessionKey' } }, dbConectionStr: { $ref: 'web.dbConnection', $tmpl: 'db:{user.username}::{user.password}@{key}' } }; configManager.get('dbConnection'); //should return object // { // user: { // username: 'loginName', // password: '12345' // }, // key: '6ketaq3cgo315rk9' // } configManager.get('dbConectionStr'); //should return string 'db:loginName::12345@6ketaq3cgo315rk9' ``` ### kea-config.set(key, value) Set 'value' for 'key'

Kind: static method of kea-config

Param Type Description
key string key in configuration. Like 'simpleKey' or 'section.subsection.complex.key'. See config-managet-test.js
value *

kea-config.has(key) ⇒ boolean

Check availability of key in configuration

Kind: static method of kea-config

Param
key