https://github.com/mongoosejs/mongoose-async-hooks
Node.js async hooks support for mongoose
https://github.com/mongoosejs/mongoose-async-hooks
Last synced: 2 months ago
JSON representation
Node.js async hooks support for mongoose
- Host: GitHub
- URL: https://github.com/mongoosejs/mongoose-async-hooks
- Owner: mongoosejs
- License: apache-2.0
- Created: 2018-05-07T22:20:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-05-06T21:22:00.000Z (about 1 year ago)
- Last Synced: 2025-03-24T05:13:41.775Z (3 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 3
- Watchers: 3
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-async-hooks
[Mongoose](http://mongoosejs.com/) plugin providing experimental support for [Node.js `async_hooks`](https://nodejs.org/api/async_hooks.html)
[](http://travis-ci.org/mongoosejs/mongoose-async-hooks)
# Importing
```javascript
// Using Node.js `require()`
const mongooseAsyncHooks = require('@mongoosejs/async-hooks');// Using ES6 imports
import mongooseAsyncHooks from '@mongoosejs/async-hooks';
```# Examples
## It integrates with Node.js async hooks
```javascript
const types = [];
const hooks = {
init: (asyncId, type) => {
types.push(type);
}
};const asyncHook = require('async_hooks').createHook(hooks);
const schema = new Schema({ name: String });
// Add this plugin
schema.plugin(mongooseAsyncHooks);const MyModel = mongoose.model('MyModel', schema);
asyncHook.enable();
const doc = new MyModel({ name: 'test' });
doc.save(function(error, doc) {
asyncHook.disable();assert.ok(types.includes('mongoose.MyModel'));
});
```