Skip to content
TypeScript
3.1 KB
Raw Blame
---
title: Drizzle ORM with Neon
description: Configure Neon projects, branches, committed Drizzle migrations, and direct or pooled connections.
---

Use the portable [Drizzle Postgres client](/sql/drizzle/postgres) with Neon; this page configures the database and its connections. For Worker deployment, follow [Add Drizzle ORM](/cloudflare/data/drizzle).

## Configure migration generation

```typescript
// 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](/sql/drizzle/postgres#define-the-schema); generation does not need Neon credentials.

## Generate and review migrations

```sh
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](/providers/drizzle/schema) is a separate integration, not required for this workflow.

## Feed `migrations` from the schema

```typescript
// 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](/neon/data/migrations) covers transaction and tracking behavior.

## Iterate

```sh
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](/sql/drizzle/migrations).

## Connect at runtime

```typescript
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](/neon/data/connections) covers their configuration, and the [Cloudflare walkthrough](/cloudflare/data/drizzle) owns Worker bindings and queries.

## Where next

Read [Preview branches per PR](/neon/guides/preview-branches) for branch provisioning, or [SQL databases](/sql/databases) for client discovery. The [Project](/providers/neon/project) and [Branch](/providers/neon/branch) references cover resource options.