mongoose-eventify

0.0.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-eventify

Weekly Downloads

2

Version

0.0.2

License

MIT

Last publish

Collaborators

  • frederickcook