Skip to content
TypeScript
8.8 KB
Raw Blame
---
title: Shared database across stages
description: Have ephemeral PR-preview stages reference a long-lived Neon or PlanetScale database from a staging stage instead of provisioning their own — fast previews, copy-on-write branches, no extra clusters.
---

[Stages](/environments/stages) make it cheap to spin up isolated copies
of a stack — per-developer, `pr-42`, `prod`. Most resources should
be isolated. But some — a Neon Postgres project, a shared S3
bucket, a global rate limiter — are too expensive or too stateful
to re-provision per stage. PR-preview stages should *point at* the
shared instance instead.

`Resource.ref(id, { stage })` reads a deployed resource's
attributes from another stage of the same stack — typed, lazy,
resolved at plan time against the persisted state store. See
[References](/infrastructure-as-code/references) for the full reference surface
(`Output.ref`, `Output.stackRef`, `Stack.stage`).

The example we'll build: a Neon Postgres project that's owned by
`staging`, with PR-preview stages (`pr-*`) referencing it instead
of creating their own. The full file lives in
[`examples/cloudflare-neon-drizzle/src/Db.ts`](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-neon-drizzle/src/Db.ts).

## Start with the unconditional version

A Neon project plus a per-stage branch. Every stage gets its own
project — wasteful for short-lived PR previews:

```typescript
// src/Db.ts
import * as Alchemy from "alchemy";
import * as Drizzle from "@alchemy.run/drizzle";
import * as Neon from "@alchemy.run/neon";
import * as Effect from "effect/Effect";

export const NeonDb = Effect.gen(function* () {
  const schema = yield* Drizzle.Schema("app-schema", {
    schema: "./src/schema.ts",
    out: "./migrations",
  });

  const project = yield* Neon.Project("app-db", {
    region: "aws-us-east-1",
  });

  const branch = yield* Neon.Branch("app-branch", {
    project,
    migrations: schema,
  });

  return { project, branch, schema };
});
```

Each stage that runs this gets:

- its own `Neon.Project` (a whole Postgres cluster),
- its own `Neon.Branch` against that project,
- migrations re-applied from scratch.

Fine for `staging` and `prod`. Overkill for `pr-147`.

## Read the current stage

Pull the active stage from the `Stack` service so we can branch on
it. `Alchemy.Stack` is the same handle used everywhere — it gives
you `name`, `stage`, and friends:

```diff lang="typescript"
 export const NeonDb = Effect.gen(function* () {
+  const { stage } = yield* Alchemy.Stack;
+
   const schema = yield* Drizzle.Schema("app-schema", {
     schema: "./src/schema.ts",
     out: "./migrations",
   });
```

`stage` is whatever was passed to `alchemy deploy --stage <name>`.
We'll use it to route PR previews to the shared project.

## Swap in `Neon.Project.ref` for PR stages

Here's the key step. Replace the unconditional `Neon.Project(...)`
with a conditional that uses `Neon.Project.ref(...)` when the stage
is a PR preview, and creates a real project otherwise:

```diff lang="typescript"
-  const project = yield* Neon.Project("app-db", {
-    region: "aws-us-east-1",
-  });
+  const project = stage.startsWith("pr-")
+    ? yield* Neon.Project.ref("app-db", { stage: "staging" })
+    : yield* Neon.Project("app-db", {
+        region: "aws-us-east-1",
+      });
```

Three things to know about `Resource.ref`:

1. **Same logical ID.** `"app-db"` — the same logical ID the
   `staging` stage uses to create the project. The ref lookup is
   keyed by `{ stack, stage, id }`.
2. **Typed.** `project` is `Neon.Project` either way — same
   attributes, same downstream API. The branch resource doesn't
   know or care that one stage's `project` is real and another's
   is a reference.
3. **Resolved at plan time.** Alchemy reads the attributes (project
   id, host, etc.) from the staging stage's state file. If
   `staging` hasn't been deployed yet, plan fails with
   `InvalidReferenceError` — deploy `staging` first, then PR
   stages can reference it.

The signature is
`Resource.ref(id, { stage?: string, stack?: string })`. Both
options default to the current stack and stage; here we override
`stage` to point at the long-lived shared environment.

## Pass the reference downstream

Once `project` exists (real or referenced), the rest of the file
is identical. The branch creates per-stage:

```typescript
const branch = yield* Neon.Branch("app-branch", {
  project,
  migrations: schema,
});
```

Each PR stage gets its own ephemeral Neon **branch** off the
shared **project** — which is exactly the point. Branches are
copy-on-write and free; projects aren't.

## The whole picture

```typescript
// src/Db.ts
import * as Alchemy from "alchemy";
import * as Drizzle from "@alchemy.run/drizzle";
import * as Neon from "@alchemy.run/neon";
import * as Effect from "effect/Effect";

export const NeonDb = Effect.gen(function* () {
  const { stage } = yield* Alchemy.Stack;

  const schema = yield* Drizzle.Schema("app-schema", {
    schema: "./src/schema.ts",
    out: "./migrations",
  });

  const project = stage.startsWith("pr-")
    ? yield* Neon.Project.ref("app-db", { stage: "staging" })
    : yield* Neon.Project("app-db", {
        region: "aws-us-east-1",
      });

  const branch = yield* Neon.Branch("app-branch", {
    project,
    migrations: schema,
  });

  return { project, branch, schema };
});
```

## Deploy

Deploy `staging` once to materialize the shared project:

```sh
alchemy deploy --stage staging
```

Then PR stages can reference it:

```sh
alchemy deploy --stage pr-147
```

Plan reads `staging`'s state, resolves `app-db` against it, and
provisions only the per-stage `Neon.Branch`. Tearing down a PR
stage with `alchemy destroy --stage pr-147` deletes the branch but
leaves the shared project alone — Alchemy doesn't own it from this
stage's perspective.

## The same pattern with PlanetScale

`Resource.ref` isn't Neon-specific. The
[PlanetScale](/planetscale) examples use the identical shape — a
long-lived `PostgresDatabase` owned by a staging tier, referenced
from `pr-*` stages that only own the ephemeral pieces (branch,
role, Hyperdrive, Worker):

```typescript
const database = stage.startsWith("pr-")
  ? yield* Planetscale.PostgresDatabase.ref("app-db", {
      stage: `staging-${stage}`,
    })
  : yield* Planetscale.PostgresDatabase("app-db", {
      region: { slug: "us-east" },
      clusterSize: "PS_10",
    });

const branch = yield* Planetscale.PostgresBranch("app-branch", {
  database,
  migrations: schema,
});
```

One twist worth stealing: instead of pointing every PR at a single
global `staging`, the ref targets `staging-${stage}` — so `pr-42`
reads from `staging-pr-42`. Each PR gets a parallel staging stage
that owns its database, keeping PR data isolated while the
create-vs-reference split stays the same. Deploy `staging-pr-42`
before `pr-42`, same as the Neon flow. If you'd rather share one
database across all PRs, use `stage: "staging"` exactly as above.

The MySQL family works identically — `MySQLDatabase.ref`,
`MySQLBranch`, `MySQLPassword`. Full files:
[`cloudflare-planetscale-postgres-drizzle`](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-planetscale-postgres-drizzle/src/Db.ts)
and
[`cloudflare-planetscale-mysql-drizzle`](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-planetscale-mysql-drizzle/src/Db.ts).

## Cross-stack too

`Resource.ref` also takes a `stack` option for pulling a resource
out of a *different* stack:

```typescript
const project = yield* Neon.Project.ref("app-db", {
  stack: "shared-infra",
  stage: "prod",
});
```

The lookup is `{ stack, stage, id }` — change any of them and
you're pointing at a different deployed resource. For pulling a
whole stack's outputs (instead of a single resource) see the
multi-stack section of [Monorepos](/project-structure/monorepo).

## When to use which

| Goal                                        | Use                                            |
| ------------------------------------------- | ---------------------------------------------- |
| Reference a single resource, same stack, different stage | `Resource.ref(id, { stage })`            |
| Reference a single resource, different stack | `Resource.ref(id, { stack, stage })`           |
| Reference an entire stack's outputs         | `Backend.stage[name]` ([guide](/project-structure/monorepo))             |
| Reference an arbitrary deployed Output      | [`Output.ref`](/infrastructure-as-code/outputs#ref)          |

All four resolve through the same state store — they just differ
in what shape they return.

## Related

- [Resource](/infrastructure-as-code/resource#ref) — `Resource.ref` reference.
- [Stages](/environments/stages) — naming conventions for `pr-*`,
  `dev_*`, `staging`, `prod`.
- [Monorepos](/project-structure/monorepo) — whole-stack references via
  typed `Stack` handles.
- [Neon](/neon) and [PlanetScale](/planetscale) — the two database
  providers used in this guide.