Drizzle ORM

0.45.2 · active · verified Sat Apr 18

Drizzle ORM is a lightweight, TypeScript-first ORM for SQL databases, offering a flexible API for database interactions, schema definition, and migrations. The current stable version is `0.45.2`, with active development ongoing on the `1.0.0-beta` branch, seeing frequent new features and fixes. Stable releases are less frequent, focusing on critical bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

Connects to an in-memory SQLite database using `better-sqlite3`, defines a simple schema, inserts data, and queries it, demonstrating basic Drizzle ORM usage.

import { drizzle } from 'drizzle-orm/better-sqlite3';
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
import Database from 'better-sqlite3';

// Initialize an in-memory SQLite database
const sqlite = new Database(':memory:');
const db = drizzle(sqlite);

// Define a simple schema for users
export const users = sqliteTable('users', {
  id: integer('id').primaryKey(),
  name: text('name').notNull(),
});

async function main() {
  // For quickstart, manually create the table. In production, use Drizzle Kit for migrations.
  sqlite.exec(`
    CREATE TABLE IF NOT EXISTS users (
      id INTEGER PRIMARY KEY,
      name TEXT NOT NULL
    );
  `);

  // Insert data
  await db.insert(users).values({ id: 1, name: 'Alice' }).execute();
  await db.insert(users).values({ id: 2, name: 'Bob' }).execute();

  // Query data
  const allUsers = db.select().from(users).all();
  console.log('Users:', allUsers); 
  // Expected output: Users: [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]

  sqlite.close();
}

main();

view raw JSON →