npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-events

v0.3.9

Published

Adding custom events for mongoose models. Plugin writen on TypeScript.

Readme

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.

import {mongooseEventsSerialPlugin} from 'mongoose-events';

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:

import * as mongoose from 'mongoose';
import {mongooseEventsSerialPlugin} from 'mongoose-events';
mongoose.plugin(mongooseEventsSerialPlugin);

Per Schema Plugin registration

import * as mongoose from 'mongoose';
import {mongooseEventsSerialPlugin} from 'mongoose-events';
var Game = new mongoose.Schema({ ... });
Game.plugin(mongooseEventsSerialPlugin);

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:

import * as mongoose from 'mongoose';
import {mongooseEventsSerialPlugin,EventNames} from 'mongoose-events';
var Game = new mongoose.Schema({ ... });
Game.plugin(mongooseEventsSerialPlugin);
Game
    .on(EventNames.ON_AFTER_CREATE,function() {
        //**code here**/
    })
    .on(EventNames.ON_AFTER_REMOVE,function() {
        //**code here**/
    })

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

LICENSE