Skip to content

mongoosejs/mongoose-async-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-async-hooks

Mongoose plugin providing experimental support for Node.js async_hooks

Build Status

Importing

// 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

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'));
});

About

Node.js async hooks support for mongoose

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •