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-socket.io

v1.2.1

Published

Mongoose - Socket.io integration

Downloads

13

Readme

mongoose-socket.io

Build Status npm npm

Mongoose plugin to automatically emit needed events via Socket.io.

Build live applications in few minutes. You can thank me later ;) ...

This modules lets you attach socket.io directly to your models and emit event at any mongoose hook you want in an extremly flexible way.

Installation

npm install --save mongoose-socket.io

Usage

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const mongooseSocketIo = require('mongoose-socket.io');

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

UserSchema.plugin(mongoooseSocketIo, {
  io, // Socketio instance
  prefix: 'user',
  namespace: function(doc){
    return ['test1','test2']
  },
  room: ['room1', 'room2'],
  events: {
    create: {
      select: 'email skills',
      populate: {
        path: 'skills',
        select: 'name'
      },
      map: function(data) {
        //Do some last mapping/modification
        data.provider = data.email.split('@').pop();
        return data;
      }
    },
    update: {
      populate: 'skills'
    },
    remove: false
  },
  partials: [
    {
      eventName: 'custom_event',
      triggers: 'name',
      select: 'name email',
      populate: 'something' //if it is a reference...
    }
  ],
  debug: false
})

Options

io

This is the result of the socket.io initialization

prefix

Prefixes all event names that are being emitted from this model. (Example for creation event: 'PREFIX:create') You can pass the following types:

| Type | Guide | | ---- | ----- | | String | Pass a string to emit to a single & fixed namespace | | Function | Pass a function that will be resolved right before emitting. Accepts string as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as eventname |

namespace

Define what namespace you want to emit the event on. This option is very powerful You can pass the following types:

| Type | Guide | | ---- | ----- | | String | Pass a string to emit to a single & fixed namespace | | Array | Pass an array to emit to every namespace contained in the array. The array accepts strings and functions | | Function | Pass a function that will be resolved right before emitting. Accepts string or array as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as namespace |

room

Define what room you want to emit the event on. This option is very powerful You can pass the following types:

| Type | Guide | | ---- | ----- | | String | Pass a string to emit to a single & fixed room | | Array | Pass an array to emit to every room contained in the array. The array accepts strings and functions | | Function | Pass a function that will be resolved right before emitting. Accepts string or array as return and handles the returned values as explained earlier in this table. The first parameter of the function contains the currently new/modified/removed item, so you could use some company id or anything in the document as room |

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'
    },
    map: function(data) {
      //Do some last mapping/modification
      data.provider = data.email.split('@').pop();
      return data;
    }
  }
}

Each event does also support a map function which gives you the ability to modify your data one last time before sending. This could be used to merge/delete/add values.

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. You have the map function here as well.

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