@fromnibly/config
precedence ordered config with configurable inputs
Last updated 2 months ago by from-nibly .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @fromnibly/config 
SYNC missed versions from official npm registry.

fromnibly/config

A precedence ordered config with configurable inputs.

Install

No need to install typescript types. They are included in this repository.

npm install --save @fromnibly/config

Getting Started

To start you need to create a PropertyDatabase. The PropertyDatabase is the main object you will interact with.

import { PropertyDatabase } from '@fromnibly/config';

let config = new PropertyDatabase();

By default the property database is not configured with any PropertyLoaders. @fromnibly/config includes a couple of default property loaders you can configure your property database with.

import { EnvironmentPropertyLoader, FilePropertyLoader } from '@fromnibly/config';

config
  .withPropertyLoader(new EnvironmentPropertyLoader(process.env))
  .whichOverrides(new FilePropertyLoader('./config'));

You can make your own PropertyLoaders as well. the EnvironmentPropertyLoader is a good example to get you started.

Once you have configured your property loaders you need to load the properties. Since property loaders can be async the loadProperties() call is also async.

config.loadProperties()
  .then(() => /* start your app */)
  .catch((err) => /* something went wrong */);

// or

await config.loadProperties();

Getting A Configuration Property

Once your properties are loaded the property sources will be merged, flattened, lowercased and dot delimited. As an example the following property sources...

server:
  http.port: 8080
  https:
    port: 443
logger:
  level: 'info'
export SERVER_HTTP_HOST=localhost

...will result in the following properties being set

server.http.port=8080
server.http.host=localhost
server.https.port=443
logger.level=info

and can be retrieved in the following ways.

let httpsPort = config.get('server.https.port').asNumber();
let logLevel = config.get('logger.level').asString();
let httpConfig = config.get('server.http').asObject();

NOTE

currently all leaf properties returned by .asObject() will be strings.

Profiles

Profiles allow you to scope different properties. Profiles must be supported by the individual PropertyLoader. For instance the FilePropertyLoader loads properties to a profile based on the name of the file ie. application-dev.yml would be given the dev profile. Using profiles limits when properties from certain sources become available. You set which properties should be loaded when creating the PropertyDatabase.

let config = new PropertyDatabase(['dev', 'local']);

However it is not really useful to hard code the profiles. Something like this is usually a good idea.

let config = new PropertyDatabase(process.env['CONFIG_PROFILES'].split(','));

Properties in profiles specified later in the array will overwrite properties in profiles specified earlier in the array.

Included PropertyLoaders

EnvironmentPropertyLoaders

The EnvironmentPropertyLoader turns environment vaiables into properties by lowercasing, and dot delimiting them.

FilePropertyLoader

The FilePropertyLoader supports json, yaml, and java properties files. It looks for files recursively in the root folder configured that are named application-<profile>.<ext>, or application.<ext> for profile-less files.

Current Tags

  • 1.8.2                                ...           latest (2 months ago)

17 Versions

  • 1.8.2                                ...           2 months ago
  • 1.8.1                                ...           4 months ago
  • 1.8.0                                ...           4 months ago
  • 1.7.4                                ...           4 months ago
  • 1.7.3                                ...           4 months ago
  • 1.7.2                                ...           4 months ago
  • 1.7.1                                ...           5 months ago
  • 1.7.0                                ...           5 months ago
  • 1.6.0                                ...           5 months ago
  • 1.5.1                                ...           6 months ago
  • 1.5.0                                ...           6 months ago
  • 1.4.0                                ...           6 months ago
  • 1.3.1                                ...           7 months ago
  • 1.3.0                                ...           7 months ago
  • 1.2.0                                ...           7 months ago
  • 1.1.0                                ...           7 months ago
  • 1.0.2                                ...           7 months ago
Maintainers (1)
Downloads
Today 0
This Week 17
This Month 17
Last Day 0
Last Week 0
Last Month 58
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2018 © taobao.org |