Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

yocto-config

yoctore30Apache-2.03.0.1

A simple config manager for node app

config, express, mongoose, passport, data, tool, schema

readme

NPM

alt text Code Climate Test Coverage Issue Count Build Status

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 :

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 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' ]);
// 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

changelog

3.0.0 (2018-02-14)

  • Major update of all dependencies
  • Update node version engine to >= 8.9.4

2.0.2 (2017-04-20)

  • Add into express schema the object staticServe

2.0.0 (2016-10-07)

  • Major package update dependencies
  • Add new env value CONFIG_SUFFIX_PATH to append like a suffix on the end of config path
  • Refactoring and merging all schema Class in a single class
  • Adding documentation form yocto-doc
  • Add more unit tests

1.8.2 (2016-10-07)

  • Add config for fingerprint for render part

1.8.2 (2016-09-27)

  • Update config and add new rules for seo redirection and www redirection process

1.8.1 (2016-09-14)

  • Update version for github and npm

1.8.0 (2016-09-14)

  • Add sandbox rules optional for chrome compatibility
  • Add cors config from a corsCfg property

1.7.6 (2016-08-08)

  • Can enable or disable security

1.7.5 (2016-06-28)

  • Add property <autoEncryptRequest> into JWT object
  • Add property <autoDecryptRequest> into JWT object

1.7.4 (2016-05-27)

  • Add property <allowedRoute> into JWT object

1.7.3 (2016-03-23)

  • Add options rules on session for SSL/TLS connexion.

1.6.2 (2015-12-28)

  • Fix a bug on NODE_ENV usage

1.6.0 && 1.6.1 (2015-12-10)

  • add support for HTTPS
  • add new rules on express schema to manage protocol and cert files
  • move port rule on protocol object rules

1.5.0 (2015-12-09)

  • Change passportjs schema for activeDirectory and standard connection. move from object to an array of object. See doc for more details.
  • Add documentation for passportJs usage

1.4.0 (2015-12-02)

  • Add jwt ips filter on config file

1.3.9 (2015-11-27)

  • Fix a bug with allow-from xframe rules

1.3.6 (2015-11-04)

  • Adding angular rules on lusca for csrf rules

1.3.5 (2015-11-04)

  • Add an allow filter on new render rules

1.3.4 (2015-11-04)

  • Add rules for social item on render config

1.3.2 <-> 1.3.3 (2015-11-04)

  • Update yocto dependencies package

1.3.1 (2015-11-03)

  • Update package and jump to v7.0.0 for joi.

1.3.0 (2015-10-23)

  • Add cors rules on default express schema.

1.2.0 (2015-10-23)

  • Add yocto-render config rules and enableRender method to enable this config
  • Add yocto-router config rules and enableRouter method to enable this config

1.1.1 (2015-10-23)

  • Change level of log for find method
  • Change use of logger on auto enable method

1.1.0 (2015-10-23)

  • Change passportjs config key to passportJs (camelCase).
  • Add a new method to auto enable method from given key
  • Add a type property on mongoose schema.

1.0.6 (2015-10-22)

  • Change message when no files was found.

1.0.4 (2015-10-16)

  • Add jwt rules on default express config format

1.0.1 && 1.0.2 && 1.0.3 (2015-10-08)

  • Change readme.md content & add new setConfigPath method
  • Minor fixes
  • Change directory config rules (all dir is not required now)

0.3.0 -> 1.0.0 (2015-10-06 <-> 2015-10-07)

  • Add readme description
  • Add rules to process enable and activate in the same method (1.0.0)
  • Add a method to enable a specific schema (0.9.0)
  • Add a method to add custom schema (0.8.0)
  • Add access method (0.7.0) - to enable passportJs / express / mongoose config
  • Add module passportJs (0.5.0) - default config for passportjs
  • Add module express (0.4.0) - default config for express
  • Add module mongoose (0.3.0) - default config for mongoose

0.2.1 (2015-10-06)

  • Hint program with yocto-hitn and fix bugs
  • Add Promise return (Q) on load function.

0.1.0 (2015-09-25)

Add all base function.

  • load : load and test given configuration from given path
  • set : default set function to set value
  • get : default get value to get property value
  • realod : reload configuration from given path if new path was given