Skip to content

Add built-in D1 databases - #8486

Merged
ry merged 1 commit into
mainfrom
ry/d1
Nov 16, 2026
Merged

ry merged 1 commit into
mainfrom
ry/d1

Conversation

@ry

@ry ry commented Nov 16, 2026

Copy link
Copy Markdown
Member

workerd can now run D1 databases itself, like KV, cron, Queues and Workflows. A bundled Worker, /workerd/d1.js, keeps each database in a SQLite-backed Durable Object. The existing D1 binding (cloudflare-internal:d1-api) reaches it over a service binding whose props name the database, so no new binding type and no C++ beyond registering the module. With --experimental, d1_databases in wrangler.json maps onto it, and databases live in .wrangler/state/workerd/d1/.

Both protocols the binding speaks are served: the query() RPC method (d1_binding_jsrpc) and POST /query and /execute. Each call runs in one transaction, so a failed batch() or exec() rolls back. There is one copy, the primary, so any session bookmark is satisfied; a call that sends one gets back a time-plus-counter bookmark that costs no write. PRAGMA miniflare_d1_export answers the database as SQL for wrangler d1 export --local, ported from Miniflare's dumpSql.ts, with golden output captured from it.

This is meant to replace Miniflare's D1 simulation; a workers-sdk PR follows that deletes about 870 lines from Miniflare and keeps existing local data. Differences from Miniflare: meta.served_by is workerd, and query() errors are named Error, not D1_ERROR (naming them doubles the prefix for callers without enhanced_error_serialization). Not included: migrations (workerd d1 migrations apply is a follow-up), D1's size limits, a cluster test.

Tests: Miniflare's D1 suites and the binding's own run against the Worker over both protocols (tests/d1:d1-test, all variants), plus d1-wrangler-test (serve and restart) and workerd-cli_test.

Adds D1 to workerd as a bundled Worker, /workerd/d1.js, alongside KV,
cron, Queues and Workflows. Bindings are the existing
cloudflare-internal:d1-api wrapped binding over a service binding to
it, with {"database": "<id>"} in its props. It serves both protocols
the binding speaks: the query() RPC method used under
d1_binding_jsrpc, and POST /query and /execute.

Each database is a SQLite-backed Durable Object, so in a cluster it
should replicate and fail over like any other object; there is no
cluster test yet. Each call runs its statements in one transaction, so
a failing batch() or exec() rolls back. There is one copy, the
primary, which satisfies any session's bookmark; a call that sends
one gets back a bookmark made of the time and a count, which needs no
write. Statements that are only comments are skipped, and SQL may end
in comments and semicolons, as Miniflare allows and workerd's SQL API
does not. HTTP answers are always an array, as Miniflare's are.
PRAGMA miniflare_d1_export answers the database as SQL, for wrangler
d1 export --local; the dump is ported from Miniflare's dumpSql.ts.

With --experimental, d1_databases in wrangler.json maps onto it. A
database is named by its preview_database_id, else database_id, else
its binding, as wrangler dev names local ones. Databases live in
.wrangler/state/workerd/d1/. Migrations are not applied.

Tests run the binding's own suites (d1-api-test-common.js) and
Miniflare's (suite.ts and jsrpc.spec.ts, ported to
d1-miniflare-test.js) against this Worker over both protocols, plus
atomicity, separate databases, blobs, sessions, comments and refused
transaction control. d1-export-test checks the export against output
captured from Miniflare and restores it into a fresh database. One
difference from Miniflare is marked DIFF and tested: query() errors
are named Error, not D1_ERROR, since callers without
enhanced_error_serialization would see the name doubled in the
message. d1-wrangler-test serves a wrangler.jsonc and restarts it.
@ry
ry requested review from a team as code owners November 16, 2026 07:06

@clanky-the-clanker clanky-the-clanker 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.

💬 Approved with comments — No correctness or security problems found. One INFO note: wrangler d1 export --local holds the whole database in memory while it builds the export.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


⚡ Performance: 💬 1 info

Export materializes the whole database in memory.

Recommendation: 💬 Optional Nits

  • [INFO] Export holds every SQL statement in memory and returns them in one response, so peak memory grows with the database size. (src/workerd/server/d1/d1.js:77)
View full review

dump() collects every statement of the export into one array, and query() or fetch() then copies it into a single response. Peak memory during wrangler d1 export --local therefore grows with the database size. This only affects the explicit local export command, not normal queries.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found. The database is chosen only from the service binding's props, which come from the operator's config. SQL comes from the binding user, as it does in D1, and the Durable Object's SQLite restrictions still apply.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

I checked d1.js against src/cloudflare/internal/d1-api.ts for both protocols: request and response shapes, failures returned as HTTP 200 with success:false, ROWS_AND_COLUMNS, and the bookmark header. In wrangler.rs, the service count matches bundled_services (with at += 2 added after workflows), the binding list is sized m + d1.len(), and check_experimental covers both serve and deploy. No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 782552e5

Comment thread src/workerd/server/d1/d1.js Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants