Configuration
Dealing with configuration files
General
- All configuration files must be placed in the
{dataDir}/configsubfolder. - Config files must be named after their plugin namespace.
- The file format should be either
.jsonor.js. If a.jsfile is used, it must be in ES6 format and should export either a plain JavaScript object or a function. Both synchronous and asynchronous functions are supported. If it returns a function, this function will be called within its plugin scope and should return a plain JS object. - Other formats (
.yml,.yamland.toml) can also be used by installing & loading bajoConfig plugin - The order of precedence is
.js>.json>.yml>.yaml>.toml. This means that if a .js file exists, it will be used instead of a .json file or any other format.
Example:
Plugins
Plugins are what make the Bajo Framework so great and flexible: they extend app features and functionalities!
To use plugins, follow these steps:
- Install it with
npm install {pkgName}, where{pkgName}is the plugin’s package name. You can install as many plugins as you want; for a complete list of plugins, please click here. - Optionally, create
{dataDir}/config/{ns}.jsonto customize the plugin’s settings, where{ns}is the namespace or plugin name. - Open or create
{dataDir}/config/.pluginsand list the plugin’s{pkgName}name in it, one per line.
For example, the text below will load bajo-config, bajo-extra, and bajo-template:
# .plugin file
bajo-config
bajo-extra
bajo-template
If you later decide to disable one or more plugins, you just need to remove them from the .plugins file or place a # hash mark in front of the package name and restart your app.
Warning: Please do not confuse {pkgName} and {ns}. The plugin package is the name of the JS package listed on npm, while {ns} is the namespace or plugin name, which is basically the camel-cased version of the plugin’s package name.
Environment
Configuration file support for different environments is also available. All you need to do is create a {ns}-{env}.json file in your {dataDir}/config, where:
{ns}: the namespace/plugin name{env}: your desired environment (devorprod)- App-wide settings with
bajo-{env}.jsonare also possible.
Bajo is smart enough to select which config file will be used based on the following order of precedence:
- Use
{ns}-{env}.jsonif the file exists. - If not, use
{ns}.json. - If that also doesn’t exist, then use the plugin’s default config values.
Config Override
You can easily override ANY key-value pair setting with environment variables and program argument switches. Bajo also supports dotenv with a .env file.
The order of precedence is: environment variable > argument switches > config files > default, built-in values.
All values (whether they come from environment variables, argument switches, or config files) will be parsed using dotenv-parse-variables, so please make sure you visit the repository to fully understand how it works.
dotenv
Using dotenv:
- Create or open
{appDir}/.env - Use
__(double underscores) as replacement for dots in an object. DIR__DATA: Sets the{dataDir}data directory.DIR__TMP: Sets{tmpDir}temporary directory.- For every key in
{ns|bajo}.json, use its snake-cased, upper-cased version. For example:env: 'prod'→ENV=prodlog.dateFormat: 'YYYY-MM-DD'→LOG__DATE_FORMAT=YYYY-MM-DDexitHandler: true→EXIT_HANDLER=true
- To override a plugin’s config, prepend every key in the plugin’s config with the snake-cased, upper-cased version of the namespace followed by a dot. For example:
keyinmyPlugin→MY_PLUGIN.KEY=...key.subKey.subSubKeyinmyPlugin→MY_PLUGIN.KEY__SUB_KEY__SUB_SUB_KEY=...
Example:
# .env file
ENV=prod
LOG__PRETTY=true
LOG__TIME_TAKEN=true
LANG=id
Argument Switches
Using argument switches:
- Use switches, for example:
node index.js --xxx=one --yyy=two - Use
-as the replacement for dots in an object. --dir-data: Sets the{dataDir}data directory.--dir-tmp: Sets the{tmpDir}temporary directory.- For every key in
{ns|bajo}.json, add--prefix. For example:env: 'prod'→--env=prodlog.dateFormat: 'YYYY-MM-DD'→--log-dateFormat=YYYY-MM-DDexitHandler: true→--exitHandler
- To override a plugin’s config, prepend every key in the plugin’s config with the plugin name followed by a colon
:. For example:keyinmyPlugin→--myPlugin:key=...key.subKey.subSubKeyinmyPlugin→--myPlugin:key-subKey-subSubKey=...
Example:
$ node index.js --env=prod --log-pretty --log-timeTaken --lang=id