seraphim
Configuration loader
Loading configuration files in Node.js have always been a very tedious task, especially when you need to merge objects in cascade, load files asynchronously, load data from external sources like a database, redis, etc., read the CLI options and the environment variables, etc.
This module brings to you a powerful API for loading and merging your configuration data in a few lines.
var seraphim = ; seraphim //Default settings //NODE_ENV (development) settings //Override/merge the previous settings with any object, eg: CLI options ;
//default.json
{
"web": {
"log": {
"type": "circular",
"backup": "1week"
}
}
}
//development.json
{
"web": {
"hostname": "1.2.3.4",
"port": 1234
}
}
Check this complete example for further details.
Functions
Objects
module.createVault([options]) : Seraphim
Returns a new Seraphim instance.
Options are:
- extensionError - Boolean
Set it to false if unknown extensions shouldn't emit an error. Default is true.
Events
Methods
- Seraphim#extension(extension, fn) : Seraphim
- Seraphim#get() : Object
- Seraphim#load(resource[, onLoad]) : Seraphim
- Seraphim#merge(o1[, o2]) : undefined | Object
Arguments: config
.
This event is emitted multiple times, when there are no more pending tasks to load.
config
is the final merged object.
Look at the end-event.js example for further details.
Arguments: error
.
Emitted when an error occurs.
Seraphim#extension(extension, fn) : Seraphim
Allows you to load files with an extension different from .json using the load() function.
extension
is a string or an array of strings.
fn
is the function that is called when the file to load has the same extension
. It has two arguments: the path of the file and a callback. The callback must be called with two parameters: the error and the object with the configuration data.
seraphim ;
Returns the internal merged object.
Seraphim#load(resource[, onLoad]) : Seraphim
Loads and merges a resource. resource
can be a string, object or function.
String
It must be a valid file path.
;
Object
;
Function
Synchronous. Return the object to be merged. Errors thrown here are catched and forwarded to the error
event. If a falsy value is returned (null, undefined, false, etc.) it won't be merged.
;
Asynchronous. Use the callback to load the next resource. The first parameter is the error, the second is the object to be merged.
;
onLoad
is a callback that is executed when load()
finishes. It has two arguments: the object to be merged and a callback. The callback allows you to execute any asynchronous function between two load()
calls. Please note that if you use the onLoad
callback the object is not merged automatically and you'll need to merge it explicitly. This callback it's also try-catched, errors thrown inside the onLoad
callback are redirected to the error
event.
;
The onLoad
function can be used to load files without an extension.
Seraphim#merge(o1[, o2]) : undefined | Object
If o2
is not used, o1
is merged with the internal object.
If o2
is used, o2
is merged with o1
and o1
is returned.
console;//{ a: 2, b: 1 }