You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@Transactions decorator, Expose and accept a transaction object #13004
TBH I do not like this approach too much (Passing extra stuff, too noisy and annoying)
Alternatives
AFAIK nothing except those listed in the doc which is not practical in most cases.
Reacted by Carlo Corradini, Rastsislau Azaronak, Vladislav, Damian Bouné, vloc-beom-kim, omershac, Bennet Hölscher, Ryunosuke Onda, Petr Vnenk, Giuseppe Miragliotta and 6 more
We have to suffer to do a simple transaction in Prisma until someone/Prisma team decide to develop a more vividly API/decorator for the transactions which is in a dire need situation.
@asio1337's answer points to the "interactive transactions" preview where you can ask Prisma to open a transaction, and then get a "new" prisma inside that function that is bound to a transaction. You can write all the code you want in there, and any time you want to use this transaction, you just use prisma as you usually would - but your query will be using the existing transaction:
...
return await prisma.$transaction(async (prisma) => {
// 1. Decrement amount from the sender.
const sender = await prisma.account.update({
data: {
balance: {
decrement: amount,
},
},
where: {
email: from,
},
})
// 2. Verify that the sender's balance didn't go below zero.
if (sender.balance < 0) {
throw new Error(`${from} doesn't have enough to send ${amount}`)
}
// 3. Increment the recipient's balance by amount
const recipient = prisma.account.update({
data: {
balance: {
increment: amount,
},
},
where: {
email: to,
},
})
return recipient
})
...
Prisma supports transaction out off the box. But in the case you need more control, there is the "interactive transactions" API, which is very nice because:
Prisma is the fastest client on JS
Best Typed
It works seamless with the SQL tag plugin for VS Code.
The only disadvantage is the missing transaction mode.
Reacted by Kyle Barrett, Javien Lee, Petr Vnenk, leungtinyau27m5, thiago-silva-deel, bhdn0monteyama, JackCme, Joaquin Luna, jan526, Mohammad Jawad (Kasir) Barati and 4 more
Reacted by Cesar V. Sampaio, GeonHun Kim, barnc, Michail Shcherbakov, Joaquin Luna, jan526, Mohammad Jawad (Kasir) Barati and Cheeshian Chuah
We will close this issue when and if the original feature request is covered by an existing feature. Let's wait for @kasir-barati and @cesarvspr getting back to this and letting us know if the interactive transaction feature fulfills their requirements. It might be that this is not flexible enough or does not the work they expect or want.
Hi @janpio, thank you for you're kindness. IMO and as far as I can say it almost all the time is the case that we wanna create a transaction for a method - Either it is a method in the service layer, or anywhere else - and decorators (@Transactions) gave us more flexibility.
@asio1337 We do not say that this is a feature which is implemented. I am saying we need to have it. Transaction's usability should be increases in the future.
@ParthGandhi Unfortunately I have to confess it, NO. I do not know what else we can do for this problem since most of the time we used to use repository pattern and with current API ATM for transactions AFAIK we cannot do too much to overcome this issue.
Would like to add to @kasir-barati, this is something we would love to have in prisma. Currently relying on the hacky solution proposed in #12458 and it is not safe to use across concurrent requests.
@janpio Our team is also waiting for @transactional decorator solution, we use totally same architecture that kasir-barati provide #13004 (comment). Do you have any plans to implement it in the nearest future?
If you use NestJS, please have a look at the @nestjs-cls/transactional plugin for nestjs-cls, which uses AsyncLocalStorage to propagate the transaction.
If you use NestJS, please have a look at the @nestjs-cls/transactional plugin for nestjs-cls, which uses AsyncLocalStorage to propagate the transaction.
Worth to who is already in the nestjs framework but this is clearly ugly. A single decorator is supposed to be simple and beautiful to read. No need for this much of tooling.
I wholeheartedly agree, but I don't see Prisma offering the @Transactional decorator any time soon until ES6 decorators are in the language. It would just mean more technical debt to them because TS decorators and future ES6 decorators are not compatible.
Also, if they offered propagating transactions via AsyncLocalStorage out of the box, the tooling around it would become much simpler, too.
Until that, I guess we have to rely on external tooling.
Activity
asix1337 commentedon Apr 26, 2022
https://www.prisma.io/docs/concepts/components/prisma-client/transactions#interactive-transactions-in-preview
cesarvspr commentedon Jun 10, 2022
Is there any way to use a @transaction decorator? @kasir-barati
I don't get you answer. @asio1337
kasir-barati commentedon Jun 11, 2022
@cesarvspr Nope, Unfortunately 😭
We have to suffer to do a simple transaction in Prisma until someone/Prisma team decide to develop a more vividly API/decorator for the transactions which is in a dire need situation.
janpio commentedon Jun 11, 2022
@asio1337's answer points to the "interactive transactions" preview where you can ask Prisma to open a transaction, and then get a "new"
prismainside that function that is bound to a transaction. You can write all the code you want in there, and any time you want to use this transaction, you just useprismaas you usually would - but your query will be using the existing transaction:Is that not what you are asking for here?
asix1337 commentedon Jun 11, 2022
Prisma supports transaction out off the box. But in the case you need more control, there is the "interactive transactions" API, which is very nice because:
The only disadvantage is the missing transaction mode.
asix1337 commentedon Jun 11, 2022
There is no such decorator on prisma. It seems your're new on Prisma. Just read the docs my friend :)
asix1337 commentedon Jun 11, 2022
#close please
janpio commentedon Jun 11, 2022
We will close this issue when and if the original feature request is covered by an existing feature. Let's wait for @kasir-barati and @cesarvspr getting back to this and letting us know if the interactive transaction feature fulfills their requirements. It might be that this is not flexible enough or does not the work they expect or want.
kasir-barati commentedon Jun 11, 2022
Hi @janpio, thank you for you're kindness. IMO and as far as I can say it almost all the time is the case that we wanna create a transaction for a method - Either it is a method in the service layer, or anywhere else - and decorators (
@Transactions) gave us more flexibility.@asio1337 We do not say that this is a feature which is implemented. I am saying we need to have it. Transaction's usability should be increases in the future.
6 remaining items
kasir-barati commentedon Jul 18, 2022
@ParthGandhi Unfortunately I have to confess it, NO. I do not know what else we can do for this problem since most of the time we used to use repository pattern and with current API ATM for transactions AFAIK we cannot do too much to overcome this issue.
adrian-goe commentedon Sep 20, 2022
please have a look at #12975, that discusses the same topic.
And also have a look at #12458
The problems discussed in #12458 regarding the test situation would also be solved with a
@Transactionalannotation.As said in my other comments in the other issues, this is more or less a standart in the Spring Hibernate world.
nickabb commentedon Sep 21, 2022
Would like to add to @kasir-barati, this is something we would love to have in prisma. Currently relying on the hacky solution proposed in #12458 and it is not safe to use across concurrent requests.
adrian-goe commentedon Oct 27, 2022
@janpio any update in this?
janpio commentedon Nov 2, 2022
No. This is a feature request that we are tracking and might prioritize in the future. Right now that is not the case.
Updates will be posted in issue or communicated through our public roadmap at https://pris.ly/roadmap
Raaz1017 commentedon Mar 1, 2023
@janpio Our team is also waiting for @transactional decorator solution, we use totally same architecture that kasir-barati provide #13004 (comment). Do you have any plans to implement it in the nearest future?
janpio commentedon Mar 1, 2023
No, this is currently not near the top of the priority list. We still will look at this in the future, but do not expect it in weeks or months.
yigitahmetsahin commentedon Apr 20, 2023
This feature is also what we are needing for.
GZLiew commentedon Aug 15, 2023
same here this is a useful feature to have
Papooch commentedon Jan 24, 2024
If you use NestJS, please have a look at the
@nestjs-cls/transactionalplugin fornestjs-cls, which uses AsyncLocalStorage to propagate the transaction.See also #5729 (comment)
cesarvspr commentedon Jan 24, 2024
Worth to who is already in the nestjs framework but this is clearly ugly. A single decorator is supposed to be simple and beautiful to read. No need for this much of tooling.
Papooch commentedon Jan 24, 2024
I wholeheartedly agree, but I don't see Prisma offering the
@Transactionaldecorator any time soon until ES6 decorators are in the language. It would just mean more technical debt to them because TS decorators and future ES6 decorators are not compatible.Also, if they offered propagating transactions via AsyncLocalStorage out of the box, the tooling around it would become much simpler, too.
Until that, I guess we have to rely on external tooling.
jansustersic526 commentedon Dec 3, 2024
@janpio, can we expect this anytime soon?
roopaish commentedon Aug 3, 2025
I made this one if anyone wants to try
https://github.com/roopaish/prisma-transaction
Its based on https://github.com/nicheeeer/prisma-transaction, but this was not working for me so I modified it.