Skip to content

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.

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.

import * as SQL from "alchemy/SQL/LibSQL";
const sql = yield* SQL.LibSQL(conn);
const users = yield* sql`SELECT * FROM users`;

See Effect SQL: libSQL.

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:

Terminal window
pnpm add @effect/sql-libsql

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)),
) {}
const conn = yield* Turso.Connect(Db, { authorization: "read-only" });

Writes on this connection fail.

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.

Create a DatabaseToken and give db.url and the token to your app.