Part 3: Add a Postgres Database
A managed database cluster is a PostgreSQL server DigitalOcean runs for you, with backups and failover. The App will record the path of every request in a visits table.
Declare a cluster
Section titled “Declare a cluster”import * as DigitalOcean from "@alchemy.run/digitalocean";
export const Registry = DigitalOcean.Registry("Registry", { tier: "starter" });
export const Uploads = DigitalOcean.Spaces.Bucket("Uploads", { region: "nyc3", forceDestroy: true });
export const Db = DigitalOcean.Databases.Cluster("Db", { engine: "pg", size: "db-s-1vcpu-1gb", region: "nyc3",});A new cluster takes five to ten minutes to come up, and deploy waits for it.
Write a migration
Section titled “Write a migration”Migrations are plain SQL files, applied in name order:
CREATE TABLE visits ( id serial PRIMARY KEY, path text NOT NULL, at timestamptz NOT NULL DEFAULT now());Declare the App’s database
Section titled “Declare the App’s database”import * as DigitalOcean from "@alchemy.run/digitalocean";import * as Effect from "effect/Effect";
export const Registry = DigitalOcean.Registry("Registry", { tier: "starter" });
export const Uploads = DigitalOcean.Spaces.Bucket("Uploads", { region: "nyc3", forceDestroy: true });
export const Db = DigitalOcean.Databases.Cluster("Db", { engine: "pg", size: "db-s-1vcpu-1gb", region: "nyc3",});
export const AppDb = Effect.gen(function* () { const db = yield* Db; return yield* DigitalOcean.Databases.Database("App", { clusterId: db.clusterId, migrations: "./migrations", });});Database creates a database named App in the cluster and applies every migration it hasn’t run yet, on every deploy. Alchemy records each applied file in an __alchemy_migrations table, so a later deploy runs only new files. It’s declared inside an Effect.gen because it needs the cluster’s id.
Install a driver
Section titled “Install a driver”pnpm add pgpnpm add -D @types/pgbun add pgbun add -d @types/pgnpm install pgnpm install -D @types/pgyarn add pgyarn add -D @types/pgBind the database
Section titled “Bind the database”import { Registry, Uploads } from "./resources.ts";import { AppDb, Registry, Uploads } from "./resources.ts";
export default class Api extends DigitalOcean.App<Api>()( "Api", { main: import.meta.url, region: "nyc", registry: Registry }, Effect.gen(function* () { const putObject = yield* DigitalOcean.Spaces.PutObject(Uploads); const getObject = yield* DigitalOcean.Spaces.GetObject(Uploads); const conn = yield* DigitalOcean.Databases.Connect(AppDb);Connect(AppDb) gives the App the database’s connection string as a secret, and the certificate of the CA DigitalOcean signs the cluster’s TLS certificate with. The App’s deploy now waits for the migrations.
Provide its client
Section titled “Provide its client”// src/api.ts, at the end of the class }).pipe( Effect.provide( Layer.mergeAll(DigitalOcean.Spaces.PutObjectHttp, DigitalOcean.Spaces.GetObjectHttp), Layer.mergeAll( DigitalOcean.Spaces.PutObjectHttp, DigitalOcean.Spaces.GetObjectHttp, DigitalOcean.Databases.ConnectHttp, ), ), ),) {}Run a query
Section titled “Run a query”Add a small helper inside the Effect, after conn:
const query = (sql: string, params: unknown[] = []) => Effect.gen(function* () { const uri = yield* conn.connectionString; const ca = yield* conn.caCertificate; return yield* Effect.tryPromise(async () => { const client = new pg.Client({ // `ssl` verifies the server against DigitalOcean's CA; pg ignores it // while the string carries `sslmode`. connectionString: Redacted.value(uri).replace(/[?&]sslmode=[^&]*/, ""), ssl: { ca }, }); await client.connect(); try { return (await client.query(sql, params)).rows; } finally { await client.end(); } }); });It opens a connection per query, which is enough for a small App. Import pg from "pg" and Redacted from "effect/Redacted" at the top of the file.
Record each request
Section titled “Record each request”// src/api.ts, inside fetch fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; const key = new URL(request.url, "http://api").pathname.slice(1); yield* query("INSERT INTO visits (path) VALUES ($1)", [`/${key}`]); if (request.method === "PUT") {Deploy
Section titled “Deploy”pnpm alchemy deploybun alchemy deploynpx alchemy deployyarn alchemy deployCall the App a few times, then look in the database. The cluster’s page in the control panel shows the connection details and a console.
Next, in Part 4, a nightly job writes a report of the visits.