Drizzle banner

Prerequisites

Before you start, make sure you:

1

Install Drizzle and the libSQL SDK

npm i drizzle-orm @libsql/client
npm i -D drizzle-kit
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.local:

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

Create a Drizzle schema

drizzle/schema.ts
import { sql } from "drizzle-orm";
import { text, sqliteTable } from "drizzle-orm/sqlite-core";

const foo = sqliteTable("foo", {
  bar: text("bar").notNull().default("Hey!"),
});

To learn about configuring Drizzle migrations, go to this page.

4

Configure Drizzle to work with the LibSQL client

db.ts
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";

const turso = createClient({
  url: process.env.TURSO_DATABASE_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN,
});

export const db = drizzle(turso);
5

Query your Turso database

App Router
import { db } from "./db";

const result = await db.select().from(foo).all();
6

Visualize Turso database with Drizzle Studio

To preview and manage database data with Drizzle Studio, prepare the Drizzle config file:

drizzle.config.ts
import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config();

export default {
  schema: "./drizzle/schema.ts",
  out: "./drizzle/migrations",
  driver: "turso",
  dbCredentials: {
    url: process.env.TURSO_DATABASE_URL,
    authToken: process.env.TURSO_AUTH_TOKEN,
  },
} satisfies Config;

Lauch Drizzle Studio.

npx drizzle-kit studio