npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@baethon/udba-bootstrap

v1.1.1

Published

Minimalistic application bootstrapping layer

Downloads

4

Readme

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