Skip to content
TypeScript
4.9 KB
Raw Blame
---
title: Drizzle ORM with PlanetScale
description: Configure PlanetScale Postgres or MySQL branches, committed Drizzle migrations, and role or password connections.
---

Use the portable [Postgres](/sql/drizzle/postgres) or [MySQL](/sql/drizzle/mysql) client with PlanetScale. This page owns database configuration; [Add Drizzle ORM](/cloudflare/data/drizzle) owns Worker deployment.

## Define the schema

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

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

Use the [Postgres schema example](/sql/drizzle/postgres#define-the-schema) with `pg-core` columns, or select `dialect: "mysql"` with the [MySQL schema](/sql/drizzle/mysql#define-the-schema).

## 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"
```

Generate when the schema changes, then review and commit the schema, SQL, and snapshots together before deploying. The optional [`Drizzle.Schema` resource](/providers/drizzle/schema) is not required for this workflow.

## Apply them on the branch

```typescript
// src/Db.ts
import * as Planetscale from "@alchemy.run/planetscale";
import * as Effect from "effect/Effect";

export const PlanetscaleDb = Effect.gen(function* () {
  const database = yield* Planetscale.PostgresDatabase("app-db", {
    region: { slug: "us-east" },
    clusterSize: "PS_10",
  });
  const branch = yield* Planetscale.PostgresBranch("app-branch", {
    database,
    migrations: "./migrations",
  });
  const role = yield* Planetscale.PostgresRole("app-role", {
    database,
    branch,
    inheritedRoles: ["postgres"],
  });
  return { database, branch, role };
});
```

Register `Planetscale.providers()` in your Stack; the branch applies committed migrations transactionally and the role supplies application credentials. [Postgres](/planetscale/data/postgres) covers branch and role options.

## Connect via `origin` / `pooledOrigin`

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

export const Hyperdrive = Effect.gen(function* () {
  const { role } = yield* PlanetscaleDb;
  return yield* Cloudflare.Hyperdrive.Connection("app-hyperdrive", {
    origin: role.origin,
    dev: role.pooledOrigin,
    caching: { disabled: true },
  });
});
```

`role.origin` uses direct port 5432, while `role.pooledOrigin` uses PSBouncer on port 6432. Other runtimes can consume the redacted `connectionUrl` or `connectionUrlPooled`; the [Cloudflare walkthrough](/cloudflare/data/drizzle) continues from this Hyperdrive resource.

## MySQL: generate and check in migrations

```diff lang="typescript"
// drizzle.config.ts
-  dialect: "postgresql",
+  dialect: "mysql",
```

For Vitess, use `mysql-core` columns and run the same generate, review, and commit commands above. Do not reuse Postgres migration SQL on a MySQL branch.

## MySQL: apply and connect

```typescript
// src/Db.ts
import * as Planetscale from "@alchemy.run/planetscale";
import * as Effect from "effect/Effect";

export const PlanetscaleDb = Effect.gen(function* () {
  const database = yield* Planetscale.MySQLDatabase("app-db", {
    region: { slug: "us-east" },
    clusterSize: "PS_10",
  });
  const branch = yield* Planetscale.MySQLBranch("app-branch", {
    database,
    isProduction: false,
    migrations: "./migrations",
  });
  const password = yield* Planetscale.MySQLPassword("app-password", {
    database,
    branch,
    role: "readwriter",
  });
  return { database, branch, password };
});
```

`MySQLPassword` provides `password.origin` for Hyperdrive, without a pooled variant. [PlanetScale migrations](/planetscale/data/migrations) covers statement splitting and MySQL's non-transactional DDL behavior.

## Deploy committed files

```sh
bun alchemy deploy
```

The branch applies pending committed files; this configuration does not generate SQL during deployment. If `drizzle-kit migrate` already owns application, keep that workflow unless you deliberately choose Alchemy-managed migrations; see [migration ownership](/sql/drizzle/migrations).

## Wire it into your runtime

Follow [Add Drizzle ORM](/cloudflare/data/drizzle) for Worker bindings, or [SQL databases](/sql/databases) to choose a client for another runtime. Existing [Postgres](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-planetscale-postgres-drizzle) and [MySQL](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-planetscale-mysql-drizzle) projects demonstrate connections and queries, but use the optional schema resource rather than this guide's generation workflow.

## Where next

Read [Preview branches per PR](/planetscale/guides/preview-branches) for branch provisioning, and [Postgres](/planetscale/data/postgres) or [MySQL](/planetscale/data/mysql) for database-specific behavior.