Skip to content

fedor/node-no-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM version Build Status Test Coverage Dependency Status Dependency Status
Intro | Quick start | Quick start (ES6) | API | Contributors

Intro

Why not config?

Answer. TL;DR: config separates data to different files based on NODE_ENV, not resources.

$ npm install no-config

Quick start

// config.js
module.exports = {
	redis: {
		init: function (params) {
			return require('redis').createClient(params)
		},
		default: {
			db: 0,
			port: 6379
		},
		development: {
			host: '127.0.0.1'
		},
		production: {
			db: 1,
			host: '192.168.0.10'
		}
	}
}
// index.js
require('no-config')({
	config: require('./config')
}).then(
	function(conf) {
		console.log('ENV', conf.env)
		console.log('Redis:', conf.redis.host+':'+conf.redis.port)
		conf.redis.instance.set('hello', 'world')
	}
)
$ NODE_ENV=development node index.js
ENV development
Redis: 127.0.0.1:6379

Quick start (ES6)

Since no-config returns a promise it is much better to use ES6 generators, arrow functions and co.
If you are not familiar with co, check this step-by-step tutorial

// config.js
module.exports = {
	redis: {
		init: params => require('redis').createClient(params),
		default: {
			db: 0,
			port: 6379
		},
		development: {
			host: '127.0.0.1'
		},
		production: {
			db: 1,
			host: '192.168.0.10'
		}
	}
}
// index.js
'use strict'
const co = require('co')
co(function* () {
	let config = require('./config')
	let conf = yield require('no-config')({config})

	console.log('ENV', conf.env)
	console.log('Redis:', conf.redis.host+':'+conf.redis.port)
	conf.redis.instance.set('hello', 'world')
})

API

Loader

require('no-config')(parameters)

Loads resources from parameters.config based on NODE_ENV environment variable. Returns a Promise which resolves ones all resources are initialized.

Parameters

Name Required? Type Default Description
config Required Object Configuration object
init Optional List of strings All Resources Resources to initialize
verbose Optional Boolean false Print resource input prior to call its init() function
mask_secrets Optional Boolean true if verbose === true will hide input value if its key contains substrings: secret, token, key, pass or pwd

Configuration object

Every high-level key in configuration object is a resource name.

Name Required? Type Default Description. Handling
<RESOURCE> Optional Object Resource configuration
<RESOURCE>.default Optional Object {} Default values
<RESOURCE>.<ENV> Optional Object {} ENV specific values. If a key duplicates default key, env-specific value is used
<RESOURCE>.init Optional Function, Generator function Called to initalize resource, <RESOURCE>.init(result). If returns Promise or Generator, it got resolved with co. Result is saved to result.instance.

Contributors

Fedor Korshunov - view contributions
Anurag Sharma - view contributions