Skip to content
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

Fix: Mongoose types incorrect for when includeResultMetadata: true is set #14078

Merged
merged 11 commits into from Nov 16, 2023

Conversation

prathamVaidya
Copy link
Contributor

@prathamVaidya prathamVaidya commented Nov 13, 2023

Summary

Fixed Issue #13987
Mongoose Version: 8.0

When includeResultMetadata is set to true. The document parameter in post middleware can also be ModifyResult<>.

Functions that support includeResultMetadata option are : findOneAndUpdate, findByIdAndUpdate, findOneAndReplace, findByIdAndDelete, and findOneAndDelete.

Changes Done:

  • Introduced a separate document type for the post middleware, specifically for these functions, ensuring they also return ModifyResult<>.
  • Some of these functions were not returning ModifyResult<> when includeResultMetadata was set to True. I've addressed this by adding appropriate return types.
  • In cases where includeResultMetadata is false and no matching document is found in any of these functions, the document parameter in the post middleware is NULL. So I have included NULL as a possible doc type in the middleware parameter.

Examples

import mongoose, { ModifyResult, Query } from "mongoose";

const UserSchema = new mongoose.Schema({
  id: String,
  username: String,
});

UserSchema.post<
  Query<
    mongoose.HydratedDocument<mongoose.InferSchemaType<typeof UserSchema>>,
    mongoose.InferSchemaType<typeof UserSchema>
  >
>("findOneAndDelete", function (doc) {
// doc can be NULL | QueryResultType<T> | ModifyResult<QueryResultType<T>>
  if (doc && "value" in doc) {
    console.debug("DELETE VALUE = ", doc.value);
  }
});

const UserModel = mongoose.model("User", UserSchema);

async function main() {
  await mongoose.connect(`mongodb://localhost:27017/mongoose-test`);

  await mongoose.connection.dropDatabase();

  const doc = new UserModel({ username: "user1" });

  await doc.save();

  const upsertResult = await UserModel.findOneAndDelete(
    {username: "user1"},
    {
      includeResultMetadata: true,
    }
  ).exec();

  if (upsertResult) {
    console.log("Result=", upsertResult.value); // it has type set to ModifyResult now
  } else {
    console.log("NULL", upsertResult);
  }
}

main();

I have fixed the issue for v8.0. If the solution is acceptable then I will also do it for v7.4 that was originally specified in the Issue. @hasezoey

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks for your PR. Please fix lint, add a test case, and address the comments in this review 👍

types/models.d.ts Outdated Show resolved Hide resolved
types/middlewares.d.ts Outdated Show resolved Hide resolved
types/models.d.ts Show resolved Hide resolved
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.

Looks good to me, though some minor questions / suggestions.

also does someone know why the tests are not running for the latest commits?

types/middlewares.d.ts Outdated Show resolved Hide resolved
types/models.d.ts Show resolved Hide resolved
@vkarpov15
Copy link
Collaborator

Can you please fix merge conflicts?

@prathamVaidya
Copy link
Contributor Author

Fixed Merge Conflicts ✅

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks 👍

@vkarpov15 vkarpov15 added this to the 8.0.2 milestone Nov 16, 2023
@vkarpov15 vkarpov15 merged commit 72b4094 into Automattic:master Nov 16, 2023
3 checks passed
vkarpov15 pushed a commit that referenced this pull request Nov 16, 2023
… set (#14078)

* Added Seperate type for Middlewares that support includeResultMetadata

* Added Return Type as Raw Result when includeResultMetadata is TRUE for all middlewares

* Added Seperate post query type for middlewares that support includeResultMetadata

* Added Seperate post query type for middlewares that support includeResultMetadata, Fixed Lint

* Updated MongooseRawResultQueryMiddleware and removed duplicate and redundant middlewares

* Removed 'findByIdAndRemove' type definations : DEPRECATED, Fixed Linting Errors

* Removed test cases for 'findByIdAndRemove', Added test case for 'findOneAndDeleteRes', 'findByIdAndDeleteRes'

* Updated findOneAndDelete, Added test case for findOneAndUpdate, findOneAndReplace middlewares
vkarpov15 added a commit that referenced this pull request Nov 16, 2023
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.

None yet

3 participants