-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (69 loc) · 2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Mongoose plugin adding lifecyle events on the model class.
*
* Initialization is straightforward:
*
* var lifecycleEventsPlugin = require('path/to/mongoose-lifecycle');
* var Book = new Schema({ ... });
* Book.plugin(lifecycleEventsPlugin);
*
* 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...
* });
*/
module.exports = exports = function lifecycleEventsPlugin(schema) {
schema.pre('save', function (next) {
var model = this.model(this.constructor.modelName);
model.emit('beforeSave', this);
schema.emit('beforeSave', this);
if (this.isNew) {
model.emit('beforeInsert', this);
schema.emit('beforeInsert', this);
}
else {
model.emit('beforeUpdate', this);
schema.emit('beforeUpdate', this);
}
this._isNew_internal = this.isNew;
next();
});
schema.post('save', function() {
var model = this.model(this.constructor.modelName);
model.emit('afterSave', this);
schema.emit('afterSave', this);
if (this._isNew_internal) {
model.emit('afterInsert', this);
schema.emit('afterInsert', this);
}
else {
model.emit('afterUpdate', this);
schema.emit('afterUpdate', this);
}
this._isNew_internal = undefined;
});
schema.pre('remove', function (next) {
var model = this.model(this.constructor.modelName);
model.emit('beforeRemove', this);
schema.emit('beforeRemove', this);
next();
});
schema.post('remove', function() {
var model = this.model(this.constructor.modelName);
model.emit('afterRemove', this);
schema.emit('afterRemove', this);
});
};