npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yocto-config

v3.0.1

Published

A simple config manager for node app

Downloads

24

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