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

Retrieve 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

Create a Drizzle schema

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

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

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

4

Connect Drizzle with libSQL

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

import { db } from "./db";
import { fooTable } from "./schema";

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

Connect 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";

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

Launch Drizzle Studio:

npx drizzle-kit studio