Mongoose plugin to automatically emit needed events.
This modules lets you attach event listeners directly to your models and emit event at any mongoose hook you want in an extremly flexible way.
npm install --save mongoose-trigger
const mongoose = ;const Schema = mongooseSchema;const MongooseTrigger = ;let UserSchema =name: Stringemail: Stringsomething: Array;const UserEvents =;UserEvents;UserEvents;UserEvents;UserEvents;UserEvents;
mongoose-trigger as a function to initialize.
It takes 2 arguments:
And returns an instance of Node Events which you can attach listeners to.
The following events are supported:
- partials (you're free to define those and they are prefixed with 'partial')
const Listener = ;Listener;
Define what events you want to emit. The following events are available:
Enabling / Disabling events
events:create: false || true
This configuration uses the same syntax as mongoose. Actually it uses mongoose functionallity to make it possible. So you can include exclude single & multiple fields
events:create:select: 'name email'populate: 'something'
events:create:select: 'name email'populate:path: 'something'select: 'example'
Partials gives you the power to create custom events with custom triggers and custom data. Very flexible... The settings do also use mongoose built in functionality. So be sure to checkout mongoose documentation.
partials:eventName: 'custom_eventname'triggers: 'name email'select: 'email'populate: 'something'
partials:eventName: 'custom_eventname'triggers: 'something'select: 'something name'populate:path: 'something'select: 'random'
Set this to true if you want to output info about all emitted events.
Development & Testing
npm run dev starts a server at localhost:3000.
The MIT License Copyright (c) Carsten Jacobsen