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-4108): improve return type for withTransaction() #3203

Closed
wants to merge 1 commit into from
Closed

fix(NODE-4108): improve return type for withTransaction() #3203

wants to merge 1 commit into from

Conversation

vkarpov15
Copy link
Contributor

Description

The withTransaction() function signature implies that withTransaction() returns the same value that the user-provided callback returns. But that isn't the case, it always returns something like this:

{
  ok: 1,
  '$clusterTime': {
    clusterTime: new Timestamp({ t: 1650213729, i: 1 }),
    signature: {
      hash: new Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: 0
    }
  },
  operationTime: new Timestamp({ t: 1650213729, i: 1 })
}

Reported in Automattic/mongoose#9919

What is changing?

Function signature for withTransaction()

Is there new documentation needed for these changes?

What is the motivation for this change?

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>
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@nbbeeken nbbeeken changed the title fix(sessions): improve return type for withTransaction() fix(NODE-4108): improve return type for withTransaction() Apr 18, 2022
Copy link
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

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

It just so happens a ticket to fix this work was next up on our scheduled queue, let me know if it would be easier for you if I took on making the changes mentioned:

options?: TransactionOptions
): ReturnType<typeof fn> {
): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though the return type is meant to be an internal command result we should reflect what's actually returned here and we can fix this in the next major version. This function will return the command result if the transaction commits successfully, it will throw if it aborts and it will return undefined if it's aborted manually within the user provided function, so it's important that the result at least demonstrate that it can be truthy or not under the right circumstance.
So we should put the type in here that matches the actual result from the function, and add tests to prevent a regression.

@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Apr 21, 2022
@nbbeeken
Copy link
Contributor

nbbeeken commented May 6, 2022

#3236

@nbbeeken nbbeeken closed this May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Primary Review In Review with primary reviewer, not yet ready for team's eyes
Projects
None yet
2 participants