Manager the configuration, may be depending on the environment, with the ability to import another config files defined inside themselves.
Possibility to load configuration from:
- Json files
- Yaml files
- Ini files
- JS files(modules that export object)
- Functions
- Objects
Instalation:
-
Add dependency
npm install conflakes --save -
The next step is create config structure files, example:
application/ │ ├─ config/ │ └─ config_dev.json │ └─ config_prod.json └─ app.js
-
Add to your app.js this piece of code
typescript2
const APP_ENV = processenvNODE_ENV || 'dev';;const config = ;javascript es6
const APP_ENV = processenvNODE_ENV || 'dev';const Conflakes = ;const config = ;If you want to use specyfic format, simply give the file with appropriate extension
- .json
- .yml
- .ini
- .js
How to use config:
-
"get" method returns a parameter by name.
To get host to database:
config;To get first element of the "blacklist" array:
config;If the parameter does not exist in configuration it will be throw exception, To avoid this you can use default parameter.
config;Now if the parameter does not exist it will return null.
-
"all" method
Method "all" return all configuration object
configall;
How to use resource loaders, method "load":
You can use resources like yml, ini, json, js(mocdule) files and objects or functions. You must keep in mind that each call "load" loads the object which it is merging to the previous configuration if there are duplicate keys in different resources that will be overwritten by the last resource.
The "Object" and "Function" resource loaders are very helpful you can add to you config for example parameters from "command line arguments" or "environments variables"
const Conflakes = ;const conflakes = ; // You can load serveral files, of course the files inside with key "imports":[ ] can import other filesconflakes; // Yaml file resourceconflakes; // Ini File resourceconflakes; // Json File resource // Object resource, the object will be merge to configuration objectconflakes; //Function resource, the returned object will be merge to configuration objectconflakes; var config = conflakes; //when you call getConfig the returned object is frozen
Important information
For increase security, avoid problems and enforce good practices, when you call getConfig config object is frozen, this mean that you can't modify configuration when your application is running. Any attempts will trow exception. You can not freeze the configuration by calling conflakes.getConfig(false).
Module allow for flexible organization configuration files, examples:
-
Configuration schema files with sufix
const config = ;application/ │ ├─ config/ │ ├─ config.json │ ├─ config_dev.json │ ├─ config_prod.json │ ├─ routing.json │ └─ services.json ├─ ...
The file config/config.json inherits config_dev.json and config_prod.json file.
config/config.json
config/config_dev.json
config/config_prod.json
-
Folders for each environment
const config = ;application/ │ ├─ config │ ├─ default/ │ │ ├─ config.json │ │ ├─ routing.json │ │ └─ security.json │ ├─ dev/ │ │ ├─ config.json │ │ ├─ routing.json │ │ └─ security.json │ └─ prod/ │ ├─ config.json │ ├─ routing.json │ └─ security.json ├─ ...
The file config/default/config.json inherits dev/config.json and prod/config.json file.
config/default/config.json
config/dev/config.json
-
Semantic configuration
const config = ;application/ ├─ config/ │ ├─ modules/ │ │ ├─ module1.json │ │ ├─ module2.json │ │ ├─ ... │ │ └─ moduleN.json │ ├─ environments/ │ │ ├─ default.json │ │ ├─ dev.json │ │ └─ prod.json │ ├─ routing/ │ │ ├─ default.json │ │ ├─ dev.json │ │ └─ prod.json │ └─ services/ │ ├─ frontend.json │ ├─ backend.json │ ├─ ... │ └─ security.json ├─ ...
The file environments/default.json inherits environments/dev.json and environments/prod.json file.
config/environments/default.json
config/environments/dev.json
Absolute Path
Configuration files can by import also from absolute path
Additional functionalities
- Yaml
- Conflakes provide custom yaml type(https://yaml.org/type/) !!js/var that allows you to read variables from a global node.js object, example:
env: !!js/var process.env.NODE_ENV
Build .dist. files
The genesis of the problem: While you work, you changes parameters in file config_dev.json for your individual preferences, example logins, passwords to databases, cache systems. When someone in your team adds to config_dev.json new parameters required by applicaton and when you pull this changes, you will have conficts and must manualy add this paramaters to your config_dev.json. To avoid this problem you must add config_dev.dist.json file and generate config_dev.json. So when someone add new parameters to config_dev.dist.json you will not have conflicts and you can merge new paramaters with simple command "npm run build-dist" and you will be automatically updated file config_dev.json. Now conflakes support json and yaml files for this feature.
-
Example configuration schema with config_dev.dist.json file
application/ ├─ config/ │ ├─ config.json │ ├─ config_dev.dist.json │ └─ config_prod.json ├─ ...
-
To you package.json file add script
"scripts": { "build-dist": "build-dist --recursive ./config" }
Avalible options:
- paths to files and folders
- --recursive or -r recursive search for dist files
build-dist ./config ./directory/file.dist.json
Files with the name *.dist.* in ./config and file ./directory/file.dist.json will be build
build-dist -r ./config ./another_dir/nest_dir
Files with the name *.dist.* will be search and bulid recursive in ./config and ./another_dir/nest_dir
-
Run command
npm run build-distScript for us merge new parameters, but leave your configuration if you have previously configured.