Drizzle <> Turso Database Sync

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/sync
npm i -D drizzle-kit@rc

Step 2 - Initialize the driver and make a query

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

const db = drizzle({
	connection: {
    	path: 'local.db',                // path used as a prefix for local files created by sync-engine
    	url: 'https://<db>.turso.io',    // URL of the remote database: turso db show <db>
    	authToken: '...',                // auth token issued from the Turso Cloud: turso db tokens create <db>
    	clientName: 'turso-sync-example' // arbitrary client name
	}
});

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

If you need to provide your existing drivers:

import { connect } from '@tursodatabase/sync';
import { drizzle } from 'drizzle-orm/tursodatabase-sync';

const client = await connect({
    path: 'local.db',                // path used as a prefix for local files created by sync-engine
    url: 'https://<db>.turso.io',    // URL of the remote database: turso db show <db>
    authToken: '...',                // auth token issued from the Turso Cloud: turso db tokens create <db>
    clientName: 'turso-sync-example' // arbitrary client name
});
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-sync';
import { users } from './schema';

const db = drizzle({ connection: { path: 'local.db', url: 'https://<db>.turso.io', authToken: '...' } });

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?