@baethon/udba-bootstrap

1.1.1 • Public • Published

Build Status JavaScript Style Guide

@baethon/udba-bootstrap

Very basic, very minimal application bootstrap layer. Use it when you need to have a control over application set up (and later shutdown) process.

Installation

First, install the package.

npm i @baethon/udba-bootstrap

Setup the bootstrap module (bootstrap/index.js)

const { Bootstrap } = require('@baethon/udba-bootstrap')

module.exports = new Bootstrap(`${__dirname}/providers`)

Bootstrap the application in the applications main file (e.g. the express server)

const bootstrap = require('./boostrap')

bootstrap.load()
    .then(() => {
        // application was bootstrapped, here you can place the main logic etc
    })

If your application handles SIGTERM signal (or other shutdown handler) you should add.

process.on('SIGTERM', async () => {
    await bootstrap.shutdown()
})

Providers

A provider is a class that is reponsible for preparing some core parts of the application before they can be used in the app. Think of it as as a startup script.

Provider can be sorts of things:

  • script setting up connection with the database (e.g. Sequelize init script)

  • configuration loader

  • external integration set up

Providers can have a priority. It's part of their name (e.g. 1-sequelize.js). Bootstrap will make sure to load providers in correct order, using their prority. The lower the number, the higher the priority. The default priority of the provider (in case when you missed adding it in the file name) is 99.

Provider example

const mongoose = require('mongoose');

class MongoProvider {
  async setup () {
    const baseOptions = {
      useNewUrlParser: true,
      useCreateIndex: true,
      user: process.env.MONGODB_USER,
      pass: process.env.MONGODB_PASS,
    };

    await mongoose.connect(process.env.MONGODB_URL, baseOptions)
  }

  async shutdown () {
    await mongoose.disconnect();
  }
}

module.exports = MongoProvider

Loading providers

Bootstrap assumes that all providers are listed in a single directory. You can define this directory in the Bootstrap constructor argument. Providers of the same priority will be loaded concurrently.

Example files structure can look like this:

bootstrap
|- providers
  |- 1-config.js
  |- 2-mongodb.js
  |- 10-middleware.js
  |- 10-logging.js

When you should use @baethon/udba-bootstrap

Quite often I've been working with the applications that don't use any specific framework (disclaimer: I don't consider express or hapi to be a fully fledged framework). They tend to have a single server.js file which tries to do many things:

  • set up core modules (database, config etc)

  • load the routes

  • start the server instance

Usually, this works.

Quite often I had to add a new application layer that required similar loading process, yet without the server parts (e.g. CLI scripts). This requires extracting the loading scripts to a separate module, so that it can be re-used. Quite often it's a single file, doing many things. It becomes a blob which can be hard to maintain.

@baethon/udba-bootstrap gives a clear separation of concerns. Each provider handles the process of setting up only a single core part of the application. With the priorities one can controll the order of their initialization. Adding new startup scripts shouldn't be any problem.

Acknowledgements

Testing

npm test

Package Sidebar

Install

npm i @baethon/udba-bootstrap

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

7.83 kB

Total Files

6

Last publish

Collaborators

  • radmen