nc
no-config
npm i no-config
nc

no-config

Config and resource loader

by Fedor Korshunov

1.1.2 (see all)License:MITTypeScript:Not FoundCategories:Node.js Configuration Management
npm i no-config
Readme

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

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

NameRequired?TypeDefaultDescription
configRequiredObjectConfiguration object
initOptionalList of stringsAll ResourcesResources to initialize
verboseOptionalBooleanfalsePrint resource input prior to call its init() function
mask_secretsOptionalBooleantrueif 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.

NameRequired?TypeDefaultDescription. Handling
<RESOURCE>OptionalObjectResource configuration
<RESOURCE>.defautOptionalObject{}Default values
<RESOURCE>.<ENV>OptionalObject{}ENV specific values. If a key duplicates default key, env-specific value is used
<RESOURCE>.initOptionalFunction, Generator functionCalled 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

Downloads/wk

4

GitHub Stars

46

LAST COMMIT

4yrs ago

MAINTAINERS

1

CONTRIBUTORS

2

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
1.1.2
latest
6yrs ago
No tutorials found
Add a tutorial