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
npm
npm install typeormpnpm
pnpm add typeormyarn
yarn add typeormMethods
No methods match this filter.
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:
TypeORMError(class) — official text: “export class TypeORMError extends Error { get name() { return this.constructor.name } constructor(message?: string) { super(message)” (source: https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/error/TypeORMError.ts, fetched 2026-11-08T16:01:12.9137723+08:00)QueryFailedError(class (extends TypeORMError)) — official text: “/** * Thrown when query execution has failed. */ export class QueryFailedErrorextends TypeORMError { constructor( readonly query: string, readonly parameters: any[] | ObjectLiteral | undefined, readonly driverError: T, ) {” (source: https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/error/QueryFailedError.ts, fetched 2026-11-08T16:01:12.9137723+08:00)
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 提取签名.
@Entity→ signature from https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/decorator/entity/Entity.ts; official text: “export function Entity(options?: EntityOptions): ClassDecorator export function Entity(name?: string, options?: EntityOptions): ClassDecorator”DataSource→ signature from https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/data-source/DataSource.ts; official text: “constructor(options: DataSourceOptions) {”getRepository→ signature from https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/data-source/DataSource.ts; official text: “getRepository( target: EntityTarget , ): Repository {” createQueryBuilder→ signature from https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/repository/Repository.ts; official text: “createQueryBuilder( alias?: string, queryRunner?: QueryRunner, ): SelectQueryBuilder{” transaction→ signature from https://github.com/typeorm/typeorm/blob/master/packages/typeorm/src/data-source/DataSource.ts; official text: “async transaction( runInTransaction: (entityManager: EntityManager) => Promise , ): Promise async transaction ( isolationLevel: IsolationLevel, runInTransaction: (entityManager: EntityManager) => Promise , ):”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.