Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

docs: add transaction API related spec, plan and ADR - #345

Merged
aqrln merged 1 commit into
mainfrom
transaction-api-spec
Apr 22, 2026
Merged

aqrln merged 1 commit into
mainfrom
transaction-api-spec

Conversation

@aqrln

@aqrln aqrln commented Apr 15, 2026 •

Copy link
Copy Markdown
Member

PR Stack: Transaction API

PR Description Base
#345 (this) 📄 Spec, plan, and ADR main
#346 ⚙️ Core transaction runtime + db.transaction() + tx.sql #345
#347 🔗 Wire tx.orm into transactions #346

Part of https://linear.app/prisma-company/issue/TML-1912/orm-transaction-support


Summary

Adds the foundational design documents for the ORM Client Transaction API (db.transaction(callback)):

  • Spec — full feature specification for db.transaction() on PostgresClient
  • Plan — 4-milestone delivery plan with acceptance criteria traceability matrix
  • ADR 187 — architecture decision on how transaction-scoped streams handle post-commit consumption

Spec highlights

The API follows a callback-scoped pattern — no dangling transactions, guaranteed cleanup:

await db.transaction(async (tx) => {
  await tx.orm.account.where({ id: fromId }).update({ balance: fromBalance - amount });
  const plan = tx.sql.from(tables.auditLog).insert({ action: 'transfer', amount }).build();
  await tx.execute(plan);
});

The transaction context exposes three surfaces:

  • tx.orm — full ORM collection API bound to the transaction connection
  • tx.sql — typed SQL builder (Db<TContract>) bound to the same connection
  • tx.execute(plan) — execute a built plan directly

Explicit non-goals: configurable isolation levels, savepoints/nesting, automatic retry, interactive begin/commit/rollback handle, timeout, MongoDB support.

Plan

Milestone Scope Status
M1 — Transaction runtime plumbing withTransaction helper, db.transaction(), tx.sql, unit + e2e tests ✅ Done
M2 — ORM integration tx.orm wiring, withMutationScope fallback, nested-mutation e2e tests ✅ Done
M3 — Type safety & edge cases Type-level tests, PromiseLike drain, post-commit invalidation, lazy connect ☐ Pending
M4 — Close-out Acceptance criteria verification, doc finalization, project cleanup ☐ Pending

ADR 187: Transaction-scoped streams use invalidation, not buffering

Decision: When a transaction ends, the scoped RuntimeQueryable sets an invalidated flag. Any AsyncIterableResult consumed after that point throws a clear error rather than hitting a released connection.

Rejected alternatives: eager buffering (silently changes semantics), type-level prevention (breaks inference), pre-commit tracking (rejects valid patterns), do nothing (confusing errors).

The ADR includes a wire-protocol safety analysis confirming that unconsumed result streams cannot hide database errors or cause an incorrect COMMIT in Postgres.

Files

  • projects/orm-client-transaction-api/spec.md (new)
  • projects/orm-client-transaction-api/plan.md (new)
  • docs/architecture docs/adrs/ADR 187 - Transaction-scoped streams use invalidation not buffering.md (new)

Summary by CodeRabbit

  • Documentation
    • Added guidance on transaction-scoped streaming behavior, including error handling for results accessed outside the transaction scope and best practices for consuming results within transactions.

@coderabbitai

coderabbitai Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A new architectural decision record (ADR 187) has been added documenting transaction-scoped streaming behavior. The design uses invalidation flags and runtime error messages to prevent consuming lazy AsyncIterableResult objects after transaction completion, rather than employing buffering or type-level restrictions.

Changes

Cohort / File(s) Summary
Transaction-Scoped Streaming ADR
docs/architecture/adrs/ADR 187 - Transaction-scoped streams use invalidation not buffering.md
New architectural decision document detailing transactional streaming strategy using invalidation-based error handling, with analysis of rejected alternatives and error scenario safeguards.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A stream within a transaction's care,
Once the dance is done, beware!
Invalidate with grace and flair,
Clear errors guide the developer's prayer,
No buffering burden to bear!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title partially relates to the changeset. While it mentions 'transaction API related spec, plan and ADR', the raw summary and PR objectives detail that the changeset includes 3 files: spec.md, plan.md, and ADR 187. The title is somewhat accurate but overly broad in phrasing—it doesn't clearly distinguish which specific ADR is being added or the primary architectural decision being documented.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch transaction-api-spec

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Apr 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-runtime@345

@prisma-next/family-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/family-mongo@345

@prisma-next/sql-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-runtime@345

@prisma-next/family-sql

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/family-sql@345

@prisma-next/middleware-telemetry

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/middleware-telemetry@345

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-paradedb@345

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-pgvector@345

@prisma-next/postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/postgres@345

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-orm-client@345

@prisma-next/sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sqlite@345

@prisma-next/target-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-mongo@345

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-mongo@345

@prisma-next/driver-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-mongo@345

@prisma-next/contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract@345

@prisma-next/utils

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/utils@345

@prisma-next/config

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/config@345

@prisma-next/errors

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/errors@345

@prisma-next/framework-components

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/framework-components@345

@prisma-next/operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/operations@345

@prisma-next/contract-authoring

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract-authoring@345

@prisma-next/ids

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/ids@345

@prisma-next/psl-parser

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-parser@345

@prisma-next/psl-printer

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-printer@345

@prisma-next/cli

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/cli@345

@prisma-next/emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/emitter@345

@prisma-next/migration-tools

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/migration-tools@345

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/vite-plugin-contract-emit@345

@prisma-next/runtime-executor

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/runtime-executor@345

@prisma-next/mongo-codec

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-codec@345

@prisma-next/mongo-contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract@345

@prisma-next/mongo-value

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-value@345

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract-psl@345

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-contract-ts@345

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-emitter@345

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-schema-ir@345

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-query-ast@345

@prisma-next/mongo-orm

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-orm@345

@prisma-next/mongo-pipeline-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-pipeline-builder@345

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-lowering@345

@prisma-next/mongo-wire

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-wire@345

@prisma-next/sql-contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract@345

@prisma-next/sql-errors

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-errors@345

@prisma-next/sql-operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-operations@345

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-schema-ir@345

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-psl@345

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-ts@345

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-emitter@345

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-lane-query-builder@345

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-relational-core@345

@prisma-next/sql-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-builder@345

@prisma-next/target-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-postgres@345

@prisma-next/target-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-sqlite@345

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-postgres@345

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-sqlite@345

@prisma-next/driver-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-postgres@345

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-sqlite@345

commit: bf9f7c0

@aqrln
aqrln marked this pull request as ready for review April 15, 2026 20:03

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/architecture` docs/adrs/ADR 187 - Transaction-scoped streams use
invalidation not buffering.md:
- Around line 50-58: Trim the implementation-specific driver internals from "ADR
187 - Transaction-scoped streams use invalidation not buffering": remove
sentences that describe low-level library behavior (e.g., the node-postgres
socket handling, tokio-postgres futures/polling model, and exact cursor
execution mechanics) and replace them with a concise decision-level statement
that the ADR assumes drivers will eagerly surface server errors and that cursor
semantics are a separate concern; move the removed details into a linked
investigation or subsystem doc (e.g., "driver-internals" notes) and add a brief
pointer/link from the ADR to that doc so readers can follow up for
implementation specifics.
- Around line 19-49: Add a single framing sentence immediately before the
"Alternatives considered" list that states the explicit comparison axis (the
invariant/trade-off) being evaluated — e.g., whether each option favors
preserving lazy/streaming semantics inside transactions versus enforcing eager
safety and runtime/type complexity — so readers understand that all alternatives
are being compared on the same knob; place this sentence above the "Eager
buffering inside transactions" subsection and reference the options named there
("Eager buffering inside transactions", "Type-level prevention", "Runtime error
before commit (tracking approach)", "Do nothing (document the pitfall)") to make
clear which approaches are being evaluated against that axis.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: ffda6f75-9532-43db-9bd2-d87b0a8762b1

📥 Commits

Reviewing files that changed from the base of the PR and between 69226a5 and bf9f7c0.

⛔ Files ignored due to path filters (2)
  • projects/orm-client-transaction-api/plan.md is excluded by !projects/**
  • projects/orm-client-transaction-api/spec.md is excluded by !projects/**
📒 Files selected for processing (1)
  • docs/architecture docs/adrs/ADR 187 - Transaction-scoped streams use invalidation not buffering.md

Comment on lines +19 to +49
## Alternatives considered

### Eager buffering inside transactions

`tx.execute` would internally drain all rows to an array, then wrap them in a pre-materialized `AsyncIterableResult`. Same return type, no leak possible.

**Rejected because:** it silently changes `execute()` semantics inside transactions (always eager) vs outside (lazy/streaming). This is surprising, increases memory pressure, and creates a behavioral difference that is invisible at the type level. Streaming inside transactions is not inherently wrong — the user may want to process a large result set row-by-row within the transaction without materializing all rows in memory.

### Type-level prevention

Constrain the callback's return type to exclude `AsyncIterableResult`:

```typescript
type NoAsyncIterable<T> = T extends AsyncIterable<unknown> ? never : T;
transaction<R>(fn: (tx: TxCtx) => PromiseLike<NoAsyncIterable<R>>): Promise<R>;
```

**Rejected because:** conditional types on generic return types interact poorly with inference — `R` may resolve to `never` instead of producing a useful error. Cannot catch `AsyncIterableResult` nested inside returned objects. Adds type complexity without runtime safety.

### Runtime error before commit (tracking approach)

Track every `AsyncIterableResult` created by the transaction. Before commit, check if any are unconsumed and throw.

**Rejected because:** it is an error to have an unconsumed stream only if the user tries to read from it after the transaction — an unconsumed stream that is simply discarded is harmless. Throwing before commit would reject valid patterns where the user intentionally ignores a result.

### Do nothing (document the pitfall)

The direct return case (`return tx.execute(plan)`) is already safe because `PromiseLike` causes auto-drain. Only the "wrap in object" case is hazardous.

**Rejected because:** `return { users: tx.execute(plan1), posts: tx.execute(plan2) }` is a natural and common pattern. Relying on documentation for a failure mode that produces confusing errors (connection gone, recycled connection) is insufficient.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Make the alternatives comparison axis explicit.

This section compares options, but it does not explicitly state the shared axis (invariant/trade-off) up front. Add one short framing sentence before the alternatives list.

Suggested doc tweak
 ## Alternatives considered
 
+Compared on a shared axis: semantic consistency of `execute()`, runtime safety, memory profile, and developer ergonomics.
+
 ### Eager buffering inside transactions

As per coding guidelines: "Research comparisons must explicitly state the shared axis (knob/invariant/trade-off) being compared."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/architecture` docs/adrs/ADR 187 - Transaction-scoped streams use
invalidation not buffering.md around lines 19 - 49, Add a single framing
sentence immediately before the "Alternatives considered" list that states the
explicit comparison axis (the invariant/trade-off) being evaluated — e.g.,
whether each option favors preserving lazy/streaming semantics inside
transactions versus enforcing eager safety and runtime/type complexity — so
readers understand that all alternatives are being compared on the same knob;
place this sentence above the "Eager buffering inside transactions" subsection
and reference the options named there ("Eager buffering inside transactions",
"Type-level prevention", "Runtime error before commit (tracking approach)", "Do
nothing (document the pitfall)") to make clear which approaches are being
evaluated against that axis.

Comment on lines +50 to +58
## Why this is safe: wire protocol analysis

A separate concern was whether unconsumed result streams could hide database errors (e.g., constraint violations) within a transaction, allowing a COMMIT that should have been a ROLLBACK. Investigation confirmed this is not an issue:

- **Not a wire protocol issue.** The Postgres wire protocol is push-oriented — the server sends `ErrorResponse` immediately. The delayed error surfacing observed in the Rust tokio-postgres driver was an architectural issue specific to that library's futures/polling model, not a protocol limitation.
- **node-postgres reads eagerly.** The `pg` library attaches `stream.on('data', ...)` to the socket. The event loop drains all server responses regardless of whether user code has consumed the result.
- **DML executes fully.** INSERT/UPDATE/DELETE (with or without RETURNING) executes atomically — cursors (DECLARE CURSOR) are SELECT/VALUES-only. Mutation errors are always surfaced.
- **Failed transaction state is server-side.** If any statement errors, the server marks the transaction as aborted. A subsequent COMMIT returns the command tag ROLLBACK regardless of whether the client read the original error.
- **Cursors defer execution, not errors.** With cursors, unfetched SELECT rows are genuinely never evaluated by the server — this is not error suppression but incomplete execution. For pure reads, this is harmless. For SELECT FOR UPDATE, locks are acquired per-FETCH, but this is only meaningful if the user reads the results to act on them within the same transaction — partial consumption of a FOR UPDATE query is already an application logic bug independent of the streaming API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Trim implementation-specific driver internals from ADR scope.

This section includes low-level library behavior details (e.g., exact pg socket handling) that read like implementation notes. Keep ADR text at decision/constraint level and move deep driver mechanics to subsystem docs or linked investigation notes.

Based on learnings: "ADRs in prisma/prisma-next should document architectural design decisions and the key constraints/assumptions only. Do not include implementation algorithms...".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/architecture` docs/adrs/ADR 187 - Transaction-scoped streams use
invalidation not buffering.md around lines 50 - 58, Trim the
implementation-specific driver internals from "ADR 187 - Transaction-scoped
streams use invalidation not buffering": remove sentences that describe
low-level library behavior (e.g., the node-postgres socket handling,
tokio-postgres futures/polling model, and exact cursor execution mechanics) and
replace them with a concise decision-level statement that the ADR assumes
drivers will eagerly surface server errors and that cursor semantics are a
separate concern; move the removed details into a linked investigation or
subsystem doc (e.g., "driver-internals" notes) and add a brief pointer/link from
the ADR to that doc so readers can follow up for implementation specifics.

@aqrln
aqrln enabled auto-merge (squash) April 22, 2026 10:17
@aqrln
aqrln disabled auto-merge April 22, 2026 10:17
@aqrln
aqrln merged commit f1f0f64 into main Apr 22, 2026
15 checks passed
@aqrln
aqrln deleted the transaction-api-spec branch April 22, 2026 10:17
aqrln added a commit that referenced this pull request Apr 23, 2026
## PR Stack: Transaction API

| PR | Description | Base |
|---|---|---|
| #345 | 📄 Spec, plan, and ADR | `main` |
| **#346** (this) | ⚙️ Core transaction runtime + `db.transaction()` +
`tx.sql` | `#345` |
| #347 | 🔗 Wire `tx.orm` into transactions | `#346` |

Refs:
https://linear.app/prisma-company/issue/TML-1912/orm-transaction-support
Closes:
https://linear.app/prisma-company/issue/TML-2161/orm-sql-dsl-transaction-interop

---

## Summary

Implements **Milestone 1** of the transaction API: the core
`withTransaction` helper in `sql-runtime` (target-agnostic) and the
`db.transaction()` surface on `PostgresClient` with `tx.sql` support.

## Implementation

### `withTransaction(runtime, fn)` — `@prisma-next/sql-runtime`

A target-agnostic helper that manages the full transaction lifecycle:

1. Acquires a connection from the pool → starts a transaction
2. Runs the user callback with a guarded `TransactionContext`
3. On success: sets `invalidated = true` → commits → releases connection
4. On error: sets `invalidated = true` → rolls back → releases
connection → re-throws

Safety features:
- **Pre-execution guard** — calling `tx.execute()` after the transaction
ends throws `RUNTIME.TRANSACTION_CLOSED`
- **Iteration guard** — a guarded `AsyncGenerator` wraps every result;
consuming rows post-commit throws the same error per-yield
- **Rollback failure wrapping** — if rollback itself fails, produces
`RUNTIME.TRANSACTION_ROLLBACK_FAILED` with the original error as
`.cause`

### `PostgresClient.transaction()` + `PostgresTransactionContext`

```ts
export interface PostgresTransactionContext<TContract extends Contract<SqlStorage>>
  extends TransactionContext {
  readonly sql: Db<TContract>;
}
```

`db.transaction(fn)` delegates to `withTransaction`, attaching a fresh
`Db<TContract>` SQL builder as `tx.sql`. Lazy-initializes the runtime on
first call (same as `connect()` / `runtime()`).

### New public exports from `@prisma-next/sql-runtime`

- `TransactionContext` (type)
- `RuntimeConnection`, `RuntimeTransaction`, `RuntimeQueryable` (types —
previously internal)
- `withTransaction` (function)

## Tests

### Unit tests — `withTransaction` (12 tests)

| Test | Covers |
|---|---|
| commits on successful callback and returns the result | Happy path |
| rolls back on callback error and re-throws | Error path |
| releases connection after commit | Cleanup on success |
| releases connection after rollback | Cleanup on failure |
| propagates commit failure | COMMIT throws |
| forwards the callback return value | Complex return type |
| executes queries against the transaction | Routing to transaction
scope |
| throws on execute after commit (invalidation) | Post-commit guard |
| throws on iteration of escaped AsyncIterableResult after commit |
Iteration guard |
| sets invalidated flag after commit | Flag transition |
| wraps original error when rollback fails | Double-fault handling |
| sets invalidated flag after rollback | Flag on error path |
| releases connection independently across sequential transactions | 3
sequential txns |

### Unit tests — Postgres (3 tests)

- `transaction()` delegates to `withTransaction` with the lazy runtime
- `transaction()` provides `sql` on the transaction context
- `transaction()` lazily creates runtime before `connect()`

### Type-level tests (3 tests)

- Transaction context does not expose a `transaction` method (no
nesting)
- `db.transaction()` infers callback return type correctly
- `tx.sql` has the same type as `db.sql`

### E2E tests against real Postgres (5 tests)

- Commits both writes atomically (two INSERTs visible after commit)
- Rolls back all writes on error (0 rows after throw)
- Forwards callback return value after commit
- Collects returned stream before commit (`PromiseLike` auto-drain)
- Rejects escaped `AsyncIterableResult` consumed after commit

## Key design decisions

- **Callback-scoped (RAII-style)** — guaranteed cleanup, no dangling
transactions
- **Invalidation over buffering** — consistent lazy `execute()`
semantics everywhere (ADR 187)
- **Structured error codes** — `RUNTIME.TRANSACTION_CLOSED`,
`RUNTIME.TRANSACTION_ROLLBACK_FAILED`
- **Layered composition** — core helper in `sql-runtime`, adapter adds
`tx.sql` surface
- **No nested transactions by type design** —
`PostgresTransactionContext` omits `transaction`, enforced at compile
time
- **`PromiseLike<R>` callback return** — auto-drains
`AsyncIterableResult` when returned directly

## Files changed

- `packages/2-sql/5-runtime/src/sql-runtime.ts` — `withTransaction`,
`TransactionContext`
- `packages/2-sql/5-runtime/src/exports/index.ts` — new exports
- `packages/2-sql/5-runtime/test/sql-runtime.test.ts` — 12 unit tests
- `packages/3-extensions/postgres/src/runtime/postgres.ts` —
`db.transaction()`, `PostgresTransactionContext`
- `packages/3-extensions/postgres/test/postgres.test.ts` — 3 unit tests
- `packages/3-extensions/postgres/test/transaction.types.test-d.ts` — 3
type tests (new)
- `test/e2e/framework/test/transaction.test.ts` — 5 e2e tests (new)
- `projects/orm-client-transaction-api/{plan,spec}.md` — mark M1 tasks
done


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added high-level transaction helper for managed callbacks and atomic
multi-operation workflows.
* Transactions expose an invalidated flag to prevent use of results
after completion.
* Postgres client gains a transaction method providing a
transaction-scoped DB context.
* Connection lifecycle: added destroy() for safer teardown and improved
driver/connection cleanup semantics.

* **Tests**
* Added unit, type-level, and E2E tests covering commit/rollback, error
wrapping, invalidation, and resource lifecycle.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
aqrln added a commit that referenced this pull request Apr 24, 2026
## PR Stack: Transaction API

| PR | Description | Base |
|---|---|---|
| #345 | 📄 Spec, plan, and ADR | `main` |
| #346 | ⚙️ Core transaction runtime + `db.transaction()` + `tx.sql` |
`#345` |
| **#347** (this) | 🔗 Wire `tx.orm` into transactions | `#346` |

Closes
https://linear.app/prisma-company/issue/TML-1912/orm-transaction-support

---

## Summary

Implements **Milestone 2** of the transaction API: wiring `tx.orm` so
that ORM collections used inside a `db.transaction()` callback execute
against the transaction connection. Also adds relations to the e2e
fixture contract to enable nested-mutation integration tests.

## Implementation

The production change is **surgical** — ~12 lines in a single file.

### `PostgresTransactionContext` gains `orm`

```ts
export interface PostgresTransactionContext<TContract extends Contract<SqlStorage>>
  extends TransactionContext {
  readonly sql: Db<TContract>;
  readonly orm: OrmClient<TContract>;  // ← new
}
```

### Transaction-scoped ORM client construction

Inside `transaction()`, a new `ormBuilder()` creates an ORM client bound
to the transaction's `execute`:

```ts
const txOrm: OrmClient<TContract> = ormBuilder({
  runtime: {
    execute(plan) {
      return txCtx.execute(plan);
    },
  },
  context,
});
```

**Key design decision**: The `runtime` object passed to `ormBuilder`
exposes **only** `execute()` — it deliberately omits `connection()` and
`transaction()`. This ensures `withMutationScope` in the mutation
executor naturally falls through to direct scope reuse instead of
attempting a nested transaction. **Zero changes to the mutation executor
were needed.**

## Tests

### Unit tests (4 new)

| Test | Location | Covers |
|---|---|---|
| `transaction()` provides `orm` on the tx context | `postgres.test.ts`
| `tx.orm` is the transaction-scoped proxy |
| `tx.orm` has the same type as `db.orm` | `transaction.types.test-d.ts`
| Compile-time type equivalence |
| `executeNestedCreateMutation()` reuses scope when runtime lacks
`transaction` | `mutation-executor.test.ts` | Fast-path: direct
`execute` on the transaction runtime |
| `withMutationScope` reuses runtime when no `transaction` or
`connection` exists | `mutation-executor.test.ts` | Scope reuse without
nested transaction |

### E2E integration tests (5 new — real PGlite database)

| Test | Covers |
|---|---|
| ORM create with nested relation mutation commits atomically |
`tx.orm.Post.create()` with nested `author: (r) => r.create(...)` — both
rows exist |
| ORM create with nested relation mutation rolls back on throw | Same
nested create + throw — neither row exists |
| ORM write then read within the same transaction |
Read-your-own-writes: `create()` then `where().first()` sees the row |
| Multiple ORM operations in a transaction are atomic | Two posts + user
all commit together |
| Multiple ORM operations roll back together on throw | Same operations
+ throw — nothing persists |

## E2E fixture contract changes

The fixture contract gained **relations** across all three models to
enable nested-mutation tests:

| Model | Relation | Type | Join |
|---|---|---|---|
| `User` | `posts` | `1:N` → `Post` | `User.id` → `Post.userId` |
| `Post` | `author` | `N:1` → `User` | `Post.userId` → `User.id` |
| `Post` | `comments` | `1:N` → `Comment` | `Post.id` → `Comment.postId`
|
| `Comment` | `post` | `N:1` → `Post` | `Comment.postId` → `Post.id` |

The contract definition was refactored to compose models with
`rel.belongsTo()` / `rel.hasMany()` before passing to `defineContract`,
using lazy refs (`() => Comment`) for circular relations.

## Design patterns

- **Capability elision for scope reuse** — the transaction-scoped
runtime is a minimal `{ execute }` object; omitting `transaction()` /
`connection()` makes `withMutationScope` fall through naturally
- **Interface segregation** — `RuntimeQueryable` passed to `ormBuilder`
is a narrowed shape, preventing accidental nested-transaction creation
- **Warm-up pattern for PGlite** — e2e helper calls
`db.orm.User.first()` before entering transactions to force contract
verification (which needs its own connection) outside the transaction,
avoiding deadlock on single-connection PGlite

## Files changed

- `packages/3-extensions/postgres/src/runtime/postgres.ts` — add
`tx.orm` construction
- `packages/3-extensions/postgres/test/postgres.test.ts` — 1 unit test
- `packages/3-extensions/postgres/test/transaction.types.test-d.ts` — 1
type test
- `packages/3-extensions/sql-orm-client/test/mutation-executor.test.ts`
— 2 unit tests
- `test/e2e/framework/test/transaction-orm.test.ts` — 5 e2e tests (new)
- `test/e2e/framework/test/fixtures/contract.ts` — add relations to
fixture
- `test/e2e/framework/test/fixtures/generated/contract.{d.ts,json}` —
regenerated
- `test/e2e/framework/package.json` + `pnpm-lock.yaml` — add
`@prisma-next/postgres` dev dep
- `projects/orm-client-transaction-api/{plan,spec}.md` — mark M2 tasks
done


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* ORM client is now available inside transaction contexts, allowing ORM
calls to participate in the same transactions as SQL.

* **Tests**
* Added e2e transaction integration tests covering atomic commits,
rollbacks, nested relation mutations, and read-your-own-writes
semantics.
* Added type-safety checks and unit tests for mutation execution scope
and transaction-scoped ORM behavior.

* **Chores**
* Test framework dependency updated to include the Postgres extension;
test fixtures reorganized for relation modeling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant