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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-trigger-fix-remove

v1.0.0

Published

Library to listen to events in mongoose

Downloads

3

Readme

mongoose-trigger

Build Status npm npm

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.

Installation

npm install --save mongoose-trigger

Usage

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MongooseTrigger = require('mongoose-trigger');

let UserSchema = new Schema({
  name: String,
  email: String,
  something: Array
});

const UserEvents = MongooseTrigger(UserSchema, {
  events: {
    create: {
      select: 'email skills',
      populate: {
        path: 'skills',
        select: 'name'
      }
    },
    update: {
      populate: 'skills'
    },
    remove: false
  },
  partials: [
    {
      eventName: 'custom_event',
      triggers: 'name',
      select: 'name email',
      populate: 'something' //if it is a reference...
    }
  ],
  debug: false
});

UserEvents.on('create', data => console.log('[create] says:', data));
UserEvents.on('update', data => console.log('[update] says:', data));
UserEvents.on('partial:skills', data => console.log('[partial:skills] says:', data));
UserEvents.on('partial:x', data => console.log('[partial:x] says:', data));
UserEvents.on('remove', data => console.log('[remove] says:', data));

Initialization

Call mongoose-trigger as a function to initialize. It takes 2 arguments:

  • Schema
  • Options

And returns an instance of Node Events which you can attach listeners to.

The following events are supported:

  • create
  • update
  • remove
  • partials (you're free to define those and they are prefixed with 'partial')
const Listener = MongooseTrigger(YourSchema, YourOptions);
Listener.on('partial:custom_event', data => console.log('do something...'));

Options

events

Define what events you want to emit. The following events are available:

  • create
  • update
  • remove

Enabling / Disabling events

events: {
  create: false || true
}

Advanced configuration

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

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'
    }
  }
]

debug

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.

License

The MIT License Copyright (c) Carsten Jacobsen