Skip to content

Callback-free interactive transactions #12458

Open

Description

@millsp

Problem

Some users are asking for a less restricted way for opening an interactive transaction. Instead of having a callback for the transaction, there would be manual calls to $begin, $commit, and $rollback.

Suggested solution

Example of a use-case

describe('some test', () => {
   let trx: Transaction;

   beforeEach(async () => {
      trx = await prisma.$begin();
      jest.mock('./prisma', () => trx);
   });

   afterEach(async () => {
      await trx.$rollback();
   });

   it('.....', () => {
      // ....
   });
});

Alternatives

Additional context

#1844 (comment)

Activity

  1. sijakubo commented on Mar 31, 2022

    @sijakubo

    That would be great to have. We are currently facing the exact same issue

  2. janpio commented on Mar 31, 2022

    @janpio
    Contributor

    Can you elaborate on your use case @sijakubo? That is always valuable to have in such issues as signal why someone wants a feature really.

  3. sroettering commented on Apr 1, 2022

    @sroettering

    I would love this feature as well. In my use case we have integration tests running with jest that need to be wrapped in a transaction so we can run many tests in parallel without bothering about side effects for other tests. There are of course techniques like cloning databases or using multiple schemas but they have limits. So a simple transaction would in most cases be more than enough. With the callback solution this is not possible right now.
    The proposed API already looks promising for this.

  4. sijakubo commented on Apr 1, 2022

    @sijakubo

    Hi @janpio,

    We are trying to ramp up our CI speed and in order to do so, we need to run the database integration tests in parallel. This is currently not possible, due to the lack of manual transaction handling within prisma.

    I have multiple years of experience in Java / Spring where it is almost default behavior to run your tests within a transaction, which rollbacks after the execution of the test to run several tests in parallel.

    The suggested example is exactly what we would need to increase our CI speed significantly

  5. garrensmith commented on Apr 4, 2022

    @garrensmith
    Contributor

    I was thinking that instead of having a manual transactions, why not rather change the test to be something like:

      describeInTransaction('some test', (trx) => {
       
       beforeEach(async () => {
               jest.mock('./prisma', () => trx);
       });
    
       it('.....', () => {
          trx.user.findUnique(...)
       });
    });
    

    With the function describeInTransaction being a wrapper over a transaction and the jest test.

  6. millsp commented on Apr 4, 2022

    @millsp
    ContributorAuthor

    Hey folks, I prototyped something today (with type safety!). Do you mind giving it a try and share your feedback?

    Prototype setup

    import { PrismaClient } from "@prisma/client"
    
    type CtorParams<C> = C extends new (...args: infer P) => any ? P[0] : never
    type TxClient = Parameters<Parameters<PrismaClient['$transaction']>[0]>[0]
    const ROLLBACK = { [Symbol.for('prisma.client.extension.rollback')]: true }
    
    async function $begin(client: PrismaClient) {
        let setTxClient: (txClient: TxClient) => void
        let commit: () => void
        let rollback: () => void
    
        // a promise for getting the tx inner client
        const txClient = new Promise<TxClient>((res) => {
            setTxClient = (txClient) => res(txClient)
        })
    
        // a promise for controlling the transaction
        const txPromise = new Promise((_res, _rej) => {
            commit = () => _res(undefined)
            rollback = () => _rej(ROLLBACK)
        })
    
        // opening a transaction to control externally
        const tx = client.$transaction((txClient) => {
            setTxClient(txClient as TxClient)
    
            return txPromise.catch((e) => {
                if (e === ROLLBACK) return
                throw e
            })
        })
    
        return Object.assign(await txClient, {
            $commit: async () => { commit(); await tx },
            $rollback: async () => { rollback(); await tx }
        } as TxClient & { $commit: () => Promise<void>, $rollback: () => Promise<void> })
    }
    
    // patches the prisma client with a $begin method
    function getTxClient(options?: CtorParams<typeof PrismaClient>) {
        const client = new PrismaClient(options)
        
        return Object.assign(client, {
            $begin: () => $begin(client)
        }) as PrismaClient & { $begin: () => ReturnType<typeof $begin> }
    }

    Example

    const prisma = getTxClient()
    await prisma.user.deleteMany()
    const tx = await prisma.$begin()
    
    await tx.user.create({
        data: {
            email: Date.now() + '@email.io'
        }
    })
    
    const users0 = await tx.user.findMany({})
    console.log(users0)
    
    await tx.$rollback()
    
    const users1 = await prisma.user.findMany({})
    console.log(users1)
  7. removed their assignment
    on Apr 5, 2022
  8. self-assigned this
    on Apr 5, 2022
  9. 13 remaining items

  10. PiotrJozefow commented on Mar 22, 2023

    @PiotrJozefow

    Maybe someone finds it useful but In my case I needed to wrap transaction object so I can pass it around different layers of my app and publish events after commit succeeds and I ended up using two promises to prevent premature commit:

    export abstract class RepositoryBase {
      protected abstract client: PrismaClient
    
      public async createTransaction(): Promise<PrismaTransaction> {
        let awaiter;
        const instance = await new Promise<PrismaTransaction>(resolveInstance => {
          awaiter = this.client.$transaction(async tx => {
            const transaction = new PrismaTransaction(tx)
            resolveInstance(transaction)
            await new Promise<void>(resolveCommit => transaction.setCommitter(() => resolveCommit()))
          })
        })
        instance.setAwaiter(awaiter)
        return instance
      }
      ...
     }

    And in the instance:

    type Committer = () => void
    type Awaiter = Promise<void>
    
    export class PrismaTransaction implements TransactionInterface {
      private subscribers: CommitSubscriber[] = []
      private committer?: Committer
      private awaiter?: Awaiter
    
      constructor(private readonly trx: Prisma.TransactionClient) {}
    
      public get client(): Prisma.TransactionClient {
        return this.trx
      }
    
      public async commit(): Promise<void> {
        if (!this.committer) {
          throw AppError.internalServerError('PrismaTransaction committer not set')
        }
        if (!this.awaiter) {
          throw AppError.internalServerError('PrismaTransaction awaiter not set')
        }
        this.committer()
        await Promise.all(this.subscribers.map(cb => cb()))
        await this.awaiter
      }
    
      public setCommitter(cb: Committer) {
        if (this.committer) {
          throw AppError.internalServerError('PrismaTransaction committer already set')
        }
        this.committer = cb
      }
    
      public setAwaiter(cb: Awaiter) {
        if (this.awaiter) {
          throw AppError.internalServerError('PrismaTransaction awaiter already set')
        }
        this.awaiter = cb
      }
    
      public onCommit(cb: CommitSubscriber): void {
        this.subscribers.push(cb)
      }
    }
  11. Omer-Shahar commented on May 24, 2023

    @Omer-Shahar

    @adrian-goe you could use https://nodejs.org/api/async_context.html#class-asynclocalstorage to implement your own @Transactional() decorator

    Thank you so much for the idea!
    I think I managed to create my own Transactional decorator:

    const asyncLocalStorage = new AsyncLocalStorage();
    
    type TransactionalPrisma = Omit<
      PrismaClient<
        Prisma.PrismaClientOptions,
        never,
        Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined
      >,
      "$connect" | "$disconnect" | "$on" | "$transaction" | "$use"
    >;
    
    const dbMap = new Map<string, TransactionalPrisma>();
    
    export function getDB() {
      const id = z.string().parse(asyncLocalStorage.getStore());
      const db = dbMap.get(id);
      if (!db) throw new Error("No db found");
      return db;
    }
    
    export function Transactional(target: { prototype: Object }) {
      const prototype = target.prototype;
      // for each method
      for (const propertyName of Object.getOwnPropertyNames(prototype)) {
        const descriptor = Object.getOwnPropertyDescriptor(prototype, propertyName);
        const isMethod = descriptor?.value instanceof Function;
        if (!isMethod) continue;
    
        const originalMethod = descriptor.value as { apply: Function };
        // wrap method
        descriptor.value = async function (...args: unknown[]) {
          try {
            getDB(); // check if db exists, i.e. we are in a transaction
            return await originalMethod.apply(this, args);
          } catch (e) {
            return prisma.$transaction(async (db) => {
              const id = randomUUID();
              dbMap.set(id, db);
              const result = await asyncLocalStorage.run(id, async () => {
                return await originalMethod.apply(this, args);
              });
              dbMap.delete(id);
              return result;
            });
          }
        };
    
        Object.defineProperty(prototype, propertyName, descriptor);
      }
    }

    Note that this is a first draft, so improvements and corrections are more than welcome. 😅

  12. barnc commented on Jun 16, 2023

    @barnc

    Hi @Omer-Shahar - thanks a bunch for this, looks very interesting. Do you have an example of this being used anywhere?

  13. salahmedamin commented on Jul 7, 2023

    @salahmedamin

    Any solution to this problem? I'd like to have understand how extensions can solve this:

    const result = await prisma.$transaction(async (tx) => {
      const rating = await tx.rating.upsert({
        where: {},
        create: {},
        update: {},
        include: {},
      }); //update (user|product) overall rating
      overall_rating = ratedProduct
        ? await ratingService.updateProductOverallRating({
            product: ratedProduct,
            tx,
            //passing tx as a parameter to another function has no effect and gets db data messy
          })
        : await ratingService.updateUserOverallRating({
            user: ratedUser ?? -1,
            tx,
            //passing tx as a parameter to another function has no effect and gets db data messy
          });
      return { ...rating, overall_rating };
    });
    return result;

    I'd like to be able to manually commit or rollback transactions within a given context of mine

  14. dvnkboi commented on Aug 8, 2023

    @dvnkboi

    Hi @Omer-Shahar, it would really help if you could share an example of using this on a class.

  15. dvnkboi commented on Aug 8, 2023

    @dvnkboi

    Hey, don't know if anyone will find this useful but after a bit of work, I found it hard to make @Omer-Shahar's version work, but using the same sort of idea I came up with this

    import { PrismaClient } from "@prisma/client";
    import { AsyncLocalStorage } from "async_hooks";
    import { cuid } from "../generators/id.js";
    import { Exception } from "~sdk";
    
    const asyncLocalStorage = new AsyncLocalStorage();
    
    export type TransactionalPrisma = Omit<
      PrismaClient,
      "$connect" | "$disconnect" | "$on" | "$transaction" | "$use"
    >;
    
    const dbMap = new Map<string, TransactionalPrisma>();
    
    export function getDB() {
      const id = asyncLocalStorage.getStore() as string;
      const db = dbMap.get(id);
      if (!db) throw new Error("No db found");
      return db;
    }
    
    export function Tx(prisma: PrismaClient) {
      return function (target: Object, propertyKey: string | symbol, parameterIndex: number) {
        const originalMethod = target[propertyKey as string] as { apply: Function; };
        target[propertyKey as string] = async function (...args: unknown[]) {
          try {
            const db = getDB();
            args[parameterIndex] = db;
            return await originalMethod.apply(this, args);
          } catch (e) {
            return new Promise(async (res) => {
              try {
                await prisma.$transaction(async (db) => {
                  const id = cuid();
                  dbMap.set(id, db as TransactionalPrisma);
                  const result = await asyncLocalStorage.run(id, async () => {
                    try {
                      args[parameterIndex] = db;
                      const result = await originalMethod.apply(this, args);
                      if (result instanceof Exception) {
                        res(result);
                        throw result;
                      }
                      return result;
                    }
                    catch (e) {
                      console.log(e);
                      res(new Exception(e.message, 'transactionError'));
                      throw e;
                    }
                  });
                  dbMap.delete(id);
                  return result;
                });
              }
              catch (e) {
                console.log(e);
                res(new Exception(e.message, 'transactionError'));
              }
            });
          }
        };
        return target as any;
      };
    };

    This is an example of using it:

    @Controller('/shipment')
    export class ShipmentData {
      ...
      @Get('/test/:dataId')
      async test(@Req() req, @Params('dataId') dataId, @Tx(client) tx: PrismaClient) {
        const data = await tx.dataSource.create({
          data: {
            data: {},
            description: 'test',
            name: 'test',
            teamId: 'test',
            type: 'BAR',
            id: dataId,
          }
        });
    
        const data2 = await tx.dataSource.findUnique({
          where: {
            id: dataId,
          },
        });
    
        console.log(data2);
    
        throw new Error('test');
      }
    }

    Hope this helps anyone.

  16. janpio commented on Aug 22, 2023

    @janpio
    Contributor

    Related: #12975 + #13004

  17. added
    topic: $transaction
    on Aug 22, 2023
  18. vineboneto commented on Dec 8, 2023

    @vineboneto

    I've been trying to implement a solution based on @PiotrJozefow approach for managing transactions in Prisma 5.7.0, PostgreSQL 15, and Node.js 18. However, I'm encountering an error when attempting to execute more than 15 simultaneous transactions. The error is "PrismaClientKnownRequestError: Transaction API error: Unable to start a transaction in the given time." My implementation involves sharing a transaction across multiple repositories through a context. I'm looking for insights into this issue and any recommended strategies for handling multiple simultaneous transactions in a shared context. Here is my code for reference:

    import { PrismaClient, Prisma } from "@prisma/client";
    
    const prisma = new PrismaClient();
    
    type Committer = () => void;
    type AwaiterTransaction = Promise<void>;
    
    export class PrismaTransaction {
      private committer?: Committer;
      private awaiter?: AwaiterTransaction;
    
      constructor(private readonly trx: Prisma.TransactionClient) {}
    
      public get client(): Prisma.TransactionClient {
        return this.trx;
      }
    
      public async commit(): Promise<void> {
        if (!this.committer) {
          throw Error("PrismaTransaction committer not set");
        }
        if (!this.awaiter) {
          throw Error("PrismaTransaction awaiter not set");
        }
        this.committer();
        // await Promise.all(this.subscribers.map((cb) => cb()))
        await this.awaiter;
      }
    
      public setCommitter(cb: Committer) {
        if (this.committer) {
          throw Error("PrismaTransaction committer already set");
        }
        this.committer = cb;
      }
    
      public setAwaiter(cb: AwaiterTransaction) {
        if (this.awaiter) {
          throw Error("PrismaTransaction awaiter already set");
        }
        this.awaiter = cb;
      }
    }
    
    async function createTransaction(): Promise<PrismaTransaction> {
      let awaiter: any;
      const instance = await new Promise<PrismaTransaction>((resolveInstance) => {
        awaiter = prisma.$transaction(async (tx) => {
          const transaction = new PrismaTransaction(tx);
          resolveInstance(transaction);
          await new Promise<void>((resolveCommit) =>
            transaction.setCommitter(() => resolveCommit())
          );
        });
      });
      instance.setAwaiter(awaiter);
      return instance;
    }
    
    class Context {
      private tx: PrismaTransaction | null = null;
    
      setTransaction(trx: PrismaTransaction) {
        this.tx = trx;
      }
    
      getTransaction(): PrismaTransaction {
        if (!this.tx) throw Error("Transaction not set");
        return this.tx;
      }
    }
    
    class RepositoryAccount {
      constructor(private readonly ctx: Context) {}
    
      async exec() {
        const p = this.ctx.getTransaction();
        const users = await p.client.tbl_user.create({
          data: {
            email: "[email protected]",
            username: "John Doe",
          },
        });
        console.log(users.id, "user");
      }
    }
    
    class RepositoryCategory {
      constructor(private readonly ctx: Context) {}
    
      async exec() {
        const p = this.ctx.getTransaction();
        const grupo = await p.client.tbl_category.create({
          data: {
            name: "Test",
          },
        });
        console.log(grupo.id, "create");
      }
    }
    
    class Controller {
      constructor(
        private readonly repoAccount: RepositoryAccount,
        private readonly repoCategory: RepositoryCategory
      ) {}
    
      async handle() {
        await this.repoCategory.exec();
        await this.repoAccount.exec();
      }
    }
    
    class Transaction {
      constructor(
        private readonly controller: Controller,
        private readonly ctx: Context,
        private readonly id: string
      ) {}
    
      async handle() {
        try {
          const trx = await createTransaction();
          this.ctx.setTransaction(trx);
          await this.controller.handle();
          await trx.commit();
        } catch (error) {
          throw error;
        }
      }
    }
    
    async function exec(idx: number) {
      const ctx = new Context();
      const repoAccount = new RepositoryAccount(ctx);
      const repoGrupo = new RepositoryCategory(ctx);
      const controller = new Controller(repoAccount, repoGrupo);
      const transaction = new Transaction(controller, ctx, String(idx));
      await transaction.handle();
    }
    
    const promises = Array(15)
      .fill(null)
      .map((_, idx) => exec(idx));
    
    Promise.allSettled(promises)
      .then(async () => {
        console.log("ok");
      })
      .catch((error) => {
        console.error("error:", error);
      });
  19. gyunseo commented on Mar 16, 2024

    @gyunseo

    We have an example of callback-free itx via extensions over here, feedback welcome. https://github.com/prisma/prisma-client-extensions/tree/main/callback-free-itx

    I tried to use db transaction rollback, but the code of main branch has the following problem.

    • rollback was not working, when explicitly called.
      so check out this PR if you're trying to use rollback. It worked for me.
  20. Omer-Shahar commented on May 19, 2024

    @Omer-Shahar

    Thank you so much for the idea!
    I think I managed to create my own Transactional decorator:

    Hi @barnc @dvnkboi,
    I've decided to write everything I did here in a much more organized way and add some new features.
    This is the link to the repo.
    The example here is using drizzle, but it should work with prisma with little or no modifications.
    Feel free to ask questions, add improvements and fix any bugs that you find :)

  21. sgoodrow commented on Aug 9, 2024

    @sgoodrow

    Thanks for the extension code.

    I wanted to mention another use case that might help justify adding this feature to prisma's API.

    Streaming requests are often managed with Async Generator functions. Imagine an API that performs some long-running task and streams incremental responses. If that tasks has database side-effects, it would be nice if the bolus of work could be wrapped in a transaction boundary, but I don't see a way to do this with the callback syntax since it doesn't support async generators.

    For a specific, simplified example of what a streamed response might look like, see this example: https://bsorrentino.github.io/bsorrentino/web/2024/03/05/how-to-stream-data-in-nextjs.html

    Thanks!

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

Metadata

Metadata

Assignees

Labels

kind/featuretech/typescripttopic: $transactiontopic: extend-clienttopic: interactiveTransactionstopic: rollbacktopic: transaction

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions