1.0.0 • Published 12 years ago

mongoose-lifecycle v1.0.0

Weekly downloads
251
License
-
Repository
github
Last release
12 years ago

mongoose-lifecycle

Mongoose plugin adding lifecyle events on the model class.

Installation

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

{
  "name": "myproject",
  ...
  "dependencies": {
    "mongoose": "2.6.5",
    "mongoose-lifecycle": "1.0.0",
    ...
  },
}

And run npm install again.

Usage

Initialization is straightforward:

var Book = new Schema({ ... });
Book.plugin(require('mongoose-lifecycle'));

Now the model emits lifecycle events before and after persistence operations:

  • beforeInsert
  • afterInsert
  • beforeUpdate
  • afterUpdate
  • beforeSave (called for both inserts and updates)
  • afterSave (called for both inserts and updates)
  • beforeRemove
  • afterRemove

You can listen to these events directly on the model.

var Book = require('path/to/models/book');
Book.on('beforeInsert', function(book) {
  // do stuff...
});

License

MIT License