yocto-config
v3.0.1
Published
A simple config manager for node app
Downloads
24
Maintainers
Readme
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 :
// a development.json file with these property
{
"test" : {
"db" : {
"uri" : "http://test.com/123456"
}
}
// a production.json file with these property
{
"test" : {
"db" : {
"options" : {
"op1" : "my-value1",
"op2" : "my-value2"
}
}
}
Will produce on production this config data :
// generated config in production env
{
"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 :
- enableExpress : add to default schema an express configuration
- enableMongoose : add to default schema a mongoose configuration
- enablePassportJs : add to default schema a passportjs configuration for (facebook, twitter, google, active-directory, standard login)
- enableRender : add to default schema a yocto-render configuration
- enableRouter : add to default schema a yocto-router configuration
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')();
// defined base config
config.enableExpress();
// add new config for mongoose and keep safe previous defined config
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');
// define your schema
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();
// add custom schema named 'test'
// third parameter enable given schema (optional, by default to false)
// fourth parameter keep safe all predefined schema (optional, by default to false)
config.addCustomSchema('test', schema, true, true);
// OR classic process
config.addCustomSchema('test', schema);
config.enableSchema('test');
How to use
var config = require('yocto-config')();
// enable defined config
config.enableExpress(true);
config.enableMongoose(true);
config.enablePassportJs(true);
// Set config path
config.setConfigPath('./example/config');
// Load and retreive data from callback
config.load().then(function(data) {
// your code here
// you can also get data with a method
var c = config.getConfig();
console.log(c);
}).catch(function(error) {
// your code here
});
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 schemapassportJs
for passportJs schemamongoose
for mongoose schemarender
for yocto-render module schemarouter
for yocto-router module schema
Example :
var config = require('yocto-config')();
config.autoEnableValidators([ 'express', 'passportJs', 'mongoose' ]);
// Extra process
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