Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Jun 12, 2012
0 parents commit 40d8f6b
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions LICENSE
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.
58 changes: 58 additions & 0 deletions README.md
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
49 changes: 49 additions & 0 deletions index.js
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);
});
};
20 changes: 20 additions & 0 deletions package.json
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"
}
}
Loading

0 comments on commit 40d8f6b

Please sign in to comment.