New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] option to disable running pre/post hooks #8768
Comments
I like this idea, definitely something to consider for a future minor release. Right now the only easy workaround is to just use I think I'd call the option |
@vkarpov15 I've been trying to implement this, but I couldn't find where the middleware-execution-related code is. I see that we're defining pre/post hooks using Kareem, and in |
@AbdelrahmanHafez Kareem's |
@AbdelrahmanHafez did you make any progress on this one? It could be a very useful feature. In our case, we have a bunch of scripts written, mostly for data migration, etc where we want the pre/post hooks to be disabled by default. |
We have found a workaround for this by adding disableMiddlewares field in the query options and then check for it in the middleware itself
|
@madaher-dev I've been using something similar to your code to pass custom query options to the pre('find') query middleware and it has been working nicely. Basically I saved the custom query option on app.locals and read it in the pre hook and obviously needed to reset it on app.locals back to undefined on every new request... so your solution is more elegant and robust. I have another issue though (and that's how I have found this issue): I would need to pass a custom query option to the pre('save') hook, which is a document middleware (not query middleware), so I'm wondering if it's at all possible to do that on the document itself. To be clear, |
this.options would not work on this case. Note that save pre middleware works save() and .create() but if you want to skip that why not use findOneAndUpdate with upsert option to skip the pre save middleware? |
findOneAndUpdate() wouldn't work in my case for a couple of reasons that are related to the specific way I have set up the app. const ops = docs.map(document => ({insertOne: {document}}));
await TheModel.bulkWrite(ops);
Also, wanted to mention for the pre('find') query middleware, wouldn't it be better to use Edit: It works as imagined: |
Re: Re: getting save options in 'use strict';
const mongoose = require('mongoose');
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
const schema = mongoose.Schema({
name: String
});
schema.pre('save', function(next, opts) {
console.log('SaveOptions:', opts); // 'SaveOptions: SaveOptions { myopt: true }'
});
const Test = mongoose.model('Test', schema);
const doc = new Test({ name: 'bar' });
await doc.save({ myopt: true });
console.log('Done');
process.exit(0);
} |
@vkarpov15 Huh, that's nice! I haven't seen this approach documented in https://mongoosejs.com/docs/middleware.html I assume I can pass custom options from |
@lorand-horvath we will add that to the docs |
I am not sure whether this is present or not, but it would be great if we have a way to optionally disable running middlewares, a couple of use cases that I could think of is for internal use in the library, this would help us avoid situations like #8739.
Other developers could also use this in their applications if they need to run a query without triggering middlewares, or inside the logic of the middleware in itself to avoid an infinite loop such as the custom option in #8756 (comment).
Considering the following API:
Later on, we could make it so that the options accept an object
The text was updated successfully, but these errors were encountered: