kea-config
v0.2.3
Published
Configuration manager support merging configurations base on Node.js environment, references to other keys and using templates with objects for complex string values
Downloads
74
Maintainers
Readme
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.
- Contents
- Quick example
- Usage
- Testing, coverage and benchmark
- Code coverage
- Performance test
- Road map
- Inspired by
- License
- API
- kea-config
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
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;
var config = {}
config.web = {
port: 4343
};
module.exports = config;
var config = {}
config.web = {
port: 7474
};
module.exports = config;
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
npm test
orgulp mocha
- run testsgulp cover
- check tests coveragegulp benchmark
- rum performance testgulp readme
- prepare README.md fromdocumentation
folder
Statements ................................... 100% ( 123/123 )
Branches ..................................... 96.67% ( 58/60 )
Functions .................................... 100% ( 20/20 )
Lines ........................................ 100% ( 123/123 )
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%
- 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
- ASP.NET web.config approach
- lorenwest/node-config
- flatiron/nconf
- dominictarr/config-chain
MIT
Image from thetartankiwi
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 | * | |
kea-config.has(key) ⇒ boolean
Check availability of key in configuration
Kind: static method of kea-config
| Param | | --- | | key |