Skip to content

fix(cloudflare-workers): add database override for Drizzle on CF Workers#4543

Open
melihakyol06 wants to merge 2 commits intorivet-dev:mainfrom
melihakyol06:fix/cf-workers-drizzle-db-override
Open

fix(cloudflare-workers): add database override for Drizzle on CF Workers#4543
melihakyol06 wants to merge 2 commits intorivet-dev:mainfrom
melihakyol06:fix/cf-workers-drizzle-db-override

Conversation

@melihakyol06
Copy link
Copy Markdown

@melihakyol06 melihakyol06 commented Mar 31, 2026

Description

db: db({ schema, migrations }) from rivetkit/db/drizzle fails on Cloudflare Workers with:

SqliteVfs instance not provided in context. The driver must provide a sqliteVfs instance.

Root cause: The @rivetkit/cloudflare-workers driver does not implement overrideRawDatabaseClient or overrideDrizzleDatabaseClient. The Drizzle db() provider only supports the KV-backed SQLite VFS path, which relies on a dynamic import of @rivetkit/sqlite-vfs that wrangler cannot resolve at build time. Durable Objects already have native SQLite via ctx.storage.sql, but the driver never exposes it to the db provider.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes

@rivetkit/cloudflare-workersactor-driver.ts

  • overrideRawDatabaseClient(actorId) — wraps DO native storage.sql as a RawDatabaseClient
  • overrideDrizzleDatabaseClient(actorId) — returns DO DurableObjectStorage for Drizzle

rivetkitdb/drizzle/mod.ts

  • createClient checks ctx.overrideDrizzleDatabaseClient before VFS fallback, creates drizzle-orm/durable-sqlite instance
  • Added runDOInlineMigrations for DO SQLite migrations

How Has This Been Tested?

Tested with a standalone CF Workers project using wrangler dev and wrangler deploy:

  • Drizzle queries (insert, select with .returning()) work via c.db
  • c.state alongside c.db — both persist correctly
  • Migrations run correctly — __drizzle_migrations table populated in CF dashboard
  • pnpm check-types passes for both rivetkit and @rivetkit/cloudflare-workers
  • Existing tests unaffected

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

melihakyol06 and others added 2 commits April 1, 2026 00:52
… 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>
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.

1 participant