sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ content/ docs/ turso/ connections.mdx

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

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

import * as SQL from "@alchemy.run/sql/SQLite";

const sql = yield* SQL.LibSQL(conn);

const users = yield* sql`SELECT * FROM users`;

See Effect SQL: libSQL.

Query with Drizzle

import * as Drizzle from "@alchemy.run/drizzle/LibSQL";

const db = yield* Drizzle.LibSQL(conn);

const users = yield* db.select().from(Users);

Pass { relations } to use db.query. See Drizzle: libSQL.

Effect SQL needs @alchemy.run/sql and Drizzle needs @alchemy.run/drizzle. Both need @effect/sql-libsql:

pnpm add @alchemy.run/sql @effect/sql-libsql      # Effect SQL
pnpm add @alchemy.run/drizzle @effect/sql-libsql  # Drizzle

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

const conn = yield* Turso.Connect(Db, { authorization: "read-only" });

Writes on this connection fail.

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

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

Where next