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 | # @alchemy.run/turso |
| 2 | |
| 3 | Turso for [Alchemy](https://alchemy.run). Declare SQLite databases, groups, and tokens in the same TypeScript program as the Workers that query them. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/turso @alchemy.run/cloudflare @alchemy.run/sql @effect/sql-libsql effect |
| 7 | ``` |
| 8 | |
| 9 | A database with migrations, queried from a Cloudflare Worker: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/worker.ts |
| 13 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 14 | import * as SQL from "@alchemy.run/sql/SQLite"; |
| 15 | import * as Turso from "@alchemy.run/turso"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 18 | |
| 19 | export const Db = Effect.gen(function* () { |
| 20 | const group = yield* Turso.Group("Group", { location: "aws-us-east-1" }); |
| 21 | return yield* Turso.Database("Db", { |
| 22 | group: group.name, |
| 23 | migrations: "./migrations", |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | export default Cloudflare.Worker( |
| 28 | "Worker", |
| 29 | { main: import.meta.url }, |
| 30 | Effect.gen(function* () { |
| 31 | // The Worker gets the database URL and its own scoped token. |
| 32 | const sql = yield* SQL.LibSQL(yield* Turso.Connect(Db)); |
| 33 | |
| 34 | return { |
| 35 | fetch: Effect.gen(function* () { |
| 36 | const notes = yield* sql`SELECT * FROM notes`; |
| 37 | return yield* HttpServerResponse.json(notes); |
| 38 | }).pipe(Effect.orDie), |
| 39 | }; |
| 40 | }).pipe(Effect.provide(Turso.ConnectHttp)), |
| 41 | ); |
| 42 | ``` |
| 43 | |
| 44 | ```typescript |
| 45 | // alchemy.run.ts |
| 46 | import * as Alchemy from "alchemy"; |
| 47 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 48 | import * as Turso from "@alchemy.run/turso"; |
| 49 | import * as Effect from "effect/Effect"; |
| 50 | import * as Layer from "effect/Layer"; |
| 51 | import Worker from "./src/worker.ts"; |
| 52 | |
| 53 | export default Alchemy.Stack( |
| 54 | "MyApp", |
| 55 | { |
| 56 | providers: Layer.mergeAll(Cloudflare.providers(), Turso.providers()), |
| 57 | state: Cloudflare.state(), |
| 58 | }, |
| 59 | Effect.gen(function* () { |
| 60 | const worker = yield* Worker; |
| 61 | return { url: worker.url }; |
| 62 | }), |
| 63 | ); |
| 64 | ``` |
| 65 | |
| 66 | Turso databases are cheap, so a Worker can also create one per tenant with `Turso.ManageDatabases`. |
| 67 | |
| 68 | ## Learn more |
| 69 | |
| 70 | - [Turso on alchemy.run](https://alchemy.run/turso) |
| 71 | - [Setup](https://alchemy.run/turso/setup) |
| 72 | - [Tutorial](https://alchemy.run/turso/tutorial/part-1) |
| 73 | - [Database per tenant](https://alchemy.run/turso/database-per-tenant) |