Data-Mapper ORM for TypeScript and ES2021+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.
TypeORM is an Object-Relational Mapping library that lets you interact with relational databases using TypeScript classes and decorators instead of raw SQL. With over 3 million weekly downloads, it's one of the most popular database solutions in the Node.js ecosystem. It abstracts away database-specific SQL dialects while giving you control through both high-level entity management and low-level query building.
The library supports both DataMapper and ActiveRecord patterns, making it unique among JavaScript ORMs. DataMapper separates entities from persistence logic (better for complex domains), while ActiveRecord combines them (faster for simple CRUD apps). This flexibility means you can structure your application architecture based on actual needs rather than framework limitations.
TypeORM handles a wide range of databases including MySQL, PostgreSQL, SQLite, MS SQL Server, Oracle, MongoDB, and others. It provides automatic schema synchronization, migration generation, relation management, query caching, and connection pooling. The library works across platforms—from Node.js servers to React Native mobile apps—making it versatile for various JavaScript/TypeScript environments.
Developers choose TypeORM when they need type safety, want to avoid writing repetitive SQL, or need to support multiple database backends. It's particularly strong in TypeScript projects where its decorator-based syntax integrates naturally with class-based architectures. The QueryBuilder API provides an escape hatch for complex queries while maintaining type safety and readability.
import 'reflect-metadata';
import { DataSource, Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@Column()
name: string;
}
@Entity()
class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column('text')
content: string;
@ManyToOne(() => User)
@JoinColumn({ name: 'author_id' })
author: User;
}
const AppDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'test',
password: 'test',
database: 'test_db',
entities: [User, Post],
synchronize: true
});
AppDataSource.initialize().then(async () => {
const userRepo = AppDataSource.getRepository(User);
const postRepo = AppDataSource.getRepository(Post);
const user = userRepo.create({ email: 'alice@example.com', name: 'Alice' });
await userRepo.save(user);
const post = postRepo.create({ title: 'Hello TypeORM', content: 'First post', author: user });
await postRepo.save(post);
const posts = await postRepo
.createQueryBuilder('post')
.leftJoinAndSelect('post.author', 'author')
.where('author.email = :email', { email: 'alice@example.com' })
.getMany();
console.log(posts);
}).catch(error => console.log(error));Enterprise applications with complex data models - TypeORM excels in applications with numerous entities and relationships. Its support for eager/lazy loading, cascades, transactions, and migrations makes it suitable for applications where data integrity and schema evolution are critical.
Multi-tenant SaaS platforms - The library's support for cross-database and cross-schema queries allows you to implement database-per-tenant or schema-per-tenant isolation strategies while maintaining a unified codebase.
API backends requiring database portability - When you need to support multiple database vendors (e.g., PostgreSQL for production, SQLite for testing), TypeORM handles dialect differences automatically, letting you switch databases with minimal code changes.
Rapid prototyping with schema synchronization - During development, TypeORM can automatically create and update database tables based on your entity definitions, eliminating manual schema management and speeding up iteration cycles.
Cross-platform applications - Projects spanning Node.js servers, Electron desktop apps, React Native mobile apps, or Ionic hybrid apps can share the same data layer code, with TypeORM handling platform-specific database adapters (SQLite for mobile, PostgreSQL for server).
npm install typeormpnpm add typeormbun add typeorm