Repository navigation
Description
Project link
https://github.com/ankur-arch/temporal-prisma-order-saga
Language
TypeScript
Short description (max 256 chars)
An order saga in TypeScript whose Activities write to Prisma Postgres through Prisma ORM. It shows idempotent Activities backed by unique constraints, compensation that is safe to retry, and tests that prove both against a real database.
Long Description
This sample is an order fulfilment saga built with the Temporal TypeScript SDK. Each Activity stores its state in Prisma Postgres through Prisma ORM, so it deals with the question every database-backed Workflow runs into: what happens when Temporal retries an Activity whose write already committed?
The Workflow creates an order, reserves stock, charges a payment and books a shipment. If a later step fails for good, it releases the reservation and refunds the payment in reverse order, inside a non-cancellable scope, and marks the order as compensated.
What it demonstrates
- Idempotent Activities. Every write carries an idempotency key with a unique constraint. Single-table writes use one
INSERT ... ON CONFLICTstatement, and the stock reservation uses a transaction with a row lock, so a retried or duplicated attempt never writes twice. - Idempotent compensations. Refund and release are guarded updates that only match rows in the expected state, so stock is returned once even when the compensation itself is retried. Compensations are registered before their step runs and do nothing when there is nothing to undo.
- Keeping the database client out of the Workflow sandbox. The Worker creates one Prisma ORM client and passes it to the Activities. The Workflow only imports Activity types.
Tested
Eight tests run real Workflows with @temporalio/testing against a Prisma Postgres database. They cover the happy path, a retry of every step after its write committed (the Activity runs twice, the row count stays at one), a rejected shipment with full compensation, retried compensations, an out of stock order, and several attempts of the same Activity running at the same time.
Try it
You need Node.js 22 or later and the Temporal CLI. The database is a temporary Prisma Postgres instance that needs no account.
npm install
npx create-db@latest --env .env
npx prisma db migrate && npm run db:seed
temporal server start-dev # terminal 1
npm run worker # terminal 2
npm run order -- --crash-after-commit # terminal 3, every step is retried once
npm run order -- --reject-shipment # the saga compensates
npm run orders # read the rows backThe README explains the idempotency and compensation code, and which Prisma Postgres connection string to use for a long-running Worker. The sample uses the Prisma ORM 8 release candidate, with pinned versions.
Disclosure: I work at Prisma.
Author(s)
Ankur Datta / Prisma
Activity
github-actions commentedon Nov 17, 2026
ZiggyBot — automated pre-evaluation
An error occurred during automated evaluation; the community team will need to review this submission manually.
Automated AI pre-screen by ZiggyBot. Final decisions are made by the community team.