Pre- and Post- Create and Update Hooks for Mongoose ODM.
Out of the box, Mongoose provides you with the handy
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.
npm install captain-hook --save
In your Mongoose model:
var captainHook = ;var userSchema = mongoose;userSchema;// function to run before saving a new user instanceuserSchema// function to run after saving a new user instanceuserSchema// function to run before updating an existing user instanceuserSchema// function to run before updating an existing user instanceuserSchema
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.
next() callback must be called for every hook.
- Tests for hooks firing in the correct order
- More granular unit tests
- schema.methods.runPreCreate(), etc
- schema.preCreateMethods array