Skip to content

Add D1 batch support#6524

Open
nr1brolyfan wants to merge 1 commit into
Effect-TS:mainfrom
nr1brolyfan:feat/d1-batch
Open

Add D1 batch support#6524
nr1brolyfan wants to merge 1 commit into
Effect-TS:mainfrom
nr1brolyfan:feat/d1-batch

Conversation

@nr1brolyfan

@nr1brolyfan nr1brolyfan commented Jul 22, 2026

Copy link
Copy Markdown

Summary

  • Add D1Client.batch, backed by native D1Database.batch
  • Single D1 request for a fixed collection of statements
  • Atomic rollback when any statement fails
  • Typed heterogeneous tuple results, preserving statement order

Details

  • Reuses the prepared statement cache
  • Supports parameter binding
  • Applies query/result name transforms and withoutTransforms()
  • Applies Statement.CurrentTransformer
  • Adds sql.execute tracing with the batch operation
  • Maps native failures to SqlError
  • Empty batches return [] without calling D1
  • Keeps withTransaction behavior unchanged
  • D1-specific API rather than a generic SqlClient.batchTx

Enables a follow-up implementation of .batch() in drizzle-orm/effect-d1.

Tests

  • Ordered and typed results
  • Name transforms and transformer replacement
  • Empty batches
  • Atomic rollback with Miniflare

Related

Refs #3888 and #5594. Previous batchTx discussion: #3045.

Summary by CodeRabbit

  • New Features

    • Added support for executing multiple SQL statements as a single atomic D1 batch.
    • Batch results are returned in statement order, including support for empty batches.
    • Query and result transformations are applied consistently to batched statements.
  • Bug Fixes

    • Batched execution failures now report structured SQL errors and roll back changes to preserve data integrity.

@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Jul 22, 2026
@changeset-bot

changeset-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 52d3d3c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@effect/sql-d1 Patch
effect Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/sql-clickhouse Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

D1Client gains an atomic batch method that compiles and executes multiple statements, applies configured transforms, classifies failures, and returns ordered results. Tests cover normal execution, transformations, rollback, empty batches, and statement transformers.

Changes

D1 batch execution

Layer / File(s) Summary
Batch contract and client wiring
packages/sql/d1/src/D1Client.ts
The exported client interface adds batch; client construction wires span attributes and transform-aware batch variants.
Atomic batch execution
packages/sql/d1/src/D1Client.ts
Statements are transformed, compiled, prepared, cached, executed through db.batch, converted to SqlError on failure, and mapped to results.
Batch behavior validation and release metadata
packages/sql/d1/test/Client.test.ts, .changeset/d1-batch-statements.md
Tests cover ordered results, transforms, rollback, empty batches, and statement transformers; the changeset records the patch release.�[

Estimated code review effort: 4 (Complex) | ~45 minutes

Suggested labels: enhancement

Sequence Diagram(s)

sequenceDiagram
  participant D1Client
  participant StatementTransformer
  participant makeBatch
  participant D1Database
  D1Client->>makeBatch: batch(statements)
  makeBatch->>StatementTransformer: compile statements
  StatementTransformer-->>makeBatch: compiled SQL
  makeBatch->>D1Database: prepare and execute db.batch
  D1Database-->>makeBatch: ordered results or error
  makeBatch-->>D1Client: transformed results or SqlError
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding batch support to the D1 client.

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

@coderabbitai coderabbitai Bot added the enhancement New feature or request label Jul 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
.changeset/d1-batch-statements.md (1)

1-6: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Use minor for this changeset

D1Client.batch adds a new public API, so @effect/sql-d1 should be bumped as minor, not patch.

📝 Proposed fix
---
-"`@effect/sql-d1`": patch
+"`@effect/sql-d1`": minor
---
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.changeset/d1-batch-statements.md around lines 1 - 6, Update the changeset
frontmatter for `@effect/sql-d1` from patch to minor to reflect the new public
D1Client.batch API; leave the changeset description unchanged.

Source: Coding guidelines

🧹 Nitpick comments (1)
packages/sql/d1/src/D1Client.ts (1)

62-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing @category tag on new public batch JSDoc.

The new batch JSDoc includes @since 4.0.0 but no @category tag, which the repo's JSDoc conventions expect for public API members.

📝 Proposed fix
    * `@since` 4.0.0
+   * `@category` combinators
    */

As per coding guidelines, **/*.{ts,tsx,md,mdx}: "Follow public JSDoc @category conventions from .patterns/jsdoc.md."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sql/d1/src/D1Client.ts` around lines 62 - 85, Update the public
batch member JSDoc in D1Client to include the repository-standard `@category` tag,
while preserving the existing description and `@since` 4.0.0 annotation.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In @.changeset/d1-batch-statements.md:
- Around line 1-6: Update the changeset frontmatter for `@effect/sql-d1` from
patch to minor to reflect the new public D1Client.batch API; leave the changeset
description unchanged.

---

Nitpick comments:
In `@packages/sql/d1/src/D1Client.ts`:
- Around line 62-85: Update the public batch member JSDoc in D1Client to include
the repository-standard `@category` tag, while preserving the existing description
and `@since` 4.0.0 annotation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1f6b373d-9650-4c24-9846-39ec8f80b1a4

📥 Commits

Reviewing files that changed from the base of the PR and between b35ed29 and 52d3d3c.

📒 Files selected for processing (3)
  • .changeset/d1-batch-statements.md
  • packages/sql/d1/src/D1Client.ts
  • packages/sql/d1/test/Client.test.ts

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

Labels

enhancement New feature or request

Projects

Status: Discussion Ongoing

Development

Successfully merging this pull request may close these issues.

1 participant