@smartnewbs/feathersjs-hook-logic

1.2.5 • Public • Published

@smartnewbs/feathersjs-hook-logic

License Version Node Coverage Status

This lib is for Feathersjs as an after hook. It is dependent on the stashBefore hook which is part of the official Feathersjs common hook library.

The goal of this hook is monitor changes on service models for specific changes and trigger a business logic. The hook can only be an after hook with the stashBefore hooked in the same method before hook.

Installation

npm install @smartnewbs/featherjs-hook-logic --save

Defining a business logic for a service

Actions will receive three arguments. The first one is the Featherjs app (hook.app), the second will be the original model (provided by stashBefore) and the third will be the requested data change (hook.data)

If you wanted to send an email when a user changes his email as a warning all you need to do is create a new file src/services/users/users.logic.js with the following:

const {hook} = require('@smartnewbs/feathersjs-hook-logic');

module.exports = function () {
  const logic = {
    email: [
      {action: sendEmailWarning}
    ]
  };

  function sendEmailWarning(app, original, changed) {
    // Send email to user
  }

  return hook(logic);
};

The logic object structure reflects the property of the model and its value is an array of triggers which has three properties: old, new and action

If only the action is specified it will trigger the action regardless of the change that is being made.

Let say you want to send a welcome email when a user has verified his email, which changes his status from STATUS_NEW to STATUS_ACTIVE. Your hook would look like:

const {hook} = require('@smartnewbs/feathersjs-hook-logic');

module.exports = function () {
  const logic = {
    email: [
      {old: 'STATUS_NEW', new: 'STATUS_ACTIVE', action: sendWelcomEmail}
    ]
  };

  function sendWelcomEmail(app, original, changed) {
    // Send email to user
  }

  return hook(logic);
};

The properties old and new do not require each other, if they are present they will be verified against stashBefore for old and hook.data for new.

Hooking the logic to your service

Usually the users.hooks.js should reside in the same directory as your users.logic.js. So in your users.hooks.js just add the following:

const {stashBefore} = require('feathers-hooks-common');
const logic = require('./users.logic');

module.exports = {
  before: {
    all: [],
    find: [],
    get: [],
    create: [ stashBefore() ],
    update: [ stashBefore() ],
    patch:  [ stashBefore() ],
    remove: [ stashBefore() ]
  },

  after: {
    all: [],
    find: [],
    get: [],
    create: [ logic() ],
    update: [ logic() ],
    patch:  [ logic() ],
    remove: [ logic() ]
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};

Readme

Keywords

Package Sidebar

Install

npm i @smartnewbs/feathersjs-hook-logic

Weekly Downloads

0

Version

1.2.5

License

MIT

Last publish

Collaborators

  • pguilbault