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, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Drizzle from "@alchemy.run/drizzle/LibSQL"; |
| 3 | import * as SQL from "@alchemy.run/sql/SQLite"; |
| 4 | import * as Turso from "@alchemy.run/turso"; |
| 5 | import { createClient } from "@libsql/client/web"; |
| 6 | import { defineRelations, sql as drizzleSql } from "drizzle-orm"; |
| 7 | import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 10 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import { BindingDb, TenantGroup } from "./resources.ts"; |
| 14 | |
| 15 | const notes = sqliteTable("notes", { |
| 16 | id: integer("id").primaryKey(), |
| 17 | body: text("body").notNull(), |
| 18 | }); |
| 19 | |
| 20 | const authors = sqliteTable("authors", { |
| 21 | id: integer("id").primaryKey(), |
| 22 | name: text("name").notNull(), |
| 23 | }); |
| 24 | |
| 25 | const posts = sqliteTable("posts", { |
| 26 | id: integer("id").primaryKey(), |
| 27 | authorId: integer("author_id").notNull(), |
| 28 | title: text("title").notNull(), |
| 29 | }); |
| 30 | |
| 31 | const relations = defineRelations({ authors, posts }, (t) => ({ |
| 32 | authors: { posts: t.many.posts() }, |
| 33 | posts: { author: t.one.authors({ from: t.posts.authorId, to: t.authors.id }) }, |
| 34 | })); |
| 35 | |
| 36 | export default class TursoWorker extends Cloudflare.Worker<TursoWorker>()( |
| 37 | "TursoWorker", |
| 38 | { main: import.meta.url }, |
| 39 | Effect.gen(function* () { |
| 40 | const conn = yield* Turso.Connect(BindingDb); |
| 41 | const sql = yield* SQL.LibSQL(conn); |
| 42 | const db = yield* Drizzle.LibSQL(conn); |
| 43 | const related = yield* Drizzle.LibSQL(conn, { relations }); |
| 44 | const tenants = yield* Turso.ManageDatabases(TenantGroup); |
| 45 | |
| 46 | return { |
| 47 | fetch: Effect.gen(function* () { |
| 48 | const request = yield* HttpServerRequest; |
| 49 | const url = new URL(request.url, "http://worker"); |
| 50 | |
| 51 | if (url.pathname === "/sql") { |
| 52 | yield* sql`CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL)`; |
| 53 | yield* sql`INSERT INTO notes (body) VALUES (${"from-sql"})`; |
| 54 | const rows = yield* sql<{ n: number }>`SELECT count(*) AS n FROM notes`; |
| 55 | return yield* HttpServerResponse.json({ count: rows[0]?.n }); |
| 56 | } |
| 57 | |
| 58 | if (url.pathname === "/relations") { |
| 59 | yield* sql`CREATE TABLE IF NOT EXISTS authors (id INTEGER PRIMARY KEY, name TEXT NOT NULL)`; |
| 60 | yield* sql`CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, author_id INTEGER NOT NULL, title TEXT NOT NULL)`; |
| 61 | yield* sql`INSERT OR REPLACE INTO authors (id, name) VALUES (1, 'ada')`; |
| 62 | yield* sql`INSERT OR REPLACE INTO posts (id, author_id, title) VALUES (1, 1, 'engines')`; |
| 63 | const author = yield* related.query.authors.findFirst({ |
| 64 | where: { id: 1 }, |
| 65 | with: { posts: true }, |
| 66 | }); |
| 67 | return yield* HttpServerResponse.json({ author }); |
| 68 | } |
| 69 | |
| 70 | if (url.pathname === "/client") { |
| 71 | const client = createClient({ |
| 72 | url: yield* conn.url, |
| 73 | authToken: Redacted.value(yield* conn.authToken), |
| 74 | }); |
| 75 | const result = yield* Effect.promise(() => client.execute("SELECT 7 AS seven")); |
| 76 | client.close(); |
| 77 | return yield* HttpServerResponse.json({ seven: result.rows[0]?.seven }); |
| 78 | } |
| 79 | |
| 80 | if (url.pathname === "/tx") { |
| 81 | yield* sql`CREATE TABLE IF NOT EXISTS ledger (v INTEGER NOT NULL)`; |
| 82 | const before = yield* sql<{ n: number }>`SELECT count(*) AS n FROM ledger`; |
| 83 | // The second insert violates NOT NULL, so the first must roll back. |
| 84 | const failed = yield* sql |
| 85 | .withTransaction( |
| 86 | Effect.gen(function* () { |
| 87 | yield* sql`INSERT INTO ledger (v) VALUES (${1})`; |
| 88 | yield* sql`INSERT INTO ledger (v) VALUES (${null})`; |
| 89 | }), |
| 90 | ) |
| 91 | .pipe( |
| 92 | Effect.as(false), |
| 93 | Effect.catchTag("SqlError", () => Effect.succeed(true)), |
| 94 | ); |
| 95 | const after = yield* sql<{ n: number }>`SELECT count(*) AS n FROM ledger`; |
| 96 | return yield* HttpServerResponse.json({ |
| 97 | failed, |
| 98 | before: before[0]?.n, |
| 99 | after: after[0]?.n, |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | if (url.pathname === "/drizzle") { |
| 104 | yield* db.insert(notes).values({ body: "from-drizzle" }); |
| 105 | const rows = yield* db |
| 106 | .select() |
| 107 | .from(notes) |
| 108 | .where(drizzleSql`body = 'from-drizzle'`); |
| 109 | return yield* HttpServerResponse.json({ bodies: rows.map((r) => r.body) }); |
| 110 | } |
| 111 | |
| 112 | if (url.pathname === "/tenant") { |
| 113 | const name = url.searchParams.get("name")!; |
| 114 | const created = yield* tenants.create(name); |
| 115 | const tenantSql = yield* SQL.LibSQL(tenants.connect(created.name)); |
| 116 | yield* tenantSql`CREATE TABLE kv (k TEXT PRIMARY KEY, v TEXT)`; |
| 117 | yield* tenantSql`INSERT INTO kv VALUES ('tenant', ${name})`; |
| 118 | const rows = yield* tenantSql<{ v: string }>`SELECT v FROM kv`; |
| 119 | const listed = yield* tenants.list(); |
| 120 | yield* tenants.delete(created.name); |
| 121 | const afterDelete = yield* tenants.get(created.name); |
| 122 | return yield* HttpServerResponse.json({ |
| 123 | url: created.url, |
| 124 | value: rows[0]?.v, |
| 125 | listed: listed.map((d) => d.name), |
| 126 | deleted: afterDelete === undefined, |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | return HttpServerResponse.text("ok"); |
| 131 | }).pipe(Effect.orDie), |
| 132 | }; |
| 133 | }).pipe(Effect.provide(Layer.mergeAll(Turso.ConnectHttp, Turso.ManageDatabasesHttp))), |
| 134 | ) {} |