-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-2014)!: return the result of provided callback in withTransaction and withSession #3524
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ export interface ClientSessionOptions { | |
} | ||
|
||
/** @public */ | ||
export type WithTransactionCallback<T = void> = (session: ClientSession) => Promise<T>; | ||
export type WithTransactionCallback<T = any> = (session: ClientSession) => Promise<T>; | ||
|
||
/** @public */ | ||
export type ClientSessionEvents = { | ||
|
@@ -470,10 +470,7 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | |
* @param options - optional settings for the transaction | ||
* @returns A raw command response or undefined | ||
*/ | ||
withTransaction<T = void>( | ||
fn: WithTransactionCallback<T>, | ||
options?: TransactionOptions | ||
): Promise<Document | undefined> { | ||
withTransaction<T>(fn: WithTransactionCallback<T>, options?: TransactionOptions): Promise<T> { | ||
const startTime = now(); | ||
return attemptTransaction(this, startTime, fn, options); | ||
} | ||
|
@@ -615,12 +612,14 @@ function attemptTransaction<TSchema>( | |
} | ||
|
||
return promise.then( | ||
() => { | ||
value => { | ||
if (userExplicitlyEndedTransaction(session)) { | ||
return; | ||
return value; | ||
} | ||
|
||
return attemptTransactionCommit(session, startTime, fn, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it still be possible to get the legacy return value somehow (i.e. the I think it would be an acceptable breaking change in mongosh if that goes away (right now our own To avoid this, you could do some hackery to attach this secondary result to the returned Promise itself or return a tuple, e.g.
(that would also maybe solve the problem mentioned above about distinguishing the callback returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only worry here is with this change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it could just be a separate function or item in |
||
return attemptTransactionCommit(session, startTime, fn, options).then(() => { | ||
return value; | ||
}); | ||
}, | ||
err => { | ||
function maybeRetryOrThrow(err: MongoError): Promise<any> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs need a little update too:
Which makes me question, should we require that the callback return something truthy (via TS) or default to something truthy (maybe
true
) if the callback returnsundefined
? Otherwise, how can someone differentiate an intentionally aborted transaction and a committed one if they don't have a return value?Spec ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think returning
true
sounds good if they haven't returned anything.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me, I'm thinking we can just add
value ?? true
(this would also coverreturn null
, but I think that's ok?) and a test for that case along with docs. I'll update the ticket AC just so we keep track