ORM for TypeScript: Comparison and Examples
In modern web development with TypeScript, choosing the right ORM (Object-Relational Mapping) can significantly speed up development and increase application reliability. An ORM allows you to work with databases through an object-oriented approach, avoiding writing raw SQL queries. In this article, we will compare three of the most popular ORMs for TypeScript: Prisma, TypeORM, and Drizzle, and also look at examples of their use.
1. Prisma — The Modern Standard
Prisma is a next-generation ORM that focuses on developer experience and performance. It uses its own schema description language (Prisma Schema) and generates a fully typed client.
Key Features:
- Automatic generation of TypeScript types
- Intuitive query API
- Built-in data migration (Prisma Migrate)
- Support for PostgreSQL, MySQL, SQLite, MongoDB, and others
TypeScript Code Example:
// schema.prismamodel User { id Int @id @default(autoincrement()) name String email String @unique posts Post[]}
model Post { id Int @id @default(autoincrement()) title String content String? authorId Int author User @relation(fields: [authorId], references: [id])}
// Usage in codeimport { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function getUsersWithPosts() { const users = await prisma.user.findMany({ include: { posts: true } }); return users;}2. TypeORM — The Classic for TypeScript
TypeORM is a mature ORM that supports both Active Record and Data Mapper patterns. It is tightly integrated with TypeScript and uses decorators to define models.
Key Features:
- Support for decorators and classes
- Two approaches: Active Record and Data Mapper
- Advanced migration and event subscriptions
- Support for many database systems
TypeScript Code Example:
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, createConnection } from "typeorm";
@Entity()export class User { @PrimaryGeneratedColumn() id: number;
@Column() name: string;
@Column({ unique: true }) email: string;
@OneToMany(() => Post, post => post.user) posts: Post[];}
@Entity()export class Post { @PrimaryGeneratedColumn() id: number;
@Column() title: string;
@Column({ nullable: true }) content: string;
@ManyToOne(() => User, user => user.posts) user: User;}
// Usageasync function getUsersWithPosts() { const connection = await createConnection(); const userRepository = connection.getRepository(User); return await userRepository.find({ relations: ["posts"] });}3. Drizzle — Lightweight and Performant
Drizzle is a relatively new ORM that emphasizes performance and minimal bundle size. It uses SQL-like syntax and does not require code generation.
Key Features:
- Minimal size and high speed
- Native TypeScript support without decorators
- SQL-like API (query builder)
- Compatibility with SQL databases
TypeScript Code Example:
import { drizzle } from 'drizzle-orm/better-sqlite3';import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';import Database from 'better-sqlite3';
const sqlite = new Database('test.db');const db = drizzle(sqlite);
// Schema definitionconst users = sqliteTable('users', { id: integer('id').primaryKey(), name: text('na