TypeORM 开源项目教程
1. 项目的目录结构及介绍
TypeORM 项目的目录结构如下:
typeorm/
├── src/
│ ├── commands/
│ ├── driver/
│ ├── entity-schema/
│ ├── errors/
│ ├── event-emitter/
│ ├── find-options/
│ ├── metadata/
│ ├── migration/
│ ├── platform/
│ ├── query-builder/
│ ├── repository/
│ ├── schema-builder/
│ ├── subscriber/
│ ├── transaction/
│ ├── util/
│ ├── connection/
│ ├── index.ts
│ ├── logger.ts
│ ├── typeorm-utils.ts
│ └── ...
├── test/
├── scripts/
├── ormconfig.json
├── package.json
├── tsconfig.json
└── README.md
目录结构介绍
-
src/
: 包含 TypeORM 的核心源代码。commands/
: 包含 CLI 命令的实现。driver/
: 包含不同数据库驱动程序的实现。entity-schema/
: 包含实体 schema 的相关代码。errors/
: 包含自定义错误类。event-emitter/
: 包含事件发射器的实现。find-options/
: 包含查询选项的相关代码。metadata/
: 包含元数据处理的相关代码。migration/
: 包含迁移相关的代码。platform/
: 包含平台相关的代码。query-builder/
: 包含查询构建器的实现。repository/
: 包含仓库模式的实现。schema-builder/
: 包含 schema 构建器的实现。subscriber/
: 包含事件订阅者的实现。transaction/
: 包含事务管理的实现。util/
: 包含各种工具函数。connection/
: 包含连接管理的相关代码。index.ts
: 入口文件。logger.ts
: 日志记录器的实现。typeorm-utils.ts
: 包含 TypeORM 的工具函数。
-
test/
: 包含测试代码。 -
scripts/
: 包含一些脚本文件。 -
ormconfig.json
: 配置文件,用于配置数据库连接等。 -
package.json
: 项目的依赖管理文件。 -
tsconfig.json
: TypeScript 配置文件。 -
README.md
: 项目说明文档。
2. 项目的启动文件介绍
TypeORM 的启动文件主要是 src/index.ts
。这个文件是项目的入口点,负责初始化和导出 TypeORM 的核心功能。
启动文件内容
// src/index.ts
export * from "./connection/Connection";
export * from "./connection/ConnectionOptions";
export * from "./entity-manager/EntityManager";
export * from "./repository/Repository";
export * from "./query-builder/QueryBuilder";
export * from "./driver/Driver";
export * from "./metadata/MetadataArgsStorage";
export * from "./migration/Migration";
export * from "./subscriber/EventSubscriber";
export * from "./transaction/Transaction";
export * from "./logger/Logger";
export * from "./util/Util";
// 其他导出...
启动文件功能
- 导出 TypeORM 的核心类和函数,如
Connection
,EntityManager
,Repository
等。 - 初始化 TypeORM 的元数据存储和配置。
3. 项目的配置文件介绍
TypeORM 的配置文件主要是 ormconfig.json
。这个文件用于配置数据库连接和其他相关设置。
配置文件内容
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir