Skip to content

younggyo/mongoose-eventify

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%