Mongoose-events
Mongoose-events is a plugin for Mongoose MongoDB ODM written on Typescript. The goal of this plugin - to extend the list of events.
Installation
First install node.js and mongodb. Then:
$ npm install mongoose-events
Overview
Importing module
First, we need to import this module.
;
Connecting with Mongoose
After module has been imported we need to connect this plugin with Mongoose
Global Plugin registration
Want to register a plugin for all schemas? The mongoose singleton has a .plugin() function that registers a plugin for every schema. For example:
;;mongoose;
Per Schema Plugin registration
;;var Game = ... ;Game;
This kind of registration means that's events should be raise for Game
model only.
More detail about plugin registration here
Plugins types
At this time only Serial
execution supported.
more
Using new events
This plugins emit that events: `
onBeforeInit // ?
onBeforeCreate // before new record in mongodb creates
onBeforeUpdate // before existing record in mongodb updates
onBeforeRemove // before removing document from mongodb
onBeforeValidate // before validation starts
onAfterInit // ?
onAfterCreate // after new record in mongodb created
onAfterUpdate // after existing record in mongodb updates
onAfterRemove // after removing document from mongodb
onAfterValidate // after validation runs
onAfterFindAndRemove // after model.findOneAndRemove(conditions, options, callback) call
onAfterFindAndUpdate // after model.findOneAndUpdate(conditions, options, callback) call
onAfterInsertMany // after model.insertMany(array, callback) call
onBeforeCount // before model.count(QueryObject) call
onBeforeFind // before model.find(QueryObject) call
onBeforeFindOne // before model.findOne(QueryObject) call
onAfterCount // after model.count(QueryObject) call
onAfterFind // after model.find(QueryObject) call
onAfterFindOne // after model.findOne(QueryObject) call
onBeforeFindAndRemove // before model.findOneAndRemove(conditions, options, callback) call
onBeforeFindAndUpdate // before model.findOneAndUpdate(conditions, options, callback) call
onBeforeInsertMany // before model.insertMany(array, callback) call
`
Also events names available from EventNames
class
Usage:
;;var Game = ... ;Game;Game
For globally plugin registration this events should be available in every created Schema
Note There are new property added to your model is $$isNewRecord witch keep model state. Don't use it for personal goals.
On next iteration
- Add support for query events WIP
'onBeforeCount' implemented
'onBeforeFind' implemented
'onBeforeFindOne' implemented
'onAfterCount' implemented
'onAfterFind' implemented
'onAfterFindOne' implemented
'onBeforeFindAndRemove' implemented
'onBeforeFindAndUpdate' implemented
'onBeforeInsertMany' implemented
'onAfterFindAndRemove' implemented
'onAfterFindAndUpdate' implemented
'onAfterInsertMany' implemented