Skip to content

Latest commit

 

History

History
 
 

retry-transactions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Prisma Client Extension - Retry Transactions

This example shows how to use a Prisma Client extension to automatically retry transactions that fail due to a write conflict / deadlock timeout. Failed transactions will be retried with exponential backoff and jitter to spread out the database load under heavy traffic contention.

Caveats

NOTE: This extension overwrites the $transaction method on a Prisma Client instance, and relies on this method being defined on a generated Prisma Client. It may not currently have correct type definitions when packaged and distributed as a reusable extension.

This extension is provided as an example only. It is not intended to be used in production environments.

Please read the documentation on client extensions for more information.

How to use

Prerequisites

1. Download example & install dependencies

Clone this repository:

git clone [email protected]:sbking/prisma-client-extensions.git

Create a .env file and install dependencies:

cd retry-transactions
cp .env.example .env
npm install

2. Start the database

Run the following command to start a new Postgres database in a Docker container:

docker compose up -d

3. Run migrations

Run this command to apply migrations to the database:

npx prisma migrate deploy

4. Seed the database

Run the following command to add seed data to the database:

npx prisma db seed

5. Run the dev script

To run the script.ts file, run the following command:

npm run dev