1.0.2 • Published 3 years ago

confn v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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 using const { 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 or production key. i.e. the objects whose keys are used for a specific NODE_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 use Conf.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 under defaults are set. (Which is why the environment has more precedence than defaults). After that the staging, production etc. NODE_ENV key, value pairs are set with the hardSet 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
}