4.0.1 • Published 4 years ago

muchconf v4.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

muchconf

Wow! So much configuration, so many sources!

npm Build Status

  1. What is muchconf?
  2. Getting started
    2.1. Promise based approach
    2.2. Event based approach
    2.3. Initializing muchconf with multiple sources
    2.4. Loading configuration conditionally
    2.5. Multiple instances of muchconf
  3. muchconf()
  4. Class: Provider
  5. Built-in providers (configuration sources) 5.1. muchEnv
    5.2. muchArgv
    5.3. muchJson
    5.4. muchJsonFile
    5.5. muchFile
  6. External providers
  7. Writing custom provider
  8. Examples
  9. Tests

What is muchconf?

Muchconf is a module which allows to get configuration for your NodeJS app. It supports multiple sources of configuration and can load different configuration according to environment (e.g. development or production) or any custom logic.

Out of the box muchconf supports 4 sources of configuration: environmental variables, command line arguments, json and json (js) files. Additional sources can be added using external providers.

Muchconf can reload your application if configuration changes and source supports it.

Getting started

Install module using your favorite package manager.

npm install muchconf

Configuration will be kept in Store. To feed Store with configuration use at least one Provider (you can choose from built-in providers or use an external one).

Promise based approach

const { muchconf, muchEnv } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        port: 'PORT',
        ip: 'IP'
    })
]);

configStore
    .load()
    .then((config) => {
        // Now start server and listen on ip and port form environmental variables
        console.log('Server running at ' + config.ip + ':' + confgi.port);
    });

Event based approach

const { muchconf, muchEnv } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        port: 'PORT',
        ip: 'IP'
    })
]);

configStore.on('ready', (config) => {
    // Now start server and listen on ip and port form environmental variables
    console.log('Server running at ' + config.ip + ':' + confgi.port);
});

configStore.load();

Initializing muchconf with multiple sources

muchconf accepts array of providers. The final configuration is result of combining configurations from each source. A succeeding provider overwrites same keys in preceding one.

Example:

const { muchconf, muchJson } = require('muchconf');

const configStore = muchconf([
    muchJson({
        port: '9000',
        ip: '127.0.0.1'
    }),
    muchJson({
        port: '8080'
    })
]);

configStore
    .load()
    .then((config) => {
        // Final configuration:
        /**
         *  {
         *      port: '8080'
         *      ip: '127.0.0.1'
         *  } 
         * 
         **/
    });

Passing configuration to provider options

Configuration can be passed to provider options. For example file path can be passed via configuration to JsonFileProvider.

const { muchconf, muchJson, muchJsonFile } = require('muchconf');

const configStore = muchconf([
    muchJson({
        filePath: '/app/config/prod.json'
    }),
    muchJsonFile(
        config => config.filePath
    )
]);

Loading configuration conditionally

Each Provider is aware of configuration of its predecessors. It is possible to load configuration of given Provider based on current state of configuration.

Example:

Let's say we want to run app on a different port than default one in production environment. In the following example the default port will be 3000 and production port will be 8080. In given example the last JsonProvider will overwrite port only if env equals 'production'.

const { muchconf, muchEnv, muchJson } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        env: 'NODE_ENV',
    }),
    muchJson({
        port: '3000'
    }),
    muchJson({
        port: '8080'
    }, {
        is: {
            env: 'production'
        }
    })
]);

Similar effect can be achieved with not option. The last Provider will overwrite configuration in every situation except when env equals 'production'.

const { muchconf, muchEnv, muchJson } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        env: 'NODE_ENV',
    }),
    muchJson({
        port: '8080'
    }),
    muchJson({
        port: '3000'
    }, {
        not: {
            env: 'production'
        }
    })
]);

It is possible to pass a function instead of expected value. The function must return true or false. In next example default port will be overwritten for 'production' or 'testing' environment.

const { muchconf, muchEnv, muchJson } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        env: 'NODE_ENV',
    }),
    muchJson({
        port: '3000'
    }),
    muchJson({
        port: '8080'
    }, {
        is: {
            env: (value) => {
                return (value === 'production' || value === 'testing');
            }
        }
    })
]);

Multiple instances of muchconf

By default calling muchconf() always returns the same instance of store. It is possible to create new store by passing unique key in options.instance.

const muchconf = require('muchconf');

const instanceKey = 'unique_key';

const configStore = muchconf([], {
    instance: instanceKey
});

To reference to that instance the same key must be passed each time.

const configStore = muchconf({ instance: 'unique_key' });

muchconf()

muchconf is a store for configuration. It accepts array of providers and additional options. Muchconf is singleton, which means wherever you require it in your project always will be returned the same instance (multiple instances are also possible - see multiple muchconf instances).

Syntax:

muchconf(providers, options);

Parameters:

nametyperequireddefaultdescription
providersarray of Providersno[]Providers of configuration to feed the store
optionsobjectnosee belowoptions for muchconf
options.instancesymbol or stringnonew Symbol() is createdEach instance of muchconf is identified by unique key. By default muchconf creates its key by itself. If more than one instance of muchconf is required it can be created by passing custom instance key. The same key must by used later to refer to this instance.
options.allowNullOrUndefinedbooleannofalseShould null or undefined be treated as a proper value. If set to false (default behavior) null or undefined won't overwrite existing configuration.

Returns:
Instance of configuration store.

Methods

load

Loads configuration from store. I returns promise, which resolves to configuration object.

Syntax:

configStore
    .load()
    .then((config) => {
        // configuration is available here
    });

get

Returns configuration from store.

Syntax:

let config = congiStore.get();

getSymbol

Returns unique key of instance.

Syntax:

configStore.getSymbol();

Events

Muchconf store is an instance of EventEmitter. During its lifecycle the following events are emitted:

Event nameDescription
readyFired after store initialization and when final configuration is ready. ready event is fired only once in store lifecycle.
loadedFired whenever new configuration is ready. It is fired both after store initialization and after configuration update.
updateFired after configuration update.
errorFired whenever error occurs.

Event cycle:

state / event namereadyloadedupdate
Instance of muchconf initialized and configuration is readyyesyesno
Configuration has been updatednoyesyes

Class: Provider

Each configuration provider extends this class. Provider is an instance of EventEmitter.

new Provider(options);

Parameters:

nametyperequireddefaultdescription
optionsobjectnosee belowoptions for provider
options.castNumbersbooleannofalseif possible, strings will be converted to number, e.g. '2' will be 2
options.convertTrueFalseStringsbooleannofalsestrings like 'true' or 'false' will be converted to boolean
options.cutQuotationsbooleannofalsedouble quotation marks form beginning and ending of string will be cut off. E.g. '"some value"' will be 'some value'
options.trimbooleannotruetrims whitespace from strings
options.notobjectnoundefinedconditions when provider should not be used
options.isobjectnoundefinedconditions when provider should be used

Methods

enableWatching

Sets watch property to true. Tells muchconf that Provider supports configuration watching.

Syntax:

provider.enableWatching();

parse

If possible and enabled in options passed to provider it transforms configuration value.

Syntax:

provider.parse(value);

Parameters:

nametyperequireddefaultdescription
valuestringyesvalue to convert

Returns:
Parsed value if it was possible, in other case original one.

castNumber

If possible converts number-like value to number.

Syntax:

provider.castNumber(value);

Parameters:

nametyperequireddefaultdescription
valuestringyesvalue to convert

Returns:
Parsed value if it was possible in other case original one.

convertTrueFalseString

If possible converts strings like "true" or "false" to its boolean equivalent. It is case insensitive.

Syntax:

provider.convertTrueFalseString(value);

Parameters:

nametyperequireddefaultdescription
valuestringyesvalue to convert

Returns:
Parsed value if it was possible in other case original one.

cutQuotations

If possible trims quotation marks from string.

Syntax:

provider.cutQuotations(value);

Parameters:

nametyperequireddefaultdescription
valuestringyesvalue to convert

Returns:
Parsed value if it was possible in other case original one.

trim

If possible trims whitespace from string.

Syntax:

provider.trim(value);

Parameters:

nametyperequireddefaultdescription
valuestringyesvalue to convert

Returns:
Parsed value if it was possible in other case original one.

load

Loads configuration. It should be implemented in custom provider. If not it always resolves to empty configuration.

Syntax:

provider.load();

Returns:
Promise which resolves to configuration object.

Built-in providers (configuration sources)

Provider represents source of configuration. Muchconf has four built-in providers and supports external providers. Out of the box muchconf can get configuration form environmental variables, command line arguments, JSON or JSON file.

Built-in providers: 1. muchEnv - environmental variables
2. muchArgv - command line arguments
3. muchJson - JSON (or javascript object)
4. muchJsonFile - JSON file 5. muchFile - configuration values from single file

muchEnv

muchEnv gets configuration form environmental variables in OS.

Syntax:

muchEnv(configurationMap, providerOptions)

Parameters:

nametyperequireddefaultdescription
configurationMapobjectyesobject representing configuration. It could be nested or include arrays. Each value will be replaced with value of ENV variable with that name
providerOptionsobjectnocommon options for provider. See Provider section

Example:

const { muchconf, muchEnv } = require('muchconf');

const configStore = muchconf([
    muchEnv({
        env: 'NODE_ENV',
        port: 'PORT',
        mongo: {
            uri: 'MONGO_URI',
            port: 'MONGO_PORT',
            dbName: 'MONGO_DATABASE_NAME'
        },
        apiEndpoints: ['API_ENDPOINT_MAIN', 'API_ENDPOINT_BACKUP']
    })
]);

EnvProvider will map environmental variables to configuration keys. Final configuration could look like this:

    {
        env: 'production',
        port: '9000',
        mongo: {
            uri: 'mongo://localhost',
            port: '27017',
            dbName: 'AppDatabase'
        },
        apiEndpoints: ['https://main.api.example', 'https://backup.api.example']
    }

muchArgv

muchArgv gets configuration from command line arguments in format --name-of-option <value>.

Syntax:

muchArgv(configurationMap, providerOptions)

Parameters:

nametyperequireddefaultdescription
configurationMapobjectyesobject representing configuration. It could be nested or include arrays. Each value will be replaced with value of option with that name preceded with double dash.
providerOptionsobjectnocommon options for provider. See Provider section

Example:

const { muchconf, muchArgv } = require('muchconf');

const configStore = muchconf([
    muchArgv({
        env: 'env',
        port: 'port',
        mongo: {
            uri: 'mongo-uri',
            port: 'mongo-port'
        }
    })
]);

If we run app with command like this:

node app.js --env production --port 9000 --mongo-uri mongo://localhost --mongo-port 27017

It will result with configuration:

    {
        env: 'production',
        port: '9000',
        mongo: {
            uri: 'mongo://localhost',
            port: '27017'
        },
    }

muchJson

muchJson accepts JSON or JS object as configuration

Syntax:

muchJson(json, providerOptions)

Parameters:

nametyperequireddefaultdescription
jsonobjectyesobject with configuration
providerOptionsobjectnocommon options for provider. See Provider section

Example:

const { muchconf, muchJson } = require('muchconf');

const configStore = muchconf([
    muchJson({
        env: 'production',
        port: 9000,
        mongo: {
            uri: 'mongo://localhost',
            port: 27017
        }
    })
]);

muchJsonFile

muchJsonFile imports JSON or JS file with configuration.

Syntax:

or

muchJsonFile(configurationMap, providerOptions)

Parameters:

nametyperequireddefaultdescription
filePathstringyespath to file with configuration
providerOptionsobjectnocommon options for provider. See Provider section

Example:

const { muchconf, muchJsonFile } = require('muchconf');

const configStore = muchconf([
    muchJsonFile('/app/config/configuration.json')
]);

muchFile

muchFile reads single values from text file.

Syntax:

muchFile(configurationMap, providerOptions)

Parameters:

nametyperequireddefaultdescription
configurationMapobjectyesobject representing configuration. Values should be filepaths or name of ENV variable containing filepath
providerOptionsobjectnocommon options for provider. See Provider section
providerOptions.fromEnvbooleannofalseis filename provided in environmental variable

Example:

const { muchconf, muchFile } = require('muchconf');

const configStore = muchconf([
    muchFile({
        password: '/run/secrets/password'
    })
]);

File path can be passed in environmental variable. It is especially useful when working with docker secrets.

Example:

const { muchconf, muchFile } = require('muchconf');

const configStore = muchconf([
    muchFile({
        password: 'PATH_TO_FILE_IN_ENV'
    }, {
        fromEnv: true
    })
]);

External providers

Here is list of external providers.

Configuration sourceLinkDescription
consulkmoskwiak/muchconf-consul-providerImports configuration from consul KV store. Support for configuration reloading.

Writing custom provider

By itself Provider is not very useful, it always returns empty configuration :). Provider class allows to create custom providers.

The simplest custom provider extends Provider class and exposes load method. Here is an example of provider, which always returns { awesome: true } configuration.

const { Provider } = require('muchconf');

class AwsomeProvider extends Provider {
    constructor(commonOptions) {
        super(commonOptions);
        this.myConfiguration = {
            awesome: true
        };
    }

    load() {
        return Promise.resolve(this.myConfiguration);
    }
}

To take advantage of Provider parsing function method parse must be explicitly called on value.

const { Provider } = require('muchconf');

class AwsomeProvider extends Provider {
    constructor(commonOptions) {
        super(commonOptions);

        this.myConfiguration = {
            awesome: 'TRUE',
            port: '9000'
        };
    }

    load() {
        let configuration = {};
        for(let key in this.myConfiguration) {
            configuration[key] = this.parse(this.myConfiguration[key]);
        }
        return Promise.resolve(configuration);
    }
}

In above example, if AsomeProvider will be called with options { castNumber: true, convertTrueFalseStrings: true } values 'TRUE' and '9000' will be converted to true and 9000 accordingly.

Provider can emit update event when configuration changes. muchconf listens for those events and can reload application. To enable provider watching method startWatching must be called.

const { Provider } = require('muchconf');
const database = require('someDatabase');

class AwsomeProvider extends Provider {
    constructor(commonOptions) {
        super(commonOptions);
        this.db = database.connect();

        this.configuration = {};
        this.enableWatching();
        watchForChanges();
    }

    async getConfiguration() {
        return this.db.select('configuration');
    }

    watchForChanges() {
        setTimeout( async () => {
            let config = await this.db.select('configuration');
            // Make sure that configuration has changed!
            this.configuration = config;
            watchForChanges();
        }, 60000)
    }

    async load() {
        this.configuration = await getConfiguration();
        return Promise.resolve(this.configuration);
    }
}

Examples

See examples:

Tests

npm run test
4.0.1

4 years ago

4.0.0

4 years ago

3.3.1

4 years ago

3.3.0

5 years ago

3.2.0

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.0

6 years ago