Skip to main content
The PlanetScale provider enables you to create and manage MySQL-compatible serverless databases with Git-like branching, zero-downtime migrations, and autoscaling.

Installation

npm install alchemy

Credentials

Create a service token at app.planetscale.com and set environment variables:
export PLANETSCALE_ORG="your-org"
export PLANETSCALE_SERVICE_TOKEN_ID="your-token-id"
export PLANETSCALE_SERVICE_TOKEN="your-token"

Available resources

  • Database - PlanetScale MySQL database
  • Branch - Database branch for development
  • Password - Database credentials

Example usage

import alchemy from "alchemy";
import { Database, Branch, Password } from "alchemy/planetscale";

const app = await alchemy("planetscale-app");

// Create a PlanetScale database
const database = await Database("db", {
  name: "my-database",
  region: "us-east",
});

// Create a development branch
const devBranch = await Branch("dev", {
  database,
  name: "development",
  parentBranch: "main",
});

// Create credentials
const password = await Password("credentials", {
  database,
  branch: devBranch,
  name: "app-credentials",
});

console.log(`Database URL: ${password.connectionString}`);

await app.finalize();

Database branching workflow

PlanetScale’s branching model enables safe schema changes:
// Production database
const prod = await Database("prod-db", {
  name: "production",
});

// Create a branch for schema changes
const schemaBranch = await Branch("schema-updates", {
  database: prod,
  name: "add-user-table",
  parentBranch: "main",
});

// After testing, merge via PlanetScale UI or API

Use with Cloudflare Workers

import { Database, Password } from "alchemy/planetscale";
import { Worker } from "alchemy/cloudflare";

const database = await Database("db", { name: "my-db" });
const password = await Password("creds", {
  database,
  branch: "main",
});

const worker = await Worker("api", {
  entrypoint: "./src/index.ts",
  bindings: {
    DATABASE_URL: password.connectionString,
  },
});

Next steps

PlanetScale API

Complete API reference

Hyperdrive

Connection pooling for PlanetScale

Build docs developers (and LLMs) love