@catalyststack/subsystem-config

An opinionated configuration subsystem for Node.js applications.

Usage no npm install needed!

<script type="module">
  import catalyststackSubsystemConfig from 'https://cdn.skypack.dev/@catalyststack/subsystem-config';
</script>

README

subsystem-config

An opinionated configuration subsystem for Node.js applications.

This subsystem is a lightweight minimal dependency subsystem which make prod ready configuration trivial, allowing you to load configurations from a range of sources including:

  • setting in code via setDefault() and config.setOverride()
  • yaml files/folders via loadFile(), loadFolder() or loadFileOrFolder()
  • application package.json
  • application and system etc folders
  • application config folder using NODE_ENV file(s)
  • environment variables
  • command line files & arguments

Configurations are loaded into buckets which overload one another in the order they are loaded.

A default behaviour helper function loadDefaultStrategy() is also provided to give a sane one call default for loading configurations. In most instances you would just call this and get a good load behaviour:

  1. Application defaults: config.setDefault()
  2. Application package.json: <app_root>/package.json
  3. Application etc: <app_root>/etc
  4. Environment default: <app_root>/config/default
  5. Environment files: <app_root>/config/<NODE_ENV, *>
  6. System etc folder: /etc/<app_name>
  7. ENVIRONMENT variables: <app_name>_PATH_TO_CONFIG_ENTRY=<VALUE>
  8. Commandline config files: [-c|--config] /path/to/config/file/or/folder
  9. Commandline arguments: --config:ppath.to[0].config.entry=<VALUE>
  10. Application overrides: config.setOverride()

Installation

npm install --save '@catalyststack/subsystem-config'

Usage

The subsystem exposes a singleton instance of the Config class so that you can just require the subsystem and have access to your configurations:

const config = require('@catalyststack/subsystem-config');

// Initialize config subsystem
// NOTE: This should only be done once
config.init();
config.loadDefaultStrategy();

Once initialzed, values are accessible via the get() method:

const config = require('@catalyststack/subsystem-config');

function printName()
  const name = config.get('path.to.some.object.name', 'default-name');
  console.log(name);
}

Also regardless of initialization, at any point within your code you can set a default or override value via the setDefault() and setOverride() methods:

const config = require('@catalyststack/subsystem-config');

config.setDefault('path.to.some.default', 'if nothing else overloads this');
config.setOverride('path.to.some.override', 'force this value regardless of overloads');

Paths support both object and array traversal using their respective . and [0] notations:

console.log(config.get('path.to.some.object.value'));
console.log(config.get('path.to.some.array[0]'));

TODO

  • Implement plugin system which lets external modules attach functions (S3)
  • Implement cli alias to config path to allow for universal easy cli based config
  • Implement loading of both files and folders when both exist
  • Implement ignoring of hidden files/folders
  • Implement folder default file
  • Implement get() operation data validation e.g. expect non empty string etc
  • Implement function to delete a specific bucket
  • Implement cache container which is read before trying to deep resolve
  • Add ... alias support for previous stack array values i.e. deep merge previous stack array values and then push following values after
  • Add reference document generation

License

MIT