🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

captain-hook

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

captain-hook

Pre- and Post- Create and Update Hooks for Mongoose ODM

0.0.3
latest
Source
npm
Version published
Maintainers
1
Created
Source

Captain Hook

Pre- and Post- Create and Update Hooks for Mongoose ODM.

Code Climate

Use-Case

Out of the box, Mongoose provides you with the handy pre('save') and post('save') methods. Unfortunately, these hooks run every time the instance is saved, whether it is being created or updated. Captain Hook extends Mongoose to give you greater control over this functionality.

Usage

npm install captain-hook --save

In your Mongoose model:

var captainHook  = require('captain-hook');
var userSchema = mongoose.Schema();

userSchema.plugin(captainHook);

// function to run before saving a new user instance
userSchema.preCreate(function(user, next){
  console.log("You are about to create a user:" + user.email);
  next();
})

// function to run after saving a new user instance
userSchema.postCreate(function(user, next){
  console.log("You just created a user:" + user.email);
  next();
})

// function to run before updating an existing user instance
userSchema.preUpdate(function(user, next){
  console.log("You are about to update a user:" + user.email);
  next();
})

// function to run before updating an existing user instance
userSchema.postUpdate(function(user, next){
  console.log("You just updated a user:" + user.email);
  next();
})

Multiple pre- and post- methods can be added for each timing (preCreate, postCreate, preUpdate, postUpdate) and the functions will be run in the order they are defined.

The next() callback must be called for every hook.

TO DO:

  • Tests for hooks firing in the correct order
  • More granular unit tests
    • schema.preCreate()
    • schema.methods.runPreCreate(), etc
    • schema.preCreateMethods array

Keywords

mongoose

FAQs

Package last updated on 19 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts