prisma / prisma/orm

Configuration for disabling default transaction wrapping in prisma calls

Open
#11,634 3 comments 18 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/feature topic: transaction
Dominant language
TypeScript
Stars
47.6k
Forks
2.5k
Avg merge
21h 59m
Merged PRs (30d)
95

Description

Problem

I'm trying to setup a test strategy with Jest that runs each of the tests inside transactions and rollback all changes after the assertions. In order for this to work the strategy need to be paired with a connection_limit=1 and running tests in sequence with --runInBand.

Basically what I'm looking for is this:

beforeEach(async () => {
   prisma.$executeRaw`BEGIN;`
})

afterEach(async () => {
    prisma.$executeRaw`ROLLBACK;`
})

describe('some context', () => {
    test('doSomethingComplexThatHitsTheDatabase', async () => {
        await setupData()
        const result = await doSomethingComplexThatHitsTheDatabase()
        expect(result.foo).toBe('bar')
   })
})

This way we could setup each test with whatever data we wanted and it would rollback automatically to the default state. The good part of it is that it causes no write to the disk and executes everything in memory, making the tests crazy fast.

Prisma don't support this today because it wraps every call between a transaction by default:
image

I have similar setup with @databases but I really wish to replicate this with Prisma so I can leverage both the speed and simplicity on writing tests like this.

Suggested solution

Add a config when instantiating the prisma client that we could use to disable the behavior:

const client = new PrismaClient({
    wrapCallsInTransaction: false
})

Or something similar.

This would simply not run BEGIN and COMMIT after every prisma call.

I'm pretty sure prisma already has to do it when we use the $transaction API. I didn't find the code myself to PR it though.

Alternatives

Can't really think of anything.

Additional context

I don't think it's necessary.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with PrismaClient configuration and the transaction behavior described in the issue; review the Jest beforeEach/afterEach setup and the $transaction API. Done means a supported way to disable automatic wrapping while retaining explicit transaction support, with tests covering the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.