Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Last active November 12, 2017 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/dda85f709388cf09affa2eeeb3927ac1 to your computer and use it in GitHub Desktop.
Save vdeturckheim/dda85f709388cf09affa2eeeb3927ac1 to your computer and use it in GitHub Desktop.
const Cat = Mongoose.model('Cat', { name: String });
const CatProto = Object.getPrototypeOf(Cat); // this is the clean way to access an object's prototype. Please don't use __proto__ anymore.
const find = CatProto.find; // we take a reference on the orignial 'find method'
CatProto.find = function (...args) { // we replace the 'find' method on the prototype
const res = find.apply(this, args); // we call the original method we overrided
const exec = res.exec;
res.exec = async function () { // exec is the method actually firing the request on mongoose.
console.time('cat.find');
try {
const result = await exec.apply(this, arguments);
console.timeEnd('cat.find');
return result;
}
catch (err) {
console.timeEnd('cat.find');
throw err;
}
};
return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment