question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: using AsyncLocalStorage for transactions

See original GitHub issue

Problem

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:

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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
BertholetDamiencommented, Jul 19, 2021

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.

2reactions
vdeturckheimcommented, Jun 15, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AsyncLocalStorage for Easy Context Passing in Node.js | Trabe
With Node.js 13.10, we got a a new feature called AsyncLocalStorage used to implement CLS (continuation local storage). We previously used cls-hooked, ...
Read more >
What's the one thing you struggled with while learning Nodejs
For transactions I use AsyncLocalStorage and some wrapper functions, and it's been working great! : import { getTransactionOrKnex ...
Read more >
Introduction to the Node.js reference architecture, Part 2
Specifically, in the latest Node.js versions, you can access AsyncLocalStorage. These APIs are still experimental but have been relatively ...
Read more >
IBM and Red Hat's recommendations for logging in Node.js ...
The Node.js Reference Architecture from Red Hat and IBM recommends using Pino for logging in Node.js applications.
Read more >
Simple Context Passing With AsyncLocalStorage in Node.js
AsyncLocalStorage in Node.js can solve problems have been encountering for years. ... single SQL transaction per HTTP request and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found