Drizzle ORM

TypeScript ORM for SQL

README

Drizzle ORM npm

npmnpm bundle sizeDiscordLicense
If you know SQL, you know Drizzle ORM


Drizzle ORM is a TypeScript ORM for SQL databases designed with maximum type safety in mind. It comes with a drizzle-kit CLI companion for automatic SQL migrations generation. Drizzle ORM is meant to be a library, not a framework. It stays as an opt-in solution all the time at any levels.

The ORM main philosophy is "If you know SQL, you know Drizzle ORM". We follow the SQL-like syntax whenever possible, are strongly typed ground top and fail at compile time, not in runtime.

Drizzle ORM is being battle-tested on production projects by multiple teams 🚀 Give it a try and let us know if you have any questions or feedback on Discord.

Feature list


- Full type safety
- No ORM learning curve
- SQL-like syntax for table definitions and queries
- Best in class fully typed joins
- Fully typed partial and non-partial selects of any complexity
- Auto-inferring of TS types for DB models for selections and insertions separately
- Zero dependencies

Supported databases


| Database    | Status | |
|:------------|:-------:|:---|
| PostgreSQL  | ✅ | Docs|
| MySQL       | ✅      |Docs|
| SQLite      | ✅      |Docs|
| DynamoDB    | ⏳      |            |
| MS SQL      | ⏳      |            |
| CockroachDB | ⏳      |            |

Installation


  1. ```bash
  2. npm install drizzle-orm
  3. npm install -D drizzle-kit
  4. ```

Feature showcase (PostgreSQL)


Note: don't forget to install pg and @types/pg packages for this example to work.


  1. ```typescript
  2. import { eq } from 'drizzle-orm/expressions';
  3. import { drizzle } from 'drizzle-orm/node-postgres';
  4. import { InferModel, integer, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core';
  5. import { sql } from 'drizzle-orm/sql';
  6. import { Pool } from 'pg';

  7. export const users = pgTable('users', {
  8.   id: serial('id').primaryKey(),
  9.   fullName: text('full_name').notNull(),
  10.   phone: varchar('phone', { length: 20 }).notNull(),
  11.   role: text<'user' | 'admin'>('role').default('user').notNull(),
  12.   cityId: integer('city_id').references(() => cities.id),
  13.   createdAt: timestamp('created_at').defaultNow().notNull(),
  14.   updatedAt: timestamp('updated_at').defaultNow().notNull(),
  15. });

  16. export type User = InferModel<typeof users>;
  17. export type NewUser = InferModel<typeof users, 'insert'>;

  18. export const cities = pgTable('cities', {
  19.   id: serial('id').primaryKey(),
  20.   name: text('name').notNull(),
  21. });

  22. export type City = InferModel<typeof cities>;
  23. export type NewCity = InferModel<typeof cities, 'insert'>;

  24. const pool = new Pool({
  25.   connectionString: 'postgres://user:password@host:port/db',
  26. });

  27. const db = drizzle(pool);

  28. // Insert
  29. const newUser: NewUser = {
  30.   fullName: 'John Doe',
  31.   phone: '+123456789',
  32. };
  33. const insertedUsers /* : User */ = await db.insert(users).values(newUser).returning();
  34. const insertedUser = insertedUsers[0]!;

  35. const newCity: NewCity = {
  36.   name: 'New York',
  37. };
  38. const insertedCities /* : City */ = await db.insert(cities).values(newCity).returning();
  39. const insertedCity = insertedCities[0]!;

  40. // Update
  41. const updateResult /* : { updated: Date }[] */ = await db.update(users)
  42.   .set({ cityId: insertedCity.id, updatedAt: new Date() })
  43.   .where(eq(users.id, insertedUser.id))
  44.   .returning({ updated: users.updatedAt });

  45. // Select
  46. const allUsers /* : User[] */ = await db.select(users);

  47. // Select custom fields
  48. const upperCaseNames /* : { id: number; name: string }[] */ = await db.select(users)
  49.   .fields({
  50.     id: users.id,
  51.     name: sql`upper(${users.fullName})`.as<string>(),
  52.   });

  53. // Joins
  54. // You wouldn't BELIEVE how SMART the result type is! 😱
  55. const allUsersWithCities = await db.select(users)
  56.   .fields({
  57.     user: {
  58.       id: users.id,
  59.       name: users.fullName,
  60.     },
  61.     cityId: cities.id,
  62.     cityName: cities.name,
  63.   })
  64.   .leftJoin(cities, eq(users.cityId, cities.id));

  65. // Delete
  66. const deletedNames /* : { name: string }[] */ = await db.delete(users)
  67.   .where(eq(users.id, insertedUser.id))
  68.   .returning({ name: users.fullName });
  69. ```

See full docs for further reference: