Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

@vkarpov15 vkarpov15 commented Dec 21, 2025

Fix #15870

Summary

Make it possible to access parameters from document middleware - currently not possible in Mongoose 8 or 9. Can't make this change for post middleware because that would be backwards breaking.

With this PR, you can do:

schema.pre('deleteOne', { document: true, query: false }, function(doc, options) {
  // Check options
});

Examples

@vkarpov15 vkarpov15 marked this pull request as draft December 21, 2025 22:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request enhances document middleware for deleteOne and updateOne operations by making options and update parameters accessible in pre-hooks. This addresses issue #15870 where these parameters were previously inaccessible in document middleware.

Key Changes:

  • Added new TypeScript middleware function types (PreDeleteOneMiddlewareFunction and PreUpdateOneMiddlewareFunction) with extended parameter signatures
  • Updated middleware execution in lib/model.js and lib/document.js to pass options (and update for updateOne) to pre-hooks
  • Added specific method overloads for deleteOne and updateOne in schema pre-hook type definitions

Reviewed changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
types/middlewares.d.ts Defines new middleware function types with additional parameters for document-level deleteOne and updateOne hooks
types/index.d.ts Adds specific overloads for deleteOne and updateOne pre-hooks to support the new parameter signatures
test/types/schema.test.ts Updates type tests to verify the new middleware signatures accept doc, update, and options parameters
test/model.middleware.test.js Enhances deleteOne test to verify options parameter is accessible in pre-hook
test/document.test.js Updates updateOne test to verify both update and options parameters are accessible in pre-hook, but incorrectly extends this to post-hook
lib/model.js Modifies deleteOne middleware execution to pass options parameter to pre-hooks
lib/document.js Updates parameter name from doc to update for clarity and modifies updateOne middleware execution to pass document, update, and options to pre-hooks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vkarpov15 vkarpov15 marked this pull request as ready for review December 23, 2025 02:25
@vkarpov15 vkarpov15 added this to the 8.21 milestone Dec 23, 2025
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 changed the base branch from 8.x to 8.21 December 29, 2025 18:56
@vkarpov15 vkarpov15 merged commit 5fe843c into 8.21 Dec 29, 2025
76 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15870 branch December 30, 2025 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants