https://github.com/dylanlott/mongoose-fire
https://github.com/dylanlott/mongoose-fire
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/dylanlott/mongoose-fire
- Owner: dylanlott
- Created: 2017-10-19T17:03:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-30T02:16:10.000Z (over 8 years ago)
- Last Synced: 2025-01-30T01:29:44.967Z (over 1 year ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongoose-fire
> Make any model emit events on create, update, and delete.
# Install
`npm install --save mongoose-fire`
Then attach it to each model that you want to be an event emitter
```
const mongoosefire = require('mongoose-fire');
exampleSchema.plugin(mongoosefire);
```
And you're done!
# Events
Once these plugins are installed, you can access the following events:
```
Model.on('create')
Model.on('update')
Model.on('update:')
Model.on('remove')
```
## Notice about Middleware hooks
Events won't be fired when `findByIdAndUpdate` and other similar aggregate methods are used
To get an event to fire, you'll have to manually edit the document and manually call the `save()` method.
This is because of the way Mongoose handles `findByIdAndUpdate` behind the scenes with the MongoDB driver.
Example
```javascript
Model.findByIdAndUpdate(id, { update: 'with this' }, (err, data) => {
// this won't fire the `update` or `update:` method
})
// instead you need to do
Model.findOne({ foo: 'bar' })
.then((doc) => {
doc.foo = 'baz'
doc.save(); // the `update` and `update:foo` events will now be fired
})
// and in callback style
Model.findOne({ foo: 'bar'}, function (err, doc) {
doc.foo = 'baz'
doc.save(); // `update` and `update:foo` events will now be fired
});
```
In general, I prefer to manually find and edit documents this way anyway.