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