0.1.0 • Published 7 years ago

configuration-service v0.1.0

Weekly downloads
3
License
ISC
Repository
github
Last release
7 years ago

js-configuration-service

An easy-to-use configuration service.

Usage

npm install configuration-service

Basic Usage

Create a file config/config.json:

{
    "configOverrideFiles": {}, // This is required
    "environmentVariables": {}, // This is required
    "simpleConfigItem": "simpleConfigValue"
}

In a file:

const ConfigurationService = require('configuration-service');

const configurationService = new ConfigurationService();
const config = configurationService.getConfig();

console.log(config.simpleConfigItem);
// 'simpleConfigValue'

Nested Config

Nested config is easy. It's implemented pretty much how you'd expect. In your config file,

{
    ...,
    "outerConfigItem": {
        "innerConfigItem": [
            { "foo": "bar" },
            42
        ]
    }
}

You can access these values through

const config = configurationService.getConfig();
console.log(config.outerConfigItem.innerConfigItem[0].foo);
// 'bar'

and

const config = configurationService.getConfig();
console.log(config.outerConfigItem.innerConfigItem[1]);
// 42

Custom Config Path and Filename

If you want your config to be in a directory other than config/ or have a name other than config.json, you can override the defaults during instantiation:

const configPath = 'foo/bar/my/config/path/';
const configFileName = 'myConfigFile.json';
const configurationService = new ConfigurationService(
    configPath, configFileName);

Environment Variables

You may have noticed that an environmentVariables object is required in your config. The purpose of this is to allow controlled access to environment variables through the configuration service. In your config file,

{
    ...,
    "environmentVariables": {
        "envVar1": "defaultValue"
    }
}

You can access this property as you normally would, but if there's an environment variable called "envVar1", the value of that environment variable will replace "defaultValue" in the config provided by this service.

Config Override Files

For private config files that you don't want to commit, you can use config override files. This is best shown with an example. In your config file,

{
    ...,
    "configOverrideFiles": {
        "myPrivateKey": "server.pem"
    },
    "myPrivateKey": "" // This value doesn't matter
}

Now suppose that there's a file called config/server.pem. The value of this file will be read in and accessible like so:

const config = configurationService.getConfig();
console.log(config.myPrivateKey);

Testing

npm test