Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update lifecycle event names.

The 'save' event being emitted before the actual persistence, it may happen that it gets emitted even if the underlying document doesn't get saved. Therefore it is necessary to create pre- and post- events.

This implies renaming all listeners, in the applications as well as in the plugins.

See https://groups.google.com/d/msg/mongoose-orm/UB5fwlIglok/53GER1voodEJ for details.
  • Loading branch information...
commit 9e4ee3c683e0c072584156219483335086d25497 1 parent 7dfba88
@fzaninotto authored
View
4 README.md
@@ -106,14 +106,14 @@ You can add your own plugins under the `plugins` directory. A plugin is simply a
// in plugins/console/index.js
var CheckEvent = require('../../models/checkEvent');
exports.init = function() {
- CheckEvent.on('insert', function(checkEvent) {
+ CheckEvent.on('postInsert', function(checkEvent) {
checkEvent.findCheck(function(err, check) {
console.log(new Date() + check.name + checkEvent.isGoDown ? ' goes down' : ' goes back up');
});
});
}
-All Uptime entities emit lifecycle events that you can listen to on the Model class. These events are 'save', 'insert', 'update', and 'remove'.
+All Uptime entities emit lifecycle events that you can listen to on the Model class. These events are `preInsert`, `postInsert`, `preUpdate`, `postUpdate`, `preSave` (called for both inserts and updates), `postSave` (called for both inserts and updates), `preRemove`, and `postRemove`.
License
-------
View
2  app.js
@@ -63,7 +63,7 @@ io.configure('development', function() {
if (!config.verbose) io.set('log level', 1);
});
-CheckEvent.on('insert', function(event) {
+CheckEvent.on('postInsert', function(event) {
io.sockets.emit('CheckEvent', event.toJSON());
});
View
6 app/api/app.js
@@ -25,9 +25,9 @@ var refreshUpCount = function(callback) {
});
}
-Check.on('insert', function() { upCount = undefined; });
-Check.on('remove', function() { upCount = undefined; });
-CheckEvent.on('insert', function() { upCount = undefined; });
+Check.on('postInsert', function() { upCount = undefined; });
+Check.on('postRemove', function() { upCount = undefined; });
+CheckEvent.on('postInsert', function() { upCount = undefined; });
app.get('/check/count', function(req, res) {
if (upCount) {
View
34 lib/lifecycleEventsPlugin.js
@@ -1,29 +1,49 @@
/**
* Mongoose plugin adding lifecyle events on the model class.
*
- * The model name must be specified during initialization, e.g.:
+ * Initialization is straightforward:
*
* var lifecycleEventsPlugin = require('path/to/lifecycleEventsPlugin');
* var Book = new Schema({ ... });
- * Book.plugin(lifecycleEventsPlugin, 'Book');
+ * Book.plugin(lifecycleEventsPlugin);
+ *
+ * Now the model emits lifecycle events before and after persistence operations:
+ *
+ * - preInsert
+ * - postInsert
+ * - preUpdate
+ * - postUpdate
+ * - preSave (called for both inserts and updates)
+ * - postSave (called for both inserts and updates)
+ * - preRemove
+ * - postRemove
*
- * Now the model emits lifecycle events on saven insert, update, and remove.
* You can listen to these events directly on the model.
*
* var Book = require('path/to/models/book');
- * Book.on('insert', function(book) {
+ * Book.on('preInsert', function(book) {
* // do stuff...
* });
*/
module.exports = exports = function lifecycleEventsPlugin(schema) {
schema.pre('save', function (next) {
var model = this.model(this.constructor.modelName);
- model.emit('save', this);
- this.isNew ? model.emit('insert', this) : model.emit('update', this);
+ model.emit('preSave', this);
+ this.isNew ? model.emit('preInsert', this) : model.emit('preUpdate', this);
+ this._isNew_internal = this.isNew;
next();
});
+ schema.post('save', function() {
+ var model = this.model(this.constructor.modelName);
+ model.emit('postSave', this);
+ this._isNew_internal ? model.emit('postInsert', this) : model.emit('postUpdate', this);
+ this._isNew_internal = undefined;
+ });
schema.pre('remove', function (next) {
- this.model(this.constructor.modelName).emit('remove', this);
+ this.model(this.constructor.modelName).emit('preRemove', this);
next();
});
+ schema.post('remove', function() {
+ this.model(this.constructor.modelName).emit('postRemove', this);
+ });
};
View
4 plugins/console/index.js
@@ -9,7 +9,7 @@ exports.init = function(enableNewEvents, enableNewPings) {
};
var registerNewEventsLogger = function() {
- CheckEvent.on('insert', function(checkEvent) {
+ CheckEvent.on('postInsert', function(checkEvent) {
checkEvent.findCheck(function(err, check) {
var message = check.name + ' ';
message += (checkEvent.isGoDown) ? 'goes down' : ('goes back up after ' + Math.floor(checkEvent.downtime / 1000) + 's of downtime');
@@ -19,7 +19,7 @@ var registerNewEventsLogger = function() {
};
var registerNewPingsLogger = function() {
- Ping.on('insert', function(ping) {
+ Ping.on('postInsert', function(ping) {
ping.findCheck(function(err, check) {
var message = check.name + ' ';
message += (ping.isUp) ? color('OK', 'green') : color('responded with error "' + ping.error + '"', 'red');
Please sign in to comment.
Something went wrong with that request. Please try again.