Scripts2026年4月16日·1 分钟阅读

TypeORM — TypeScript & JavaScript ORM for Node.js

A full-featured ORM supporting PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, and Oracle with decorators, migrations, and Active Record or Data Mapper patterns.

Introduction

TypeORM is a full-featured ORM that runs in Node.js, Deno, and the browser. It draws heavy inspiration from Hibernate and Entity Framework, letting you define database schemas with TypeScript decorators and manage data with both Active Record and Data Mapper patterns.

What TypeORM Does

  • Maps TypeScript/JavaScript classes to database tables using decorators
  • Supports PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Oracle, and CockroachDB
  • Provides automatic migration generation from entity changes
  • Handles complex relations: one-to-one, one-to-many, many-to-many with eager/lazy loading
  • Offers a powerful QueryBuilder for advanced SQL without raw strings

Architecture Overview

TypeORM revolves around a DataSource that manages connection pooling and metadata. Entity decorators annotate classes, which a metadata builder converts into table schemas. The EntityManager coordinates persistence operations, while the QueryBuilder compiles a fluent API into parameterized SQL. Migrations are generated by diffing entity metadata against the current database state.

Self-Hosting & Configuration

  • Install via npm alongside a database driver (pg, mysql2, better-sqlite3, etc.)
  • Configure DataSource with host, port, credentials, and entity paths in a data-source.ts file
  • Enable synchronize: true for development to auto-create tables; use migrations in production
  • Set logging: true to see generated SQL; supports custom loggers
  • Use environment variables or ormconfig.json for multi-environment configuration

Key Features

  • Decorator-based entity definitions with full TypeScript type safety
  • Built-in migration CLI for schema versioning
  • Transaction support with manual and automatic strategies
  • Subscriber and listener hooks for entity lifecycle events
  • Query caching with pluggable cache providers (Redis, database)

Comparison with Similar Tools

  • Prisma — schema-first with generated client; TypeORM is code-first with decorators
  • Drizzle ORM — lighter, SQL-like syntax; TypeORM offers richer Active Record patterns
  • Sequelize — similar feature set but JavaScript-first; TypeORM has stronger TypeScript integration
  • MikroORM — identity map and unit of work focus; TypeORM has a larger ecosystem
  • Knex.js — query builder only; TypeORM adds full entity mapping and migrations

FAQ

Q: Should I use Active Record or Data Mapper pattern? A: Active Record is simpler for small projects. Data Mapper offers better separation of concerns and testability for larger applications.

Q: Is synchronize safe for production? A: No. It can drop columns and data. Always use migrations in production environments.

Q: Does TypeORM support MongoDB? A: Yes, but MongoDB support is experimental and less mature than relational database support.

Q: How do I handle connection pooling? A: TypeORM uses the underlying driver pool. Configure pool size via the extra option in DataSource settings.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产