fix(cloudflare-workers): add database override for Drizzle on CF Workers#4543
Open
melihakyol06 wants to merge 2 commits intorivet-dev:mainfrom
Open
fix(cloudflare-workers): add database override for Drizzle on CF Workers#4543melihakyol06 wants to merge 2 commits intorivet-dev:mainfrom
melihakyol06 wants to merge 2 commits intorivet-dev:mainfrom
Conversation
… CF Workers
The Cloudflare Workers driver was missing `overrideRawDatabaseClient` and
`overrideDrizzleDatabaseClient` methods, causing `db: db({ schema, migrations })`
from `rivetkit/db/drizzle` to fail with "SqliteVfs instance not provided in context".
This happened because the Drizzle db provider only supported the KV-backed SQLite
VFS path, which relies on a dynamic import of `@rivetkit/sqlite-vfs` that cannot
be resolved by wrangler's esbuild bundler at build time.
Changes:
- Add `overrideRawDatabaseClient` to CF driver — wraps DO's native `storage.sql`
as a `RawDatabaseClient` for the raw SQL `db()` provider
- Add `overrideDrizzleDatabaseClient` to CF driver — returns DO's
`DurableObjectStorage` for the Drizzle `db()` provider
- Update Drizzle `db()` to check `overrideDrizzleDatabaseClient` before falling
back to the VFS path, creating a `drizzle-orm/durable-sqlite` instance when
available
- Add `runDOInlineMigrations` for running Drizzle migrations directly on DO
SQLite without requiring the VFS
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
db: db({ schema, migrations })fromrivetkit/db/drizzlefails on Cloudflare Workers with:Root cause: The
@rivetkit/cloudflare-workersdriver does not implementoverrideRawDatabaseClientoroverrideDrizzleDatabaseClient. The Drizzledb()provider only supports the KV-backed SQLite VFS path, which relies on a dynamic import of@rivetkit/sqlite-vfsthat wrangler cannot resolve at build time. Durable Objects already have native SQLite viactx.storage.sql, but the driver never exposes it to the db provider.Type of change
Changes
@rivetkit/cloudflare-workers—actor-driver.tsoverrideRawDatabaseClient(actorId)— wraps DO nativestorage.sqlas aRawDatabaseClientoverrideDrizzleDatabaseClient(actorId)— returns DODurableObjectStoragefor Drizzlerivetkit—db/drizzle/mod.tscreateClientchecksctx.overrideDrizzleDatabaseClientbefore VFS fallback, createsdrizzle-orm/durable-sqliteinstancerunDOInlineMigrationsfor DO SQLite migrationsHow Has This Been Tested?
Tested with a standalone CF Workers project using
wrangler devandwrangler deploy:.returning()) work viac.dbc.statealongsidec.db— both persist correctly__drizzle_migrationstable populated in CF dashboardpnpm check-typespasses for bothrivetkitand@rivetkit/cloudflare-workersChecklist