@windingtree/config-profiles
v1.0.11
Published
Downloads
4
Readme
About
This package allows developers to:
- use various configuration profiles (e.g. production, staging, development, unit-test, integration-test)
- activate profile and retrieve configuration properties for a given profile
- store and retrieve configuration properties from database (mongoDb collection) at runtime/development
- maintain all profiles and it's values in source code repository (so that no configuration entry is lost while branching/development cycle)
Installation
npm i @windingtree/config-profiles
Usage
###Generate local file with configuration from database In order to avoid using async/promises each time you need to retrieve configuration entry from collection, at start/build time local JSON file needs to be generated. Below snippet will generate such a file which can be later loaded to retrieve configuration values.
//Initialize - bare minimum is to provide URI to mongo database which stores configuration
const profiles = require('../src/profiles').init({dbUrl:process.env.MONGO_URI});
//generate local JSON file with 'staging' profile
profiles.dumpProfile('staging')
It's possible to customize it by providing various init parameters
//Initialize - bare minimum is to provide URI to mongo database which stores configuration
const profiles = require('../src/profiles').init({
dbUrl:process.env.MONGO_URI,
baseFolder: '/profiles', //folder where local JSON file should be generated to
profilesCollectionName:'profiles', //name of the collection in the database which stores our configuration,
activeProfileEnvVariableName:'ACTIVE_PROFILE', //name of environment variable which stores the name of the current profile (e.g. staging, production)
encryptionDetails:'aes-256-ctr:1234567890123:ff7dc996632d5ea672a7acd5776f2ff5' //key to encrypt/decrypt configuration entires
}
);
//generate local JSON file with 'staging' profile
profiles.dumpProfile('staging')
###Retrieve value of configuration entry for a given profile
In order to retrieve configuration entry use below snippet. It will: a) load local JSON file for 'staging' profile b) find and return value of 'example_configuration_key' configuration entry c) decrypt it if it was encrypted
const profiles = require('../src/profiles').init({dbUrl:process.env.MONGO_URI});
let keyValue = profiles.getProfileEntry('staging', 'example_configuration_key');
If you need to have the possibility to also override configuration entries using environment variables, use the below snippet
Below code will: a) Determine what's the current profile (based on environment variable) b) load local JSON file for that profile c) find and return value of 'example_configuration_key' configuration entry d) decrypt it if it was encrypted e) if there is environment variable 'example_configuration_key' defined - it will use that (env variable has higher priority over value from local JSON file) f) if neither (env or JSON) keys are found - default value will be returned
const profiles = require('../src/profiles').init({dbUrl:process.env.MONGO_URI});
let keyValue = profiles.getEnvOrProfileEntry('example_configuration_key','DEFAULT_VALUE');
CLI
There is also a CLI available which allows manipulation of the configuration/profiles from command line prompts. To execute this - use below snippet.
const profiles = require('../src/profiles');
profiles.executeCLI();
CLI is used to initialize/update configuration profiles from command line. CLI requires additional arguments to be provided, depending on a task you want to execute. See below usage.
This script is used to:
-initialize(restore) configuration in database based on configuration from JSON file (e.g. at initial setup)
-update(sync) configuration in database with configuration from JSON file (e.g. once new configuration entry was added/changed in JSON file)
Usage:
cli.js [options] -t <task>
Options:
-c <config_file> - file name which contains all profiles that are supposed to be loaded to the database(default: profiles.json)
-r <repository_subfolder> - subfolder name which contains <config_file> (default: repository)
-a <active_subfolder> - subfolder name where generated files with profiles from the database should be stored (default: active)
-b <base_folder> - path to a base folder with profiles library and which should contain <repository_subfolder> and <active_subfolder> subfolders (default: process.cwd())
-p <profile_name> - profile name to be loaded to or from the database (e.g. 'staging' or 'production')
-db <dburl> - mongo connection string pointing to a database that should be used
-collection <collection_name> - name of mongo collection that should be used to store/load profiles from (default: profiles)
Tasks:
[initialize] - initialize configuration in the database based on data from <config_file> (all profiles from <config_file> will be loaded into the database, if something existed in the database it will be removed)
[loadprofile] - restore entire profile (specified with <profile_name>) in the database based on data from <config_file> (it will remove all entries for <profile_name> from database and load <profile_name> entries from <config_file>)
[deleteprofile] - delete profile (specified with <profile_name>) from the database
[dumpprofile] - download profile from the database (specified with <profile_name>) from the database