sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ content/ docs/ neon/ guides/ drizzle.mdx

Use the portable Drizzle Postgres client with Neon; this page configures the database and its connections. For Worker deployment, follow Add Drizzle ORM.

Configure migration generation

// drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/schema.ts",
  out: "./migrations",
  dialect: "postgresql",
});

Use a pg-core schema as shown in Drizzle Postgres; generation does not need Neon credentials.

Generate and review migrations

bunx drizzle-kit generate
git diff -- src/schema.ts migrations
git status --short
git add src/schema.ts drizzle.config.ts migrations
git diff --cached
git commit -m "Add database migration"

Run this when the schema changes, then review and commit the schema, generated SQL, and snapshots together before deploying. The optional Drizzle.Schema resource is a separate integration, not required for this workflow.

Feed migrations from the schema

// src/Db.ts
import * as Neon from "@alchemy.run/neon";
import * as Effect from "effect/Effect";

export const NeonDb = Effect.gen(function* () {
  const project = yield* Neon.Project("app-db", {
    region: "aws-us-east-1",
  });
  const branch = yield* Neon.Branch("app-branch", {
    project,
    migrations: "./migrations",
  });
  return { project, branch };
});

With Neon.providers() registered in your Stack, Neon.Branch applies the committed directory; Neon.Project accepts the same prop for its default branch. Neon migrations covers transaction and tracking behavior.

Iterate

bun alchemy deploy

Deploy the committed files; this configuration does not generate SQL during deployment. If you already use drizzle-kit migrate, keep that application workflow unless you deliberately choose Alchemy-managed migrations; see migration ownership.

Connect at runtime

import * as Cloudflare from "@alchemy.run/cloudflare";

export const Hyperdrive = Effect.gen(function* () {
  const { branch } = yield* NeonDb;
  return yield* Cloudflare.Hyperdrive.Connection("app-hyperdrive", {
    origin: branch.origin,
    dev: branch.pooledOrigin,
  });
});

Use branch.origin when Hyperdrive handles pooling, and branch.pooledOrigin for local development that bypasses Hyperdrive. Other runtimes can use connectionUri or pooledConnectionUri; Connections covers their configuration, and the Cloudflare walkthrough owns Worker bindings and queries.

Where next

Read Preview branches per PR for branch provisioning, or SQL databases for client discovery. The Project and Branch references cover resource options.