Skip to content

@Transactions decorator, Expose and accept a transaction object #13004

Open

Description

@kasir-barati

Problem

We cannot use a @Transaction decorator or access the transaction object and pass it directly to the query itself.

Suggested solution

Any thought on doing this: Returning $transaction to the user and pass it to the API interface as a argument. Something like this:

const transactionObj = prisma.createTransaction();
const user = await prisma.user.create({
  data: {
    email: '[email protected]',
    name: 'Elsa Prisma',
  },
  { transactions: transactionObj }
})
const temp = prisma.temp.update({ where: { id: 1}, data: {f1: 1},  { transactions: transactionObj }})

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.

Activity

  1. added
    kind/feature
    topic: $transaction
    on Apr 26, 2022
  2. added
    topic: extend-client
    on Apr 26, 2022
  3. cesarvspr commented on Jun 10, 2022

    @cesarvspr

    Is there any way to use a @transaction decorator? @kasir-barati

    I don't get you answer. @asio1337

  4. kasir-barati commented on Jun 11, 2022

    @kasir-barati
    Author

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

  5. janpio commented on Jun 11, 2022

    @janpio
    Contributor

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

    Is that not what you are asking for here?

  6. asix1337 commented on Jun 11, 2022

    @asix1337

    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.

  7. asix1337 commented on Jun 11, 2022

    @asix1337

    Is there any way to use a @transaction decorator? @kasir-barati

    I don't get you answer. @asio1337

    There is no such decorator on prisma. It seems your're new on Prisma. Just read the docs my friend :)

  8. asix1337 commented on Jun 11, 2022

    @asix1337

    #close please

  9. janpio commented on Jun 11, 2022

    @janpio
    Contributor

    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.

  10. kasir-barati commented on Jun 11, 2022

    @kasir-barati
    Author

    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.

  11. 6 remaining items

  12. kasir-barati commented on Jul 18, 2022

    @kasir-barati
    Author

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

  13. adrian-goe commented on Sep 20, 2022

    @adrian-goe

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

    As said in my other comments in the other issues, this is more or less a standart in the Spring Hibernate world.

  14. nickabb commented on Sep 21, 2022

    @nickabb

    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.

  15. adrian-goe commented on Oct 27, 2022

    @adrian-goe

    @janpio any update in this?

  16. janpio commented on Nov 2, 2022

    @janpio
    Contributor

    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

  17. Raaz1017 commented on Mar 1, 2023

    @Raaz1017

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

  18. janpio commented on Mar 1, 2023

    @janpio
    Contributor

    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.

  19. yigitahmetsahin commented on Apr 20, 2023

    @yigitahmetsahin

    This feature is also what we are needing for.

  20. GZLiew commented on Aug 15, 2023

    @GZLiew

    same here this is a useful feature to have

  21. Papooch commented on Jan 24, 2024

    @Papooch

    If you use NestJS, please have a look at the @nestjs-cls/transactional plugin for nestjs-cls, which uses AsyncLocalStorage to propagate the transaction.

    See also #5729 (comment)

  22. cesarvspr commented on Jan 24, 2024

    @cesarvspr

    If you use NestJS, please have a look at the @nestjs-cls/transactional plugin for nestjs-cls, which uses AsyncLocalStorage to propagate the transaction.

    See also #5729 (comment)

    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.

  23. Papooch commented on Jan 24, 2024

    @Papooch

    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.

  24. jansustersic526 commented on Dec 3, 2024

    @jansustersic526

    @janpio, can we expect this anytime soon?

  25. roopaish commented on Aug 3, 2025

    @roopaish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featuretopic: $transactiontopic: extend-clienttopic: interactiveTransactionstopic: transaction

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions