confn
v1.0.2
Published
A simple to implement config management node js library. Setup dev, staging, production, etc. config for your node app effortlessly.
Downloads
21
Maintainers
Readme
Usage
It's very simple to setup and use confn . confn also reads from the environment by default.
Sample:
const Conf = require('confn');
Conf.set('HOST', '127.0.0.1');
Conf.set('PORT', 9337);
console.log('HOST: ', Conf.get('HOST'));
console.log('PORT: ', Conf.get('PORT'));
console.log('USER: ', Conf.get('USER'));
Now if you run the script above:
USER=root node script.js
The output will be:
HOST: 127.0.0.1
PORT: 9337
USER: root
Intialize config
const Conf = require('confn');
const config = {
defaults: {
HOST: '127.0.0.1',
PORT: 9337,
USER: 'root'
OBJECT: {
RANDOM: 'random'
}
},
staging: {
HOST: 'https://staging.co'
},
production: {
HOST: 'https://production.co',
PORT: 9335
}
}
Conf.init(config);
console.log(Conf.get().HOST);
- Now if you run the script above:
NODE_ENV=staging node script.js
- The output will be:
https://staging.co
- Note after you have called
Conf.init(config)
you can fetch the keys from any file by usingconst { KEY1, KEY2 } = require('confn').get()
Description:
confn reads the key-value pairs from defaults key of the config passed in Conf.init(config)
. If there is any key that is in defaults and also in the environment then the value in the enviroment is given precedence. This means that if you run the above script with HOST=localhost node script.js
then the output would be localhost
and not 127.0.0.1
.
- Note: Keys present inside the
staging
orproduction
key. i.e. the objects whose keys are used for a specificNODE_ENV
have precedence over both environment and defaults keys. In the above example, in the production object the host and port both will override the defaults and environment keys.
set, override and hardSet
- You can use the above three methods to set keys.
const Conf = require('confn');
Conf.set('HOST', 1);
Conf.override('HOST', 12);
Conf.hardSet('HOST', 123);
- The difference between these methods is that though you can use set to update / add a key, value pair. You cannot set a key if it has been overriden. i.e. if
Conf.override(key, value)
has been used on that specific key. However if you really want to update that key then you can useConf.hardSet(key, value)
to update that key. - The above three methods are used in
Conf.init(config)
. First the environment keys are set by using the override method. And after that the keys underdefaults
are set. (Which is why the environment has more precedence than defaults). After that thestaging
,production
etc.NODE_ENV
key, value pairs are set with thehardSet
method hence they have the highest precedence.
Stores
- confn by default uses a store
env
for config management. If you want to then you can also add more stores that you might want to use.
Sample code:
const Conf = require('confn');
const config = {
defaults: {
HOST: '127.0.0.1',
PORT: 9337,
USER: 'root',
},
staging: {
HOST: 'https://staging.co',
},
production: {
HOST: 'https://production.co',
PORT: 9335,
},
};
Conf.addStore('memory');
Conf.init(config, 'memory');
console.log(Conf.get(null, 'memory'));
- The code above will create a different store name
memory
where all the configuration keys will be stored. Please note that stores don't share any key, value information amongst each other apart from the environment key, value pairs. - Sample output of above code:
.
.
SPACESHIP_VERSION: '3.11.2',
SPACESHIP_ROOT: '/Users/root/.nvm/versions/node/v14.6.0/lib/node_modules/spaceship-prompt',
P9K_TTY: 'old',
_: '/Users/root/.nvm/versions/node/v14.6.0/bin/node',
HOST: '127.0.0.1',
PORT: 9337
}