Become a Gold Sponsor

Drizzle <> SQLite

Drizzle has native support for SQLite connections with the libsql and better-sqlite3 drivers.

There are a few differences between the libsql and better-sqlite3 drivers that we discovered while using both and integrating them with the Drizzle ORM. For example:

At the driver level, there may not be many differences between the two, but the main one is that libSQL can connect to both SQLite files and Turso remote databases. LibSQL is a fork of SQLite that offers a bit more functionality compared to standard SQLite, such as:

libsql

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm @libsql/client
npm i -D drizzle-kit

Step 2 - Initialize the driver

Drizzle has native support for all @libsql/client driver variations:

@libsql/clientdefaults to node import, automatically changes to web if target or platofrm is set for bundler, e.g. esbuild --platform=browser
@libsql/client/nodenode compatible module, supports :memory:, file, wss, http and turso conneciton protocols
@libsql/client/webmodule for fullstack web frameworks like next, nuxt, astro, etc.
@libsql/client/httpmodule for http and https connection protocols
@libsql/client/wsmodule for ws and wss conneciton protocols
@libsql/client/sqlite3module for :memory: and file conneciton protocols
@libsql/client-wasmSeparate experimental package for WASM

default
node
web
http
web sockets
wasm
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle({ connection: {
  url: process.env.DATABASE_URL, 
  authToken: process.env.DATABASE_AUTH_TOKEN 
}});

Step 3 - make a query

libsql
libsql with config
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle(process.env.DATABASE_URL);
 
const result = await db.execute('select 1');

If you need a synchronous connection, you can use our additional connection API, where you specify a driver connection and pass it to the Drizzle instance.

import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';

const client = createClient({ url: process.env.DATABASE_URL, authToken: process.env.DATABASE_AUTH_TOKEN });
const db = drizzle(client);

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

better-sqlite3

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm better-sqlite3
npm i -D drizzle-kit @types/better-sqlite3

Step 2 - Initialize the driver and make a query

better-sqlite3
better-sqlite3 with config
import { drizzle } from 'drizzle-orm/better-sqlite3';

const db = drizzle(process.env.DATABASE_URL);

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

If you need to provide your existing driver:

import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';

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

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

What’s next?