Remix banner

Prerequisites

Before you start, make sure you:

1

Install the libSQL SDK

npm install @libsql/client
2

Configure database credentials

Get the database URL.

turso db show --url <database-name>

Get the database authentication token.

turso db tokens create <database-name>

Assign credentials to the environment variables inside .env.

TURSO_DATABASE_URL="..."
TURSO_AUTH_TOKEN="..."
3

Configure LibSQL Client.

app/lib/turso.ts
import { createClient } from "@libsql/client/http";
import { AppLoadContext } from "@remix-run/node";

interface Env {
  TURSO_AUTH_TOKEN?: string;
  TURSO_DATABASE_URL?: string;
}

export const turso = (serverContext: AppLoadContext) =>
  createClient({
    url: serverContext.env.TURSO_DATABASE_URL,
    authToken: serverContext.env.TURSO_AUTH_TOKEN,
  });
4

Execute SQL

app/routes/_index.ts
import type { LoaderArgs, LoaderFunction } from "@remix-run/node";
import { buildDbClient } from "~/lib/turso";

export const loader: LoaderFunction = async ({ context }: LoaderArgs) => {
  const db = turso(context);

  const { rows } = await db.execute("select * from table_name");

  return {
    items: rows,
  };
};

Examples