Documentation

Cloudflare Hyperdrive & D1 SQL

ready

Ultra-low latency database connectivity from edge Workers to PostgreSQL/MySQL via Hyperdrive and Cloudflare D1 distributed SQLite with automatic SQL migrations.

Source reference: cli/internal/serverless/cloudflare/hyperdrive.go, cli/internal/serverless/cloudflare/d1.go


The Edge Database Dilemma

Connecting to centralized relational databases (PostgreSQL, MySQL, Neon, Supabase) from edge compute runtimes normally incurs severe latency penalties due to repeated TCP handshakes and TLS negotiations across long geographical distances.

NextDeploy natively orchestrates two edge database solutions:

  1. Cloudflare Hyperdrive: Globally distributed connection pooling and query caching for existing Postgres/MySQL databases (including private databases behind Cloudflare Tunnel + Access).
  2. Cloudflare D1: Serverless, SQLite-compatible distributed SQL database with read replication and forward-only SQL migrations (migrations_dir).

1. Cloudflare Hyperdrive (Postgres / MySQL Acceleration)

Hyperdrive pools database connections across Cloudflare's global edge network, maintaining pre-warmed TCP and TLS sessions directly to your origin database.

Configuration in nextdeploy.yml

version: "1.0"
target_type: serverless

app:
  name: my-app
  domain: app.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    resources:
      hyperdrive:
        - name: prod-db
          binding: HYPERDRIVE
          origin_env: DATABASE_URL
          caching:
            max_age: 60
            stale_while_revalidate: 15

For private databases behind Cloudflare Tunnel + Access, add access_client_id_env and access_client_secret_env.

Usage in Next.js Server Components

Read the Hyperdrive binding from globalThis.__nextdeployEnv and pass env.HYPERDRIVE.connectionString to pg or Drizzle:

// app/users/page.tsx
import { Client } from 'pg';

export const dynamic = 'force-dynamic';

export default async function UsersPage() {
  const env = (globalThis as { __nextdeployEnv?: CloudflareEnv }).__nextdeployEnv;
  const client = new Client({
    connectionString: env?.HYPERDRIVE.connectionString ?? process.env.DATABASE_URL,
  });
  await client.connect();
  try {
    const { rows } = await client.query('SELECT id, name FROM users LIMIT 10');
    return (
      <ul>
        {rows.map((user) => (
          <li key={user.id}>{user.name}</li>
        ))}
      </ul>
    );
  } finally {
    await client.end();
  }
}

2. Cloudflare D1 (Native Distributed SQL)

For applications that need zero-latency edge relational storage with automated Drizzle-compatible SQL migrations:

version: "1.0"
target_type: serverless

app:
  name: my-app
  domain: app.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    resources:
      d1:
        - name: my-app-db
          binding: DB
          migrations_dir: ./drizzle
          read_replication: auto
          protect: true

Query D1 directly via globalThis.__nextdeployEnv.DB:

// app/api/todos/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const env = (globalThis as { __nextdeployEnv?: CloudflareEnv }).__nextdeployEnv;
  if (!env?.DB) {
    return NextResponse.json({ error: 'DB binding not configured' }, { status: 500 });
  }
  const { results } = await env.DB.prepare('SELECT * FROM todos').all();
  return NextResponse.json(results);
}

Related