@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 50 lines 1.85 KB
Raw
1 import * as Drizzle from "@alchemy.run/drizzle";
2 import * as GCP from "@alchemy.run/gcp";
3 import * as Alchemy from "alchemy";
4 import * as Effect from "effect/Effect";
5
6 export const database = Effect.gen(function* () {
7 // Shared-core, zonal, no backups: the cheapest Postgres Cloud SQL runs.
8 // No authorized networks — the only ways in are the Cloud SQL connector
9 // (Cloud Run's `/cloudsql` socket, IAM-gated) and the Data API that
10 // applies migrations.
11 const instance = yield* GCP.SQL.Instance("Postgres", {
12 databaseVersion: "POSTGRES_17",
13 edition: "ENTERPRISE",
14 tier: "db-f1-micro",
15 availabilityType: "ZONAL",
16 backup: { enabled: false },
17 dataApiAccess: true,
18 });
19
20 // Generated once and kept in state; the regional secret below is where
21 // the service reads it at runtime.
22 const password = yield* Alchemy.Random("AppPasswordValue", { bytes: 24 });
23 const user = yield* GCP.SQL.User("AppUser", {
24 instance: instance.instanceName,
25 password: password.text,
26 // The user owns the migrated tables, so Postgres refuses to drop it
27 // while the database still exists; deleting the instance removes it.
28 deletionPolicy: "ABANDON",
29 });
30 const passwordSecret = yield* GCP.SecretManager.LocationsSecret("AppPassword", {
31 location: instance.region,
32 });
33
34 // drizzle-kit regenerates ./migrations whenever src/schema.ts changes.
35 const schema = yield* Drizzle.Schema("Schema", {
36 schema: "./src/schema.ts",
37 out: "./migrations",
38 });
39
40 // Every deploy applies pending migrations through the Cloud SQL Data
41 // API, logged in as the app user, so the user owns the tables.
42 const db = yield* GCP.SQL.Database("App", {
43 instance: instance.instanceName,
44 migrations: schema,
45 migrationsUser: user.userName,
46 migrationsPassword: password.text,
47 });
48
49 return { instance, db, user, password, passwordSecret, schema };
50 });