Support middleware skip option for cursors and custom hooks#16348
Merged
AbdelrahmanHafez merged 4 commits intoJun 29, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Extends the existing middleware skip option so users can selectively skip user-defined hooks when using query cursors, aggregation cursors, and custom statics/methods, while ensuring the Mongoose-only option is not forwarded to the MongoDB driver.
Changes:
- Add
middlewarefiltering support to query cursors (pre('find')+ per-documentpost('find')) and aggregation cursors (pre('aggregate')only), and stripmiddlewarebefore calling the MongoDB driver. - Enable per-call hook skipping for custom statics and methods by reading
middlewarefrom the trailing plain-object options argument via Kareem wrappergetOptions. - Update TypeScript types, docs, and add runtime + type-level tests; pin
kareemto a fork commit needed for wrappergetOptions.
Reviewed changes
Copilot reviewed 11 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| types/middlewares.d.ts | Update middleware function return typing to Kareem.OverwriteResult. |
| types/index.d.ts | Update overwriteMiddlewareResult() return type to Kareem.OverwriteResult. |
| types/aggregate.d.ts | Add cursor-specific options/types for aggregate cursors, including middleware skipping semantics. |
| test/types/model.skip.middleware.test.ts | Extend TS type tests to cover cursor middleware options and new aggregate cursor types. |
| test/model.skip.middleware.test.js | Add runtime tests for cursor middleware skipping, option parsing behavior, and custom statics/methods skipping. |
| package.json | Pin kareem dependency to a fork commit that supports wrapper getOptions. |
| lib/query.js | Document cursor hook behavior and cursor-level middleware option. |
| lib/helpers/model/applyStaticHooks.js | Add Kareem wrapper getOptions for custom statics to support per-call hook skipping. |
| lib/helpers/model/applyHooks.js | Add Kareem wrapper getOptions for custom document methods to support per-call hook skipping. |
| lib/helpers/buildMiddlewareFilter.js | Tighten skip detection to only treat strict false as “skip”. |
| lib/cursor/queryCursor.js | Apply middleware filtering to cursor execPre/execPost('find') and strip middleware before driver call. |
| lib/cursor/aggregationCursor.js | Apply middleware filtering to execPre('aggregate') and strip middleware before driver calls. |
| lib/aggregate.js | Document aggregate cursor middleware behavior and plumb cursor-level middleware into aggregate options. |
| docs/middleware.md | Document cursor behavior and recommended custom static/method signature for options. |
Comment on lines
614
to
+617
| function _nextDoc(ctx, doc, pop, callback) { | ||
| const postFilter = buildMiddlewareFilter(ctx.query.options, 'post'); | ||
| if (ctx.query._mongooseOptions.lean) { | ||
| return ctx.model.hooks.execPost('find', ctx.query, [[doc]]).then(() => callback(null, doc), err => callback(err)); | ||
| return ctx.model.hooks.execPost('find', ctx.query, [[doc]], { filter: postFilter }).then(() => callback(null, doc), err => callback(err)); |
Comment on lines
+5333
to
+5334
| * The `.cursor()` function triggers pre find hooks before opening the cursor | ||
| * and post find hooks for each document. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
re #8768, builds on top of mongoosejs/kareem#46
We left some parts uncovered in #15883, namely statics, methods, and cursors (aggregation, and query).
This PR:
middlewareskip option to query cursors, including both prefindhooks and per-document postfindhooks.aggregatehooks. Aggregate cursors still don't run postaggregatehooks, so the cursor-specific TS type only exposesmiddleware: falseand{ pre: false }.middlewareoption before passing cursor options to the MongoDB driver.middlewarefrom the final plain options object.For now this pins
kareemto the fork commit that adds wrappergetOptionssupport for custom statics/methods. Once the Kareem change lands and is released, we can switch this back to a normal version range.A design decision that was made is how we pass the
middlewareoption to statics/methods, I chose to pass them to the last arg if it's POJO, and the docs now have examples with a pattern where users define the last arg as options, following the same pattern as the other mongoose operations.