-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 40d8f6b
Showing
6 changed files
with
435 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2012 Francois Zaninotto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
mongoose-lifecycle | ||
================== | ||
|
||
Mongoose plugin adding lifecyle events on the model class. | ||
|
||
Installation | ||
------------ | ||
|
||
Add the plugin as a dependency to your project in `package.json`: | ||
|
||
```javascript | ||
{ | ||
"name": "myproject", | ||
... | ||
"dependencies": { | ||
"mongoose": "2.6.5", | ||
"mongoose-lifecycle": "1.0.0", | ||
... | ||
}, | ||
} | ||
``` | ||
|
||
And run `npm install` again. | ||
|
||
Usage | ||
----- | ||
|
||
Initialization is straightforward: | ||
|
||
```javascript | ||
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. | ||
|
||
```javascript | ||
var Book = require('path/to/models/book'); | ||
Book.on('beforeInsert', function(book) { | ||
// do stuff... | ||
}); | ||
``` | ||
|
||
License | ||
------- | ||
|
||
MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* 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); | ||
this.isNew ? model.emit('beforeInsert', this) : model.emit('beforeUpdate', this); | ||
this._isNew_internal = this.isNew; | ||
next(); | ||
}); | ||
schema.post('save', function() { | ||
var model = this.model(this.constructor.modelName); | ||
model.emit('afterSave', this); | ||
this._isNew_internal ? model.emit('afterInsert', this) : model.emit('afterUpdate', this); | ||
this._isNew_internal = undefined; | ||
}); | ||
schema.pre('remove', function (next) { | ||
this.model(this.constructor.modelName).emit('beforeRemove', this); | ||
next(); | ||
}); | ||
schema.post('remove', function() { | ||
this.model(this.constructor.modelName).emit('afterRemove', this); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "mongoose-lifecycle", | ||
"description": "Mongoose plugin adding lifecyle events on the model class", | ||
"version": "1.0.0", | ||
"author": "Francois Zaninotto", | ||
"keywords": ["mongodb", "mongoose", "plugin", "presave", "beforesave"], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/fzaninotto/mongoose-lifecycle" | ||
}, | ||
"devDependencies": { | ||
"should": ">=0.2.1", | ||
"mongoose": ">=2.6.5", | ||
"cli-table" : ">=0.0.1" | ||
}, | ||
"license": "MIT", | ||
"engine": { | ||
"node": ">=0.6" | ||
} | ||
} |
Oops, something went wrong.