-
-
Save vdeturckheim/dda85f709388cf09affa2eeeb3927ac1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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