Skip to content

From Cloudflare Workers + D1

This is the full path for a Next.js app running on Cloudflare Workers/Pages (typically via OpenNext) with a D1 database. You will move the runtime to brrrd, the database to Louhi (libSQL), and your data from D1 to Louhi. It is written as a runbook a coding agent can execute.

In practice the Cloudflare coupling is small and usually lives in one file (your database client). The steps below are the validated path used to migrate real apps onto the platform.

If the application also stores files in Cloudflare R2, treat that as a separate object migration: complete this runtime/database guide, then follow From S3, R2, or compatible storage. Do not put an R2 API token or access key into the deployed app; the destination uses a project-scoped cwp_ token through the Comwit S3 endpoint.

Do the runtime work from the Next.js guide first:

  • pnpm add @brrrd/adapter; set adapterPath: require.resolve("@brrrd/adapter") in next.config.
  • Remove the OpenNext / @opennextjs/cloudflare setup (the init call, wrangler deploy wiring, open-next.config.ts).
  • Upgrade to Next ^16.2 / React 19 if you are not there yet.
  • Apply the runtime checklist (native image dependencies, request duration, public assets, etc.).
  • Build with next build --webpack → dist/brrrd.

2. Swap the database client (D1 → libSQL)

Section titled “2. Swap the database client (D1 → libSQL)”

Find your D1 client — usually src/server/db/index.ts — and replace the Cloudflare/D1 wiring with a libSQL client. Keep the same exported names so the rest of your app doesn’t change.

Before — D1 on Cloudflare
import { drizzle } from "drizzle-orm/d1";
import { getCloudflareContext } from "@opennextjs/cloudflare";
export const getDb = async () => {
const { env } = await getCloudflareContext({ async: true });
return drizzle(env.DB, { schema });
};
After — libSQL on Louhi
import "server-only";
import { type Client as LibsqlClient } from "@libsql/client/web";
import { createClient } from "@tursodatabase/serverless/compat";
import { drizzle } from "drizzle-orm/libsql/web";
import * as schema from "./schema";
export const getDb = async () => {
const client = createClient({
url: process.env.DATABASE_URL!,
authToken: process.env.DATABASE_AUTH_TOKEN!,
});
// Create a fresh client per request: the serverless compat client serializes
// work internally, so one long-lived instance can stall unrelated requests.
return drizzle({ client: client as unknown as LibsqlClient, schema });
};
Install / remove deps
pnpm add @tursodatabase/serverless @libsql/client drizzle-orm
pnpm remove @opennextjs/cloudflare # and any wrangler-only D1 deps

Your Drizzle schema is portable as-is — only the client changes.

There are two paths. For anything beyond a few megabytes, convert the D1 export to a SQLite file locally and upload the file; Comwit creates the database from it server-side in upload time plus a short validation. Replaying the SQL dump statement by statement works for small databases but takes minutes for large ones.

Export from D1
wrangler d1 export <DB_NAME> --remote --output d1-dump.sql
Create the Louhi database from the dump
comwit databases create \
--project <projectId> \
--name app-db \
--from-dump ./d1-dump.sql \
--token-out ./app-db.token

The CLI converts the dump into a single SQLite file locally (no sqlite3 install needed; add --sqlite-out ./app.sqlite to keep it), checks integrity_check and foreign_key_check, streams the file, waits until the database is ready, and prints the database_id, database_url, and one-time token. If you already have a SQLite file, pass it with --from-file instead (single file with no -wal/-shm sidecar; use sqlite3 src.db ".backup out.sqlite" if needed). Generated columns and extension-backed virtual tables are not supported. Size limit is 2 GiB.

Import into a new Louhi database
comwit databases import-dump \
--project <projectId> \
--name app-db \
--from-dump d1-dump.sql

The command prints:

  • database — the new database id
  • url — set this as DATABASE_URL
  • token — set this as DATABASE_AUTH_TOKEN; it is shown once

import-dump is new database only. It does not import into an existing database and it does not accept a raw SQLite .db file. If you already have a local .db, export SQL first with sqlite3 source.db .dump > dump.sql.

The importer handles normal Wrangler/D1, SQLite .dump, and Turso .dump output. It also skips known fresh-database noise such as PRAGMA defer_foreign_keys and direct sqlite_sequence maintenance.

Large transaction-wrapped dumps must fit in one CLI replay request. If the CLI reports that the transaction is too large, split the export into smaller transaction-free chunks or wait for a future server-side import workflow.

If you have no D1 data to keep, create an empty database and push your final schema directly:

Create an empty database
comwit databases create --project <projectId> --name app-db
Push the schema
DATABASE_URL=... DATABASE_AUTH_TOKEN=... pnpm drizzle-kit push --force
Configure and deploy
comwit apps create --project <projectId> --name web
comwit apps env set --project <projectId> --app <appId> DATABASE_URL "$DATABASE_URL"
comwit apps env set --project <projectId> --app <appId> DATABASE_AUTH_TOKEN "$DATABASE_AUTH_TOKEN"
comwit deploy \
--project <projectId> \
--app <appId> \
--package ./dist/brrrd \
--host app.example.com
  • App renders on its hostname (test pages in a browser — isolate-only runtime errors pass the build).
  • comwit apps logs --project <projectId> --app <appId> --tail 100 shows no startup or route errors.
  • Database reads/writes work against Louhi.
  • If you moved data, spot-check row counts and a few records.

Common Cloudflare-specific gotchas, recapped

Section titled “Common Cloudflare-specific gotchas, recapped”
SymptomCauseFix
OG/icon route 500sRoute imports native image code such as sharp or another .node addonUse next/og’s adapter/runtime WASM fallback, static assets, or an external image service; do not use runtime = "edge" as a blanket fix
Import request too largeOne oversized transaction-wrapped dumpSplit into smaller transaction-free dumps or wait for server-side import
Import rejectedSQL feature or provider-specific statement does not translate to Louhi/libSQLEdit the dump at the statement index printed by the CLI, then rerun into a new database
.db rejected by import-dumpimport-dump accepts SQL dumps onlyUse comwit databases create --from-file source.db instead

Once it’s live, automate redeploys from GitHub Actions.