Skills2026年4月6日·1 分钟阅读

Drizzle ORM — TypeScript SQL That Feels Like Code

Type-safe TypeScript ORM with SQL-like syntax. Zero overhead, serverless-ready, supports PostgreSQL, MySQL, SQLite. Schema as code with automatic migrations. 28,000+ GitHub stars.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Drizzle ORM — TypeScript SQL That Feels Like Code
直接安装命令
npx -y tokrepo@latest install 0e904437-215d-4941-af94-eb0cf7424e0d --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Drizzle ORM gives you type-safe SQL queries in TypeScript with zero runtime overhead and schema-as-code.
§01

What it is

Drizzle ORM is a TypeScript ORM that mirrors SQL syntax directly in your code. Instead of learning a new query API, you write queries that look like SQL but with full TypeScript type safety. It supports PostgreSQL, MySQL, and SQLite with zero runtime overhead, meaning the generated SQL is exactly what you would write by hand.

Drizzle targets TypeScript developers building serverless functions, edge workers, and full-stack applications who want type safety without the performance cost of traditional ORMs.

§02

How it saves time or tokens

Drizzle eliminates the gap between your mental model of SQL and your ORM code. If you know SQL, you know Drizzle. Schema-as-code means your database schema lives in TypeScript files, version-controlled alongside your application. Drizzle Kit handles migrations automatically by diffing your schema against the database. The zero-overhead design means queries compile to plain SQL with no runtime parsing or transformation.

§03

How to use

  1. Install Drizzle:
npm install drizzle-orm postgres
npm install -D drizzle-kit
  1. Define your schema:
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  createdAt: timestamp('created_at').defaultNow(),
});
  1. Query with full type safety:
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { users } from './schema';
import { eq } from 'drizzle-orm';

const client = postgres(process.env.DATABASE_URL!);
const db = drizzle(client);

const result = await db.select().from(users).where(eq(users.email, 'alice@example.com'));
// result is typed as { id: number; name: string; email: string; createdAt: Date }[]
§04

Example

Joins and relations with type inference:

import { pgTable, serial, text, integer } from 'drizzle-orm/pg-core';

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  authorId: integer('author_id').references(() => users.id),
});

const postsWithAuthors = await db
  .select({ title: posts.title, author: users.name })
  .from(posts)
  .innerJoin(users, eq(posts.authorId, users.id));
§05

Related on TokRepo

§06

Common pitfalls

  • Drizzle's SQL-like API means you need to know SQL. Developers expecting a high-level abstraction like ActiveRecord may find it verbose for simple CRUD.
  • Migration files generated by Drizzle Kit should be reviewed before running in production. Automatic diffing can produce unexpected ALTER TABLE statements.
  • The ecosystem of plugins and extensions is smaller than Prisma's. Check community adapter availability for your specific database driver.

常见问题

How is Drizzle different from Prisma?+

Prisma uses its own schema language (Prisma Schema) and generates a client with a custom query API. Drizzle uses TypeScript for schemas and mirrors SQL syntax directly. Drizzle has zero runtime overhead while Prisma includes a query engine binary.

Does Drizzle support serverless environments?+

Yes. Drizzle works with serverless databases like Neon, PlanetScale, Turso, and Cloudflare D1. Its zero-dependency design and small bundle size make it ideal for edge and serverless runtimes.

How do migrations work?+

Drizzle Kit compares your TypeScript schema against the database and generates SQL migration files. Run drizzle-kit generate to create migrations and drizzle-kit migrate to apply them.

Can I use raw SQL with Drizzle?+

Yes. Drizzle provides a sql template tag for raw SQL queries while maintaining type safety. You can also use db.execute() for fully custom queries.

What databases does Drizzle support?+

Drizzle supports PostgreSQL, MySQL, and SQLite. Driver adapters exist for node-postgres, mysql2, better-sqlite3, Neon, PlanetScale, Turso, and Cloudflare D1.

引用来源 (3)
🙏

来源与感谢

Created by Drizzle Team. Licensed under Apache 2.0.

drizzle-orm — ⭐ 28,000+

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产