Connections
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.
Connect the database
Section titled “Connect the database”const conn = yield* Turso.Connect(Db);Add Turso.ConnectHttp to the Worker:
Effect.gen(function* () { const conn = yield* Turso.Connect(Db); // ...}).pipe(Effect.provide(Turso.ConnectHttp))conn.url and conn.authToken give you the URL and token while the Worker runs.
Query with Effect SQL
Section titled “Query with Effect SQL”import * as SQL from "alchemy/SQL/LibSQL";
const sql = yield* SQL.LibSQL(conn);
const users = yield* sql`SELECT * FROM users`;See Effect SQL: libSQL.
Query with Drizzle
Section titled “Query with Drizzle”import * as Drizzle from "alchemy/Drizzle/LibSQL";
const db = yield* Drizzle.LibSQL(conn);
const users = yield* db.select().from(Users);Pass { relations } to use db.query. See Drizzle: libSQL.
Both need @effect/sql-libsql:
pnpm add @effect/sql-libsqlConnect from AWS Lambda
Section titled “Connect from AWS Lambda”Turso.Connect works the same in a Lambda. Add @libsql/client to build.install so the Lambda gets the Linux version of the driver:
export default class Api extends Lambda.Function<Api>()( "Api", { main: import.meta.url, functionUrl: true, build: { install: ["@libsql/client"] }, }, Effect.gen(function* () { const conn = yield* Turso.Connect(Db); const sql = yield* SQL.LibSQL(conn); // ... }).pipe(Effect.provide(Turso.ConnectHttp)),) {}Connect read-only
Section titled “Connect read-only”const conn = yield* Turso.Connect(Db, { authorization: "read-only" });Writes on this connection fail.
Use @libsql/client directly
Section titled “Use @libsql/client directly”import { createClient } from "@libsql/client/web";import * as Redacted from "effect/Redacted";
const client = createClient({ url: yield* conn.url, authToken: Redacted.value(yield* conn.authToken),});Read conn.url and conn.authToken inside a request, not when the Worker starts.
Connect from somewhere else
Section titled “Connect from somewhere else”Create a DatabaseToken and give db.url and the token to your app.
Where next
Section titled “Where next”- Tokens
- Database per tenant — connect to databases your app creates
- Connect reference