Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace lifecycleEventPLugin by a dependency to mongoose-lifecycle.

This change renames events on Mongoose models from 'pre-' to 'before-'
and from 'post-' to 'after-' (e.g. 'postRemove' becomes 'afterRemove').

Use npm install to grab the new dependency.
  • Loading branch information...
commit c2d5c01c16f9ee36b75df5fcdc75e28c1af3ebde 1 parent 65b244b
@fzaninotto authored
View
1  CHANGELOG.md
@@ -4,6 +4,7 @@ Uptime Changelog
To be released, v1.3
--------------------
+* Removed lifecycleEventsPlugin and added [mongoose-lifecycle](https://github.com/fzaninotto/mongoose-lifecycle) module as a dependency. This change renames events on Mongoose models from 'pre-' to 'before-' and from 'post-' to 'after-' (e.g. 'postRemove' becomes 'afterRemove').
* Modified API routes to be more RESTful
* Upgraded dependencies (mongoose, express, ejs, config, async, socket.io)
* Added a Reports tab for tags, offering easily accessible monthly reports
View
4 README.md
@@ -118,14 +118,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('postInsert', function(checkEvent) {
+ CheckEvent.on('afterInsert', 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 `preInsert`, `postInsert`, `preUpdate`, `postUpdate`, `preSave` (called for both inserts and updates), `postSave` (called for both inserts and updates), `preRemove`, and `postRemove`.
+All Uptime entities emit lifecycle events that you can listen to on the Model class. These events are `beforeInsert`, `afterInsert`, `beforeUpdate`, `afterUpdate`, `beforeSave` (called for both inserts and updates), `afterSave` (called for both inserts and updates), `beforeRemove`, and `afterRemove`. For more information about these events, check the [mongoose-lifecycle](https://github.com/fzaninotto/mongoose-lifecycle) plugin.
License
-------
View
4 app.js
@@ -72,7 +72,7 @@ io.configure('development', function() {
if (!config.verbose) io.set('log level', 1);
});
-CheckEvent.on('postInsert', function(event) {
+CheckEvent.on('afterInsert', function(event) {
io.sockets.emit('CheckEvent', event.toJSON());
});
@@ -80,7 +80,7 @@ io.sockets.on('connection', function(socket) {
socket.on('set check', function(check) {
socket.set('check', check);
});
- Ping.on('postInsert', function(ping) {
+ Ping.on('afterInsert', function(ping) {
socket.get('check', function(err, check) {
if (ping.check == check) {
socket.emit('ping', ping);
View
6 app/api/app.js
@@ -35,9 +35,9 @@ var refreshUpCount = function(callback) {
});
}
-Check.on('postInsert', function() { upCount = undefined; });
-Check.on('postRemove', function() { upCount = undefined; });
-CheckEvent.on('postInsert', function() { upCount = undefined; });
+Check.on('afterInsert', function() { upCount = undefined; });
+Check.on('afterRemove', function() { upCount = undefined; });
+CheckEvent.on('afterInsert', function() { upCount = undefined; });
app.get('/checks/count', function(req, res) {
if (upCount) {
View
49 lib/lifecycleEventsPlugin.js
@@ -1,49 +0,0 @@
-/**
- * Mongoose plugin adding lifecyle events on the model class.
- *
- * Initialization is straightforward:
- *
- * var lifecycleEventsPlugin = require('path/to/lifecycleEventsPlugin');
- * var Book = new Schema({ ... });
- * 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
- *
- * You can listen to these events directly on the model.
- *
- * var Book = require('path/to/models/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('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('preRemove', this);
- next();
- });
- schema.post('remove', function() {
- this.model(this.constructor.modelName).emit('postRemove', this);
- });
-};
View
2  models/check.js
@@ -28,7 +28,7 @@ var Check = new Schema({
qos : {},
qosPerHour : {}
});
-Check.plugin(require('../lib/lifecycleEventsPlugin'));
+Check.plugin(require('mongoose-lifecycle'));
Check.pre('remove', function(next) {
async.parallel([this.removePings.bind(this), this.removeEvents.bind(this), this.removeStats.bind(this)], function() {
View
2  models/checkDailyStat.js
@@ -12,6 +12,6 @@ var CheckDailyStat = new Schema({
downtime : Number
});
CheckDailyStat.index({ check: 1, timestamp: -1 }, { unique: true });
-CheckDailyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+CheckDailyStat.plugin(require('mongoose-lifecycle'));
module.exports = mongoose.model('CheckDailyStat', CheckDailyStat);
View
2  models/checkEvent.js
@@ -11,7 +11,7 @@ var CheckEvent = new Schema({
downtime : Number
});
CheckEvent.index({ check: 1, timestamp: -1 });
-CheckEvent.plugin(require('../lib/lifecycleEventsPlugin'));
+CheckEvent.plugin(require('mongoose-lifecycle'));
CheckEvent.methods.findCheck = function(callback) {
return this.db.model('Check').findById(this.check, callback);
View
2  models/checkHourlyStat.js
@@ -15,7 +15,7 @@ var CheckHourlyStat = new Schema({
downtime : Number
});
CheckHourlyStat.index({ check: 1, timestamp: -1 }, { unique: true });
-CheckHourlyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+CheckHourlyStat.plugin(require('mongoose-lifecycle'));
var mapCheck = function() {
var qos = { count: this.count, ups: this.ups , responsives: this.responsives, time: this.time, downtime: this.downtime };
View
2  models/checkMonthlyStat.js
@@ -12,6 +12,6 @@ var CheckMonthlyStat = new Schema({
downtime : Number
});
CheckMonthlyStat.index({ check: 1, timestamp: -1 }, { unique: true });
-CheckMonthlyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+CheckMonthlyStat.plugin(require('mongoose-lifecycle'));
module.exports = mongoose.model('CheckMonthlyStat', CheckMonthlyStat);
View
2  models/ping.js
@@ -17,7 +17,7 @@ var Ping = new Schema({
error : String
});
Ping.index({ timestamp: -1 });
-Ping.plugin(require('../lib/lifecycleEventsPlugin'));
+Ping.plugin(require('mongoose-lifecycle'));
Ping.methods.findCheck = function(callback) {
return this.db.model('Check').findById(this.check, callback);
View
2  models/tag.js
@@ -21,7 +21,7 @@ var Tag = new Schema({
downtime : Number
});
Tag.index({ name: 1 }, { unique: true });
-Tag.plugin(require('../lib/lifecycleEventsPlugin'));
+Tag.plugin(require('mongoose-lifecycle'));
Tag.pre('remove', function(next) {
this.removeStats(function() {
View
2  models/tagDailyStat.js
@@ -12,6 +12,6 @@ var TagDailyStat = new Schema({
downtime : Number
});
TagDailyStat.index({ name: 1, timestamp: -1 }, { unique: true });
-TagDailyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+TagDailyStat.plugin(require('mongoose-lifecycle'));
module.exports = mongoose.model('TagDailyStat', TagDailyStat);
View
2  models/tagHourlyStat.js
@@ -15,7 +15,7 @@ var TagHourlyStat = new Schema({
downtime : Number
});
TagHourlyStat.index({ name: 1, timestamp: -1 }, { unique: true });
-TagHourlyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+TagHourlyStat.plugin(require('mongoose-lifecycle'));
var mapTag = function() {
var qos = { count: this.count, ups: this.ups , responsives: this.responsives, time: this.time, downtime: this.downtime };
View
2  models/tagMonthlyStat.js
@@ -12,6 +12,6 @@ var TagMonthlyStat = new Schema({
downtime : Number
});
TagMonthlyStat.index({ name: 1, timestamp: -1 }, { unique: true });
-TagMonthlyStat.plugin(require('../lib/lifecycleEventsPlugin'));
+TagMonthlyStat.plugin(require('mongoose-lifecycle'));
module.exports = mongoose.model('TagMonthlyStat', TagMonthlyStat);
View
1  package.json
@@ -5,6 +5,7 @@
"author": "Francois Zaninotto",
"dependencies": {
"mongoose": "2.6.5",
+ "mongoose-lifecycle": "1.0.0",
"express": "2.5.9",
"ejs": "0.7.1",
"config": "0.4.15",
View
4 plugins/console/index.js
@@ -9,7 +9,7 @@ exports.init = function(enableNewEvents, enableNewPings) {
};
var registerNewEventsLogger = function() {
- CheckEvent.on('postInsert', function(checkEvent) {
+ CheckEvent.on('afterInsert', 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('postInsert', function(ping) {
+ Ping.on('afterInsert', 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.