The best way to control Claude Code, Codex and more from any computer or phone
| 1 | # @alchemy.run/planetscale |
| 2 | |
| 3 | PlanetScale for [Alchemy](https://alchemy.run). Declare serverless MySQL and Postgres databases, branches, and roles in the same TypeScript program as the code that uses them. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/planetscale @alchemy.run/cloudflare @alchemy.run/sql effect |
| 7 | ``` |
| 8 | |
| 9 | A Postgres database, a branch with migrations, and a role, reached from a Cloudflare Worker through Hyperdrive: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/db.ts |
| 13 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 14 | import * as Planetscale from "@alchemy.run/planetscale"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | |
| 17 | export const Hyperdrive = Effect.gen(function* () { |
| 18 | const database = yield* Planetscale.PostgresDatabase("Db", { |
| 19 | region: { slug: "us-east" }, |
| 20 | clusterSize: "PS_10", |
| 21 | }); |
| 22 | const branch = yield* Planetscale.PostgresBranch("Main", { |
| 23 | database, |
| 24 | migrations: "./migrations", |
| 25 | }); |
| 26 | const role = yield* Planetscale.PostgresRole("App", { |
| 27 | database, |
| 28 | branch, |
| 29 | inheritedRoles: ["postgres"], |
| 30 | }); |
| 31 | return yield* Cloudflare.Hyperdrive.Connection("Hyperdrive", { |
| 32 | origin: role.origin, |
| 33 | }); |
| 34 | }); |
| 35 | ``` |
| 36 | |
| 37 | ```typescript |
| 38 | // src/worker.ts |
| 39 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 40 | import * as SQL from "@alchemy.run/sql/Postgres"; |
| 41 | import * as Effect from "effect/Effect"; |
| 42 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 43 | import { Hyperdrive } from "./db.ts"; |
| 44 | |
| 45 | export default Cloudflare.Worker( |
| 46 | "Worker", |
| 47 | { main: import.meta.url }, |
| 48 | Effect.gen(function* () { |
| 49 | const hyperdrive = yield* Cloudflare.Hyperdrive.Connect(yield* Hyperdrive); |
| 50 | const sql = yield* SQL.Postgres({ url: hyperdrive.connectionString }); |
| 51 | |
| 52 | return { |
| 53 | fetch: Effect.gen(function* () { |
| 54 | const users = yield* sql`SELECT * FROM users`; |
| 55 | return yield* HttpServerResponse.json(users); |
| 56 | }).pipe(Effect.orDie), |
| 57 | }; |
| 58 | }).pipe(Effect.provide(Cloudflare.Hyperdrive.ConnectBinding)), |
| 59 | ); |
| 60 | ``` |
| 61 | |
| 62 | ```typescript |
| 63 | // alchemy.run.ts |
| 64 | import * as Alchemy from "alchemy"; |
| 65 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 66 | import * as Planetscale from "@alchemy.run/planetscale"; |
| 67 | import * as Effect from "effect/Effect"; |
| 68 | import * as Layer from "effect/Layer"; |
| 69 | import Worker from "./src/worker.ts"; |
| 70 | |
| 71 | export default Alchemy.Stack( |
| 72 | "MyApp", |
| 73 | { |
| 74 | providers: Layer.mergeAll(Cloudflare.providers(), Planetscale.providers()), |
| 75 | state: Cloudflare.state(), |
| 76 | }, |
| 77 | Effect.gen(function* () { |
| 78 | const worker = yield* Worker; |
| 79 | return { url: worker.url }; |
| 80 | }), |
| 81 | ); |
| 82 | ``` |
| 83 | |
| 84 | ## Learn more |
| 85 | |
| 86 | - [PlanetScale on alchemy.run](https://alchemy.run/planetscale) |
| 87 | - [Setup](https://alchemy.run/planetscale/setup) |
| 88 | - [API reference](https://alchemy.run/providers) |