Suggestion: using AsyncLocalStorage for transactions
See original GitHub issueProblem
First, a disclaimer: AsyncLocalStorage is not available in all supported versions of Node.js yet but this can either be discussed for the future or there are ways of having similar APIs in older version sof Node.js depending on the choices made.
Right now (if I understand the doc well), to build a transaction, one must keep a list of writes and commit all of them at once at the end. So let’s imagine in a web server, when the user wants all the db writes linked with the HTTP requests to be in the same transaction, they would have to do something like attaching an array to a request object or something like that, add all the writes to it and commit before the response with prisma.$transaction(...).
Suggested solution
Using an API like AsyncLocalStorage can give the following UX:
prisma.$openTransaction()
...
write_1
...
write_n
prisma.$commitTransaction() <- all the writes are automatically linked with the current transaction
This has a few shortcomings:
- how do you do when you have multiple transactions at once?
 - AsyncLocalStorage is still experiemental in node (well, I am working on that https://github.com/nodejs/node/issues/35286#issuecomment-776870263)
 
Alternatives
N/A
Additional context
So I am mostly trying the water to see if there would be interest and what would be the feedback over here on such feature 😃
Issue Analytics
- State:
 - Created 3 years ago
 - Reactions:6
 - Comments:6 (2 by maintainers)
 

Top Related StackOverflow Question
I agree with the proposition. Without that, it will be impossible for us to migrate or even used Prisma in any production application. It’s an essential feature to manage transactions correctly without programmation overhead.
Other than that, Prisma fixes a lot of ORMs commons problems. I’m in love with what you’ve built, it’s just amazing.
Let me know if I can help you to provide this feature, it is really essential for all large applications. And Prisma deserves to be used by large applications.
@sorenbs this forces you to always have access to this singleton array, meaning you need to pass a context with all function calls. The point of AsyncLocalStorage is to be able not to do that and provide a UX where the code would be the same inside and outside transactions.