Nitrogenous Polymorphic Molecule

    mongoose-events
    TypeScript icon, indicating that this package has built-in type declarations

    0.3.9 • Public • Published

    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

    Install

    npm i mongoose-events

    DownloadsWeekly Downloads

    126

    Version

    0.3.9

    License

    MIT

    Last publish

    Collaborators

    • cejixo3dr