mongoose-eventify

Mongoose plugin adding add, remove, and change events for attributes on Model class

npm install mongoose-eventify
44 downloads in the last day
95 downloads in the last week
252 downloads in the last month

mongoose-eventify

Mongoose plugin adding add, remove, and change events for attributes on Model class.

Install

Add the plugin as a dependency to your project in package.json:

{
  ...
  "dependencies": {
    "mongoose": ">=3.0.x",
    "mongoose-eventify": ">=0.0.1",
    ...
  },
  ...
}

Run npm install.

Example

// define our move schema and export our model
var MoveSchema = new Schema({
  date: {type: Date},
});
MoveSchema.plugin(require('mongoose-eventify'));
var Move = mongoose.model('Move', MoveSchema);
module.exports = exports = Move;


// now, in a different file, or wherever, we define some change events on the Model
Move.on('change:date', function(move) {
  console.log('Move ' + move._id + ' changed date to + ' move.date);
});

Move.on('add', function(move) {
  console.log('New move created');
});

Move.on('remove', function(move) {
  console.log('Move ' + move._id + ' has been cancelled');
});

Move.on('change', function(move) {
  console.log('Something changed in ' + move._id + '!!');
});

Credit

Inspired by mongoose-lifecycle Event syntax inspired by Backbone.js

License

MIT License

npm loves you