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 | --- |
| 2 | title: Connections |
| 3 | description: Query a Turso database from a Worker or Lambda. |
| 4 | --- |
| 5 | |
| 6 | `Turso.Connect` gives a Worker a database's URL and a token made just for that Worker. Your own Turso API token stays on your machine. |
| 7 | |
| 8 | ## Connect the database |
| 9 | |
| 10 | ```typescript |
| 11 | const conn = yield* Turso.Connect(Db); |
| 12 | ``` |
| 13 | |
| 14 | Add `Turso.ConnectHttp` to the Worker: |
| 15 | |
| 16 | ```typescript |
| 17 | Effect.gen(function* () { |
| 18 | const conn = yield* Turso.Connect(Db); |
| 19 | // ... |
| 20 | }).pipe(Effect.provide(Turso.ConnectHttp)) |
| 21 | ``` |
| 22 | |
| 23 | `conn.url` and `conn.authToken` give you the URL and token while the Worker runs. |
| 24 | |
| 25 | ## Query with Effect SQL |
| 26 | |
| 27 | ```typescript |
| 28 | import * as SQL from "@alchemy.run/sql/SQLite"; |
| 29 | |
| 30 | const sql = yield* SQL.LibSQL(conn); |
| 31 | |
| 32 | const users = yield* sql`SELECT * FROM users`; |
| 33 | ``` |
| 34 | |
| 35 | See [Effect SQL: libSQL](/sql/effect-sql/libsql). |
| 36 | |
| 37 | ## Query with Drizzle |
| 38 | |
| 39 | ```typescript |
| 40 | import * as Drizzle from "@alchemy.run/drizzle/LibSQL"; |
| 41 | |
| 42 | const db = yield* Drizzle.LibSQL(conn); |
| 43 | |
| 44 | const users = yield* db.select().from(Users); |
| 45 | ``` |
| 46 | |
| 47 | Pass `{ relations }` to use `db.query`. See [Drizzle: libSQL](/sql/drizzle/libsql). |
| 48 | |
| 49 | Effect SQL needs `@alchemy.run/sql` and Drizzle needs `@alchemy.run/drizzle`. Both need `@effect/sql-libsql`: |
| 50 | |
| 51 | ```sh |
| 52 | pnpm add @alchemy.run/sql @effect/sql-libsql # Effect SQL |
| 53 | pnpm add @alchemy.run/drizzle @effect/sql-libsql # Drizzle |
| 54 | ``` |
| 55 | |
| 56 | ## Connect from AWS Lambda |
| 57 | |
| 58 | `Turso.Connect` works the same in a Lambda. Add `@libsql/client` to `build.install` so the Lambda gets the Linux version of the driver: |
| 59 | |
| 60 | ```typescript |
| 61 | export default class Api extends Lambda.Function<Api>()( |
| 62 | "Api", |
| 63 | { |
| 64 | main: import.meta.url, |
| 65 | functionUrl: true, |
| 66 | build: { install: ["@libsql/client"] }, |
| 67 | }, |
| 68 | Effect.gen(function* () { |
| 69 | const conn = yield* Turso.Connect(Db); |
| 70 | const sql = yield* SQL.LibSQL(conn); |
| 71 | // ... |
| 72 | }).pipe(Effect.provide(Turso.ConnectHttp)), |
| 73 | ) {} |
| 74 | ``` |
| 75 | |
| 76 | ## Connect read-only |
| 77 | |
| 78 | ```typescript |
| 79 | const conn = yield* Turso.Connect(Db, { authorization: "read-only" }); |
| 80 | ``` |
| 81 | |
| 82 | Writes on this connection fail. |
| 83 | |
| 84 | ## Use @libsql/client directly |
| 85 | |
| 86 | ```typescript |
| 87 | import { createClient } from "@libsql/client/web"; |
| 88 | import * as Redacted from "effect/Redacted"; |
| 89 | |
| 90 | const client = createClient({ |
| 91 | url: yield* conn.url, |
| 92 | authToken: Redacted.value(yield* conn.authToken), |
| 93 | }); |
| 94 | ``` |
| 95 | |
| 96 | Read `conn.url` and `conn.authToken` inside a request, not when the Worker starts. |
| 97 | |
| 98 | ## Connect from somewhere else |
| 99 | |
| 100 | Create a [DatabaseToken](/turso/tokens) and give `db.url` and the token to your app. |
| 101 | |
| 102 | ## Where next |
| 103 | |
| 104 | - [Tokens](/turso/tokens) |
| 105 | - [Database per tenant](/turso/database-per-tenant) — connect to databases your app creates |
| 106 | - [Connect reference](/providers/turso/reference/database#connect) |