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

feat: add transactionAsyncLocalStorage option to opt in to automatically setting session on all transactions #14583

Merged
merged 5 commits into from
May 15, 2024

Conversation

vkarpov15
Copy link
Collaborator

Fix #13889

Summary

Having to pass session option to every operation in a transaction is cumbersome and error prone. Node 16+ has a stable AsyncLocalStorage class as part of the async_hooks API; we can use AsyncLocalStorage to set session option by default on all operations in a function as long as the user uses Connection.prototype.transaction() wrapper. See docs/transactions.md changes for an example.

Examples

…cally setting `session` on all transactions

Fix #13889
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.

Mostly looks good to me, though one minor code example thing and failing Replica set tests because of this PR (this time not because of watch errors)

test error for reference:

   1) transactions
       transactionAsyncLocalStorage option
         transaction() sets `session` by default if transactionAsyncLocalStorage option is set:
     AssertionError [ERR_ASSERTION]: The input did not match the regular expression /Oops!/. Input:

'AssertionError [ERR_ASSERTION]: 0 == 1'

      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Context.<anonymous> (test/docs/transactions.test.js:374:7)

docs/transactions.md Outdated Show resolved Hide resolved
@hasezoey hasezoey added the new feature This change adds new functionality, like a new method or class label May 12, 2024
vkarpov15 and others added 2 commits May 13, 2024 10:30
Co-authored-by: hasezoey <hasezoey@gmail.com>
@vkarpov15 vkarpov15 added this to the 8.4 milestone May 15, 2024
@vkarpov15 vkarpov15 merged commit ec7bad6 into 8.4 May 15, 2024
23 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-13889 branch May 15, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants