API Reference

TypeORM

TypeScript ORM for Node.js with decorators, active record and data mapper patterns, migrations, and relations.

Official Documentation ↗
CompatibilityThis documentation covers version v0.3.20. Check the official docs for the latest API surface.

Installation

npmnpm install typeorm
pnpmpnpm add typeorm
yarnyarn add typeorm

Methods

TypeORM is an ORM that runs in Node.js and the browser, written in TypeScript. It supports both Active Record and Data Mapper patterns, decorator-based entities, migrations, and advanced relations.

Key Features

  • Decorators: @Entity, @Column, @ManyToOne, @OneToMany for schema mapping
  • Patterns: Active Record and Data Mapper repositories
  • Migrations: Generated and manual schema migrations
  • Query builder: Fluent SQL construction with joins and subqueries
  • Multi-database: MySQL, Postgres, SQLite, SQL Server, MongoDB

Errors

The official API reference declares the following error types:

API reference evidence

Fetched from official sources on 2026-11-08T16:01:12.9137723+08:00 via PowerShell Invoke-WebRequest 下载官方仓库 packages/typeorm/src 下源码文件,Grep/Read 提取签名.

FAQ

Active Record vs Data Mapper in TypeORM?
Active Record extends BaseEntity and calls methods on entities (user.save()). Data Mapper uses repositories (repo.save(user)). Data Mapper is recommended for larger apps; Active Record is simpler for small ones.
Should synchronize be true in production?
No. synchronize: true alters tables on boot and risks data loss. Use migrations for production schema changes and keep synchronize off or dev-only.
How do I handle relations with eager loading?
Mark a relation with { eager: true } to always load it, or use leftJoinAndSelect in a query builder. Eager loading can cause N+1 if misused, so prefer explicit joins for control.