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

loopback-boot-scripts

v1.0.3

Published

Collection of useful boot scripts for Loopback framework

Downloads

19

Readme

Loopback Boot Scripts

Introduction

Loopback Boot Scripts is collection of useful boot scripts for Loopback framework.

Each boot script can be individually enabled / disabled and also it's behavior can be configured to an extent.

Installation

npm install loopback-boot-scripts --save

OR

yarn add loopback-boot-scripts

Modify server.js file as below. This will make the boot-scripts files run first and then boot files in the server/boot directory.

let bootOptions = {
  'appRootDir': __dirname,
  'bootDirs': [
    './node_modules/loopback-boot-scripts/dist/'
  ]
};

...


boot(app, bootOptions, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

Boot Scripts

Custom Errors

Inspired from https://gist.github.com/justmoon/15511f92e5216fa2624b.

Creates Error classes for various HTTP error codes as default Node.js Error class does not capture statusCode and code.

Configuration

"bootScripts": {
  "customErrors": {
    "errors": [
      {"statusCode": 400, "code": "BAD_REQUEST"}
    ],
    "mode": "merge"
  }
}

In above example, a new class BadRequestError will be created with default statusCode=400 and code=BAD_REQUEST.

mode parameter can have a truthy value merge or anything else as falsy. If mode = merge i.e. truthy, errors given with config will be merged with default errors in errors.json, else default errors will be overwritten.

Global Promise

Simply replaces native Promise with bluebird Promise.

Global Models

Accessing models within loopback can be tedious at times. This boot script simply brings all models to global scope for easy reference.

Find By Property

Adds findBy{Property} and findOneBy{Property} methods to models.

e.g.

{
  "name": "Contact",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string"
    },
    "mobile": {
      "type": "string"
    },
    "email": {
      "type": "string"
    }
  }
}

For the above model, following new methods will be added.

Contact.findByName
Contact.findByMobile
Contact.findByEmail

Contact.findOneByName
Contact.findOneByMobile
Contact.findOneByEmail

Please note that since findById method already exists, it won't be overwritten. In that sense, if any of the dynamic generated method already exists in model, it won't be overwritten.

Configuration

In case if either find or findOne methods are not required, they can be disabled with following config.

"bootScripts": {
  "findByProperty": {
    "find": false,
    "findOne": false
  }
}

Current User

Adds a user instance into context accessible at ctx.args.options.[KEY] where [KEY] defaults to currentUser but can also be configured.

User model is also configurable.

Configuration

"bootScripts": {
  "currentUser": {
    "key": "user",
    "model": "CustomUser"
  }
}

Token Refresh

A general use case is where token needs to expire after certain days of last access. This requires refreshing token expiry after every access.

Refreshing the token in every request is also costly, thus this boot script takes care of refreshing token onceIn specified time frame by given ttl.

Configuration

"bootScripts": {
  "tokenRefresh": {
    "onceIn": 86400 * 2,
    "ttl": 86400 * 7
  }
}

Above config will refresh token once in 2 days and will set ttl as 7 days.

Enable / Disable

Each boot script is by default enabled. Need be, it can be disabled by either of following ways.

Let's disable Custom Errors for example.

"bootScripts": {
  "customErrors": false
}

OR

"bootScripts": {
  "customErrors": {
    "enabled": false
  }
}