Socket
Socket
Sign inDemoInstall

captain-hook

Package Overview
Dependencies
1
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    captain-hook

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


Version published
Weekly downloads
18
decreased by-30.77%
Maintainers
1
Install size
102 kB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 19 Sep 2014

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc