Bailiff - A configs manager
Bailiff is a single point of config management.
Bailiff lets you access all your configs. Be it .env
, json files, and it also supports a central config in mongo collection.
A central config can make it easy to share and manage common configs between your services, e.g. core DB configs.
Setup
Install
npm install --save bailiff
Add variable in ‘.env’ file in root of your service
You need this if you want to have central configs in Mongo DB.
BAILIFF_MONGO_USER="your-mongo-user"
BAILIFF_MONGO_PASS="your-mongo-pass"
BAILIFF_MONGO_HOST="your-mongo-host"
BAILIFF_MONGO_PORT="your-mongo-port"
BAILIFF_MONGO_DB="your-db-name"
BAILIFF_MONGO_COLLECTION="your-collection-name"
OR
BAILIFF_MONGO_URI="mongodb://user:pass@host:27017"
BAILIFF_MONGO_DB="your-db-name"
BAILIFF_MONGO_COLLECTION="your-collection-name"
How to use
Get a config from .env or central config from mongo
Add bailiff
in your code.
const bailiffPromise = require("bailiff").default;
bailiffPromise.then(bailiff =>{
bailiff.get("MY_CONFIG_VAR");
})
To add custom configurations
const bailiffPromise = require("bailiff").default;
OR
import bailiffPromise from "bailiff"
bailiffPromise.then(bailiff =>{
bailiff.addStore({"MY_CONFIG_VAR": "MY_CONFIG_VALUE", "ANOTHER_CONFIG": "ANOTHER_VALUE"})
.addStore("relative/path/to/your/json");
})
bailiff uses Singleton pattern. Add config store once and then use it later anywhere in the code in any file.
const bailiffPromise = require("bailiff").default;
const bailiff = await bailiffPromise();
bailiff.get("ANOTHER_CONFIG");
To access central configs from Mongo DB
Data structure of the mongo document should look like -
{
"name": "MY_CONFIG_VAR",
"value": "MY_CONFIG_VALUE",
"status": 1
}
Make a unique index on name and status for fast search and accurate data.
You can access central config similar to other configs.
bailiff.get("MY_CONFIG_VAR");
Bailiff keeps a copy of central mongo config.
It does not read from MongoDB every time.
It only reads from MongoDB once on the service startup. That makes it very fast.
Priority of configs
Priority of configs are
- Custom added store
- Dotenv file
- Central config
This makes it easy to override central config if required.