Drizzle ORM for TypeScript: Complete Guide
\n\n1. What is Drizzle ORM and why is it needed?
\nDrizzle ORM is a modern, lightweight, and type-safe ORM (Object-Relational Mapping) library for TypeScript and JavaScript. Unlike heavyweight ORMs like TypeORM or Sequelize, Drizzle is designed with a focus on performance, predictability, and full integration with the TypeScript type system. It does not try to hide SQL; instead, it provides a convenient and type-safe way to write SQL queries while maintaining full control over the generated code.
\nThe main goal of Drizzle is to free the developer from writing raw SQL strings and manually typing query results. You describe the database schema in TypeScript code, and Drizzle automatically infers types for all tables, relations, and queries. This means errors related to incorrect column names, wrong data types, or missing fields will be caught at compile time rather than at runtime. The library supports PostgreSQL, MySQL, SQLite, and other popular databases.
\nDrizzle is ideal for projects where performance and control over SQL are important, but you still want the conveniences of an ORM: IDE autocompletion, type checking, and declarative schema description. It is often used in conjunction with Next.js, NestJS, Express, and other frameworks. Thanks to its lightweight nature (about 7KB gzipped) and lack of magic, Drizzle is an excellent choice for both small projects and large enterprise applications.
\n2. Installation
\nInstalling Drizzle ORM and its CLI tool (Drizzle Kit) is done via npm or yarn. The choice of driver depends on the database being used.
\n# For PostgreSQL\nnpm install drizzle-orm pg\nnpm install -D drizzle-kit @types/pg\n\n# For MySQL\nnpm install drizzle-orm mysql2\nnpm install -D drizzle-kit @types/node\n\n# For SQLite\nnpm install drizzle-orm better-sqlite3\nnpm install -D drizzle-kit @types/better-sqlite3\n After installation, you need to create a configuration file drizzle.config.ts for Drizzle Kit:
import type { Config } from "drizzle-kit";\n\nexport default {\n schema: "./src/db/schema.ts",\n out: "./drizzle",\n dialect: "postgresql", // or "mysql", "sqlite"\n dbCredentials: {\n url: process.env.DATABASE_URL!,\n },\n} satisfies Config;\n 3. Quick Start
\nA minimal working example for PostgreSQL. First, define the schema, then create a connection and execute a query.
\n// src/db/schema.ts\nimport { pgTable, serial, text, integer } from "drizzle-orm/pg-core";\n\nexport const users = pgTable("users", {\n id: serial("id").primaryKey(),\n name: text("name").notNull(),\n age: integer("age"),\n});\n\n// src/index.ts\nimport { drizzle } from "drizzle-orm/node-postgres";\nimport { Pool } from "pg";\nimport { users } from "./db/schema";\nimport { eq } from "drizzle-orm";\n\nconst pool = new Pool({\n connectionString: process.env.DATABASE_URL,\n});\n\nconst db = drizzle(pool);\n\nasync function main() {\n // Insert data\n const newUser = await db.insert(users).values({\n name: "Alice",\n age: 30,\n }).returning();\n console.log("Created user:", newUser);\n\n // Select data\n const allUsers = await db.select().from(users);\n console.log("All users:", allUsers);\n\n // Select with condition\n const user = await db.select()\n .from(users)\n .where(eq(users.name, "Alice"));\n console.log("Found user:", user);\n}\n\nmain().catch(console.error);\n 4. Main Methods and Functions
\nBelow are the key methods of Drizzle ORM with