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(NODE-5791): type error with $addToSet in bulkWrite #3953

Merged
merged 10 commits into from Jan 3, 2024

Conversation

alenakhineika
Copy link
Contributor

@alenakhineika alenakhineika commented Dec 20, 2023

Description

The update methods for Collection and BulkWrite with $addToSet should not show a type error for collections with shema, collections extended from the Document/any type, and fields of any type.

What is changing?

  • The type of update in Collection.UpdateOneModel/Collection.UpdateManyModel/BulkWrite.updateOne/BulkWrite.updateMany becomes UpdateFilter<TSchema> | Document[].
  • The SetFields type helper skips validation for fields of any type (provided by custom interface or index signatures).
Is there new documentation needed for these changes?

None

What is the motivation for this change?

NODE-5647
NODE-4664

Release Highlight

The type error with $addToSet in bulkWrite was fixed

Previously the following code sample would show a type error:

interface IndexSingatureTestDocument extends Document {
    readonly myId: number;
    readonly mySet: number[];
  }
const indexSingatureCollection = undefined as unknown as Collection<IndexSingatureTestDocument>;
indexSingatureCollection.bulkWrite([
  {
    updateOne: {
      filter: { myId: 0 },
      update: {
        $addToSet: { mySet: 0 } // The type error! Type 'number' is not assignable to type 'never'.
      }
    }
  }
]);

It happened because the driver's Document type falls back to any, and internally we could not distinguish whether or not this assignment was intentional and should be allowed.

After this change, users can extend their types from Document/any, or use properties of any type and we skip the $addToSet validation in those cases.

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@alenakhineika alenakhineika changed the title fix(NODE-5647): type error with $addToSet in bulkWrite fix(NODE-5791): type error with $addToSet in bulkWrite Dec 20, 2023
@alenakhineika alenakhineika marked this pull request as ready for review December 20, 2023 13:54
@nbbeeken nbbeeken self-assigned this Dec 20, 2023
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Dec 20, 2023
@nbbeeken nbbeeken self-requested a review December 20, 2023 19:01
src/collection.ts Outdated Show resolved Hide resolved
src/collection.ts Show resolved Hide resolved
@W-A-James W-A-James self-requested a review December 28, 2023 20:26
src/bulk/common.ts Outdated Show resolved Hide resolved
src/bulk/common.ts Outdated Show resolved Hide resolved
@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Jan 2, 2024
@nbbeeken nbbeeken merged commit b93d405 into main Jan 3, 2024
22 of 27 checks passed
@nbbeeken nbbeeken deleted the NODE-5647-type-error-with-addtoset-in-bulkwrite branch January 3, 2024 17:07
@alenakhineika alenakhineika removed the Team Review Needs review from team label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants