5

A simple yet handy configuration compositor and locator!

 2 years ago
source link: https://dev.to/siegfredrodriguez/a-simple-yet-handy-configuration-compositor-and-locator-2pa9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A simple yet handy configuration compositor and locator!

Sep 4

・1 min read

I finally dipped my toes in opensource, with my simple unopinionated yet capable configuration utility config-discovery!

  • Will not force you on any conventions such as directory and specific files.
  • Uses fluent interface, no strange incantations.
  • Fit for containerized deployments where configurations maybe split between ConfigMaps and Secrets.

Github
NPMJS

Some of its features include the ability to define a source priority for your configuration, including environment and directly from an object!

let Config = require('config-discovery');

....

let configuration = new Config()
    .fromFile('/configs/config.json')
    .orFile('/configuration/config.json')
    .orFile('/etc/my_configs/config.json')
    .orEnv(prototype)
    .orObj(configObject)
    .get();

Enter fullscreen modeExit fullscreen mode

Compose a configuration from multiple sources, including the environment!

let prototype = {user: 'DB_USERNAME', password: 'DB_PASSWORD'}

let configuration = new Config()
    .fromFile('/configs/config.json')
    .orFile('/configuration/config.json')
    .orFile('/etc/my_configs/config.json')
    .thenPatchWith()
    .env(prototype)
    .get();

// or from another file

let configuration = new Config()
    .fromFile('/configs/config.json')
    .orFile('/configuration/config.json')
    .orFile('/etc/my_configs/config.json')
    .thenPatchWith()
    .configFile(/etc/secrets/credentials.json)
    .get();

// or from another object

let configuration = new Config()
    .fromFile('/configs/config.json')
    .orFile('/configuration/config.json')
    .orFile('/etc/my_configs/config.json')
    .thenPatchWith()
    .object(secretsJson)
    .get();
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK