mhook
Middleware like hooks for node.js which is useful for building some relations between entities (ODM, ORM, etc).
Installation
npm install mhook
Usage
Define your actions, bind hooks on them, trigger actions. When you trigger an action all binded hooks will be sequentially executed. Be careful, hook function should return promise or call provided callback, otherwise trigger will hang.
var Hook = Hook; var hook = 'beforeUpdate' 'afterUpdate' 'afterRemove'; // add hook with callbackhook; // add another hook with promisehook; // trigger `action` - executes all two hookshook; // trigger also return promisehook;
for getting on
and trigger
methods to your object you can inherits from Hook
var Hook = Hook inherits = inherits; { // apply parent cunstructor Hook;} // inherits from Hook; // now we can use `on` and `trigger` as own methodsModelprototype { this;}; var model = ; model;
Api
Hook()
Hook constructor
accepts array of string actions
- possible actions which could be used
at on
and trigger
Hook.on(action:String, hook:Function)
Bind hook
on action
Hook.trigger(action:String, hookArgs:Array, [callback]:Function)
Trigger some action
with hookArgs
(arguments which will be passed to
every hook function).
If callback
(accepts error as first argument) function presents it will be
called after hooks execution.
Return promise which will be resolved after hooks execution.
Running test
Into cloned repository run npm run test