@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 51 lines 1.95 KB
Raw
1 import * as Presign from "@distilled.cloud/aws/Presign";
2 import * as PgClient from "@effect/sql-pg/PgClient";
3 import * as Alchemy from "alchemy";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6
7 export const BootstrapDatabase = Alchemy.Action(
8 "BootstrapDatabase",
9 Effect.fn(function* (input: { endpoint: string; roleArn: string; version: string }) {
10 const token = yield* Presign.presignUrl({
11 method: "GET",
12 url: `https://${input.endpoint}/?Action=DbConnectAdmin`,
13 service: "dsql",
14 expiresIn: 900,
15 });
16 yield* Effect.gen(function* () {
17 const sql = yield* PgClient.PgClient;
18 // DSQL requires separate autocommit statements for catalog changes.
19 yield* sql`CREATE SCHEMA IF NOT EXISTS app`;
20 const roles = yield* sql`SELECT rolname FROM pg_roles WHERE rolname = 'app_user'`;
21 if (roles.length === 0) {
22 yield* sql`CREATE ROLE app_user WITH LOGIN`;
23 }
24 yield* sql`CREATE TABLE IF NOT EXISTS app.todos (
25 id uuid PRIMARY KEY,
26 text text NOT NULL,
27 done boolean NOT NULL DEFAULT false,
28 created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
29 )`;
30 yield* sql`GRANT USAGE ON SCHEMA app TO app_user`;
31 yield* sql`GRANT SELECT, INSERT, UPDATE, DELETE ON app.todos TO app_user`;
32 const mappings = yield* sql`SELECT arn FROM sys.iam_pg_role_mappings
33 WHERE pg_role_name = 'app_user' AND arn = ${input.roleArn}`;
34 if (mappings.length === 0) {
35 yield* sql.unsafe(`AWS IAM GRANT app_user TO '${input.roleArn.replaceAll("'", "''")}'`);
36 }
37 }).pipe(
38 Effect.provide(
39 PgClient.layer({
40 host: input.endpoint,
41 port: 5432,
42 database: "postgres",
43 username: "admin",
44 password: Redacted.make(token.slice("https://".length)),
45 ssl: { rejectUnauthorized: true, servername: input.endpoint },
46 }),
47 ),
48 );
49 return input.version;
50 }),
51 );