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
- Contents
- Quick example
- Usage
- Testing, coverage and benchmark
- Code coverage
- Performance test
- Road map
- Inspired by
- License
- API
- kea-config
Quick example
Initialization and usage
configManager; // For process.env.NODE_ENV === 'development'; configManager; // 4343 // For process.env.NODE_ENV === 'production'; configManager; // 7474 // If you don't want to apply changes connected to environment // just use init method configManager; configManager; // 3005
File ./config/main.conf.js
var config = {} configweb = 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}' ; moduleexports = config;
File ./config/development.conf.js
var config = {} configweb = port: 4343 ; moduleexports = config;
File ./config/production.conf.js
var config = {} configweb = port: 7474 ; moduleexports = 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; configManager; // also webConfig = configManager; /* { defaultPageSize: 15, numberVisiblePages: 10 } */ // Usage references configManager; // 25 // Usage templates configManager; // 'mongodb://dbUser:strongPassword@localhost:27101/database' - string
Testing, coverage and benchmark
npm test
orgulp mocha
- run testsgulp cover
- check tests coveragegulp benchmark
- rum performance testgulp readme
- prepare README.md fromdocumentation
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
- ASP.NET web.config approach
- lorenwest/node-config
- flatiron/nconf
- dominictarr/config-chain
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
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 | * |
boolean
kea-config.has(key) ⇒ Check availability of key in configuration
Kind: static method of kea-config
Param |
---|
key |