Drizzle <> Turso Database

This guide assumes familiarity with:

According to the official website, Turso is the small database to power your big dreams in the age of AI.

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm@rc @tursodatabase/database
npm i -D drizzle-kit@rc

Step 2 - Initialize the driver and make a query

import { drizzle } from 'drizzle-orm/tursodatabase/database';

const db = drizzle('sqlite.db');

const result = await db.all('select 1');

If you need to provide your existing drivers:

import { Database } from '@tursodatabase/database';
import { drizzle } from 'drizzle-orm/tursodatabase/database';

const client = new Database('sqlite.db');
const db = drizzle({ client });

const result = await db.all('select 1');

Transaction modes

You can set the transaction mode with the behavior option. Drizzle passes it to the driver, which opens the transaction with BEGIN <mode>. Without behavior, a plain BEGIN is used, which SQLite runs as deferred.

import { drizzle } from 'drizzle-orm/tursodatabase/database';
import { users } from './schema';

const db = drizzle('sqlite.db');

await db.transaction(async (tx) => {
  await tx.insert(users).values({ name: 'Dan' });
}, {
  behavior: 'immediate', // 'deferred' | 'immediate' | 'exclusive' | 'concurrent'
});

Nested tx.transaction() calls use savepoints and don’t take a behavior option.

What’s next?