

Overview
This module is a part of yocto node modules for NodeJS.
Please see our NPM repository for complete list of available tools (completed day after day).
This module provide a simple config validator tools for a node app.
Motivation
Our main motivation for this module is, create and provide a single, simple and custom config manager (with validation) for each part of our program in the same place.
How validation schema works ?
Default validation schema was build with joi package manager, and all custom all schema must be associate with a joi rules too.
During load process given files was auto validated with associated schema.
Installation
npm install --save yocto-config
Config files priority
Priority : (Other file).json
< all.json
< common.json
< development.json
< stagging.json
< production.json
all.json : place in this file all general property (shared key, node port, etc)
common.json : place in this file all common data between each env (app name, express config, etc)
development.json : place in this file all development property for development environnement
stagging.json : place in this file all stagging property for stagging environnement
production.json : place in this file all production property for production environnement
IMPORTANT : file was merged with previous defined priority, so it should be understood that we dont need to define multiple times the same property if we doesn't need to replace it. JUST PLACE IN CORRECT ENV WHAT YOU NEED
For example :
{
"test" : {
"db" : {
"uri" : "http://test.com/123456"
}
}
{
"test" : {
"db" : {
"options" : {
"op1" : "my-value1",
"op2" : "my-value2"
}
}
}
Will produce on production this config data :
{
"test" : {
"db" : {
"uri" : "http://test.com/123456",
"options" : {
"op1" : "my-value1",
"op2" : "my-value2"
}
}
}
Pre-defined configuration
Predefined configuration schema was availabe. To use them see methods below :
All of these function was replaced by default an already defined configuration.
If you want to enable a new configuration and keep safe the previous configuration, just pass true
on enable function, and the new config will be append on the previous content, for example :
var config = require('yocto-config')();
config.enableExpress();
config.enableMongoose(true);
Adding custom config schema
You can add your custom config schema for custom validation, two methods was available for these action :
- addCustomSchema(name, schema, autoenable, keepSafePreviousSchema) : add new schema with given name
- enableSchema(name, keepSafePreviousSchema) : enable a schema with given name
Example :
var config = require('yocto-config')();
var joi = require('joi');
var schema = joi.object().required().keys({
mailer : joi.object().required(),
daemon : joi.object().required(),
core : joi.object().keys({
daemon : joi.object().default({ delay : 10, retry : 10 }).keys({
delay : joi.number().min(0).default(10),
retry : joi.number().min(0).default(10)
}).allow([ 'delay', 'retry' ]),
mailer : joi.object().default({ limit : 1, sort : 'ascending' }).keys({
limit : joi.number().min(1).default(1),
sort : joi.string().empty().default('ascending').allow([ 'ascending', 'descending' ]),
}).allow([ 'limit', 'sort' ])
}).allow([ 'daemon', 'mailer' ])
}).unknown();
config.addCustomSchema('test', schema, true, true);
config.addCustomSchema('test', schema);
config.enableSchema('test');
How to use
var config = require('yocto-config')();
config.enableExpress(true);
config.enableMongoose(true);
config.enablePassportJs(true);
config.setConfigPath('./example/config');
config.load().then(function(data) {
var c = config.getConfig();
console.log(c);
}).catch(function(error) {
});
How to change config on the fly ?
In some case we need to use same core app for different apps.
To change the config path for each app during run it's possible to use process.env
.
To use it run during start your app use these params CONFIG_SUFFIX_PATH
.
CONFIG_SUFFIX_PATH='suffix/base/path' node app
Tricks
You can also use a utility method autoEnableValidators
to enable your validator. See below key associated with schema :
express
for express schema
passportJs
for passportJs schema
mongoose
for mongoose schema
render
for yocto-render module schema
router
for yocto-router module schema
Example :
var config = require('yocto-config')();
config.autoEnableValidators([ 'express', 'passportJs', 'mongoose' ]);
Default configuration rules
- Mongoose schema can be find here
- Express schema can be find here
- Yocto Render schema can be find here
- Yocto Router schema can be find here
- Yocto PassportJS schema can be find here
Logging in tool
By Default this module include yocto-logger for logging.
It's possible to inject in your config instance your current logger instance if is another yocto-logger
instance.
Changelog
All history is here
Full Documenation
You can find full online documentation here