Drizzle ORM
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
-
ER_PARSE_ERROR: You have an error in your SQL syntax
cause Untrusted input was passed to `sql.identifier()` or `sql.as()` without proper escaping in Drizzle ORM versions prior to 0.45.2 or 1.0.0-beta.20, leading to malformed SQL queries.fixUpgrade Drizzle ORM to at least `0.45.2` (or `1.0.0-beta.20`) to ensure automatic escaping. Always validate or sanitize user-provided identifiers if they are directly used in `sql.identifier()`. -
Error: DrizzleError: Object key collision detected. Make sure your schema definitions are unique.
cause Multiple schema definitions (e.g., tables, columns, relations) with the same key were found within your Drizzle ORM schema, leading to a conflict during processing.fixReview your Drizzle ORM schema files and ensure that all table, column, and relation definitions have unique names to prevent key collisions. -
Error: D1_ERROR: ... syntax error near '...'
cause Migration script generated by `drizzle-kit` contained incompatible SQL syntax or commands for Cloudflare D1, or there were environment setup issues specific to D1.fixUpgrade `drizzle-kit` to the latest version (>=0.31.10) as a fix for D1 migration failures was released. Also, ensure your D1 environment is correctly configured and consult Drizzle ORM's official documentation for D1-specific migration guidelines.
Warnings
- breaking Previously, `sql.identifier()` and `sql.as()` functions were not properly escaping values, leading to a potential SQL Injection vulnerability (CWE-89).
- breaking A significant regression in the migration infrastructure was present, potentially causing issues with migration tracking, validation, and application.
- gotcha Adding a new value to a PostgreSQL enum will no longer be automatically treated as a commutative (non-breaking) change in migration generation by `drizzle-kit`.
- gotcha MSSQL `real()` column types might return imprecise `float64` values due to a missing `mapFromDriverValue` implementation.
Install
-
npm install drizzle-orm -
yarn add drizzle-orm -
pnpm add drizzle-orm
Imports
- drizzle
const drizzle = require('drizzle-orm/postgres-js');import { drizzle } from 'drizzle-orm/postgres-js';
Quickstart
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();