1.1.3 • Published 4 months ago

bajo v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

bajo

GitHub package.json version NPM Version

Attention: I do NOT accept any pull request at the moment, thanks!

Overview

Before we go any further, below are some terminologies I use throughout these documentations:

  • <bajo-base-dir>: project directory is where you write all your codes
  • <bajo-data-dir>: data directory, defaults to <bajo-base-dir>/data if not specifically stated
  • <bajo-tmp-dir>: temp directory, defaults to OS temporary directory
  • <package>: plugin package name as normally showed on npm listing
  • <plugin>: plugin name, which is camel cased version of package name

Installation

Open your terminal and type:

$ npm install bajo

Fire up!

Create a new empty directory, this will be your project directory or your <bajo-base-dir>. Now goto your newly created directory, and type:

$ npm init

You'll be asked to name your project etc. IMPORTANT: don't forget to mark your project as ES6 project by setting type key in your package.json to module.

After completing those steps, move on the the next one: crating bajo bootstrap.

Inside your <bajo-base-dir>, create the index.js file and put these lines below:

import bajo from 'bajo'
await bajo.default()

A bajo app ALWAYS needs a data directory to put configuration files, etc. This could be located inside or outside your <bajo-base-dir>.

Lets assume you're going to put your data directory inside your <bajo-base-dir>. So please create a new directory called data first. After that, just type in your terminal:

$ node index.js --dir-data=data

Or you could utilize dotenv by creating .env file in the same directory as index.js, and put this inside:

DIR_DATA = ./data

Now you can omit calling node with arguments, you just need to type:

$ node index.js

Configuration

General rules

  • All configuration files must be placed in <bajo-data-dir>/config/ subfolder
  • Config files should be named after its plugin name
  • File format should be in .json or .js format
  • If .js file is used, it should be in ES6 format and should export either plain javascript object or a function (sync or async both 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, .yaml and .toml) can also be used by installing bajoConfig plugin
  • Order of precedence: .js > .json > .yml > .yaml > .toml

Main configuration File

It should be named bajo.json with following keys:

KeyTypeRequiredDefaultDescription
envstringnodevApp environment: dev or prod
logobjectnoLogger setting
  dateFormatstringnoYYYY-MM-DDTHH:MM:ss.SSS[Z]Date format accoding to dayjs
  toolbooleannofalseSet to true if you want to show log even in tool mode
  levelstringnoSet one of these: trace, debug, info, warn, error, fatal and silent. If it isn't set, it will auto selected based on environment
langstringnoLanguage to use. If not set, it will be auto detected
exitHandlerbooleannotrueSet to false if you want your app NOT to exit gracefully

Installed Plugins

Plugins are what make Bajo Framework so great and flexible: they extends app features!

To use plugins:

  1. Install with npm install <package>
  2. Optionally create <bajo-data-dir>/config/<plugin>.json to customize plugin settings
  3. Open/create <bajo-data-dir>/config/.plugins and put <package> in it, line by line

Example below will load bajoConfig, bajoLogger and bajoMqtt:

bajo-config
bajo-logger
bajo-mqtt

If you later decide to NOT load one or more plugins from your app, you just need to remove those from .plugins file and restart your app.

Warning: please do not confuse between <package> and <plugin>. Plugin package is the name of JS package listed on npm, while plugin name is the name of a plugin - a camel cased version of plugin package

Configuration Overrides

You can override ANY settings on ANY configuration files with dotenv variables and program's argument switches easily.

Order of importance: dotenv variable > args switches > config files

dotenv

  • Create/open <bajo-base-dir>/.env
  • Use __ (double underscores) as the replacement of the dot in object
  • DIR__DATA: Set <bajo-data-dir> data directory
  • DIR__TMP: Set <bajo-tmp-dir> temp directory
  • For every key in bajo.json, use its snake cased, upper cased version, e.g:
    • env => ENV
    • log.dateFormat => LOG__DATE_FORMAT
    • exitHandler => EXIT_HANDLER
  • To override plugins config, prepend every key in plugin config with snake cased, upper cased version of the plugin name followed by a dot, e.g:
    • key in myPlugin => MY_PLUGIN.KEY
    • key.subKey.subSubKey in myPlugin => MY_PLUGIN.KEY__SUB_KEY__SUB_SUB_KEY

Program argument switches

  • Execute with switches, e.g: node index.js --xxx=one --yyy=two
  • Every switches must be prefixed with --
  • Use - as the replacement of the dot in object
  • --dir-data: Set <bajo-data-dir> data directory
  • --dir-tmp: Set <bajo-tmp-dir> temp directory
  • For every key in bajo.json, add prefix -- e.g:
    • env => --env=prod
    • log.dateFormat => --log-dateFormat=xxx
    • exitHandler => --exitHandler
  • To override plugins config, prepend every key in plugin config with the plugin name followed by a :, e.g:
    • key in myPlugin => --myPlugin:key
    • key.subKey.subSubKey in myPlugin => --myPlugin:key-subKey-subSubKey

More Documentations

License

MIT

1.1.3

4 months ago

1.1.2

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.11

4 months ago

1.0.10

4 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.3

6 months ago

0.3.9

1 year ago

0.3.10

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.29

1 year ago

0.2.28

1 year ago

0.2.27

1 year ago

0.2.26

1 year ago

0.2.25

1 year ago

0.2.24

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.1.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.0.1

2 years ago