TypeORM 支持多种数据库,包括:
- MySQL / MariaDB:最常用的关系型数据库
- PostgreSQL:功能强大的开源数据库
- SQLite:轻量级嵌入式数据库
- Microsoft SQL Server:微软的数据库系统
- Oracle:企业级数据库
- MongoDB:NoSQL 文档数据库(通过 TypeORM 的 MongoDB 支持)
- CockroachDB:分布式 SQL 数据库
配置不同数据库的示例:
typescript// MySQL new DataSource({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'password', database: 'test' }); // PostgreSQL new DataSource({ type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'password', database: 'test' }); // SQLite new DataSource({ type: 'sqlite', database: './database.sqlite' });
TypeORM 的优势在于统一的 API,使得在不同数据库之间切换变得非常简单。