@hile/typeorm 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/SKILL.md +110 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +62 -0
- package/package.json +32 -0
- package/skill.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# @hile/typeorm
|
|
2
|
+
|
|
3
|
+
基于 `@hile/core` 的 TypeORM 集成:将 TypeORM DataSource 封装为 Hile 服务(单例、随进程退出销毁),并提供事务封装 `transaction`。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @hile/typeorm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
依赖 `@hile/core` 与 `typeorm`,请一并安装。
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
通过环境变量配置数据库,用 `loadService` 获取 DataSource:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { loadService } from '@hile/core'
|
|
19
|
+
import typeormService from '@hile/typeorm'
|
|
20
|
+
|
|
21
|
+
const ds = await loadService(typeormService)
|
|
22
|
+
// 使用 ds.getRepository(Entity)、ds.manager 等
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 环境变量
|
|
26
|
+
|
|
27
|
+
DataSource 从以下环境变量读取配置:
|
|
28
|
+
|
|
29
|
+
| 变量 | 说明 |
|
|
30
|
+
|------|------|
|
|
31
|
+
| `TYPEORM_TYPE` | 数据库类型(如 `mysql`、`postgres`) |
|
|
32
|
+
| `TYPEORM_HOST` | 主机 |
|
|
33
|
+
| `TYPEORM_USERNAME` | 用户名 |
|
|
34
|
+
| `TYPEORM_PASSWORD` | 密码 |
|
|
35
|
+
| `TYPEORM_DATABASE` | 数据库名 |
|
|
36
|
+
| `TYPEORM_PORT` | 端口 |
|
|
37
|
+
|
|
38
|
+
行为:`synchronize: true`;当 `NODE_ENV === 'development'` 时开启 `logging`。连接在进程退出时通过 Hile 的 shutdown 自动销毁。
|
|
39
|
+
|
|
40
|
+
## 事务
|
|
41
|
+
|
|
42
|
+
使用 `transaction` 在 DataSource 上执行事务,并在失败时执行已注册的回滚逻辑(LIFO):
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { loadService } from '@hile/core'
|
|
46
|
+
import { transaction } from '@hile/typeorm'
|
|
47
|
+
import typeormService from '@hile/typeorm'
|
|
48
|
+
|
|
49
|
+
const ds = await loadService(typeormService)
|
|
50
|
+
|
|
51
|
+
const result = await transaction(ds, async (runner, rollback) => {
|
|
52
|
+
// 使用 runner 进行查询/写入
|
|
53
|
+
rollback(() => {
|
|
54
|
+
// 事务失败时执行的清理(如撤销外部副作用)
|
|
55
|
+
})
|
|
56
|
+
return value
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
- 成功:`transaction` 提交并返回回调的返回值。
|
|
61
|
+
- 失败:回滚事务并按后进先出顺序执行所有通过 `rollback(fn)` 注册的函数,再抛出原错误。
|
|
62
|
+
|
|
63
|
+
## 与 @hile/cli 一起使用
|
|
64
|
+
|
|
65
|
+
若使用 `@hile/cli` 启动应用,可在项目根 `package.json` 中配置自动加载本包默认服务:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"hile": {
|
|
70
|
+
"auto_load_packages": ["@hile/typeorm"]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
之后在业务中直接 `loadService(typeormService)` 即可,DataSource 会在应用启动时初始化、退出时销毁。
|
|
76
|
+
|
|
77
|
+
## 开发
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pnpm install
|
|
81
|
+
pnpm build # 编译
|
|
82
|
+
pnpm dev # 监听模式
|
|
83
|
+
pnpm test # 测试
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
package/SKILL.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# @hile/typeorm
|
|
2
|
+
|
|
3
|
+
本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. 架构总览
|
|
8
|
+
|
|
9
|
+
`@hile/typeorm` 在 `@hile/core` 之上提供:
|
|
10
|
+
|
|
11
|
+
- **默认导出**:一个通过 `defineService` 定义的 TypeORM **DataSource** 服务,配置来自环境变量,进程退出时通过 `shutdown` 注册 `connection.destroy()`。
|
|
12
|
+
- **transaction**:事务封装函数,接收 DataSource 与回调,在回调内提供 `QueryRunner` 与 `rollback` 注册器;提交成功则返回结果,失败则回滚并按 LIFO 执行已注册的 rollback 回调。
|
|
13
|
+
|
|
14
|
+
依赖:`@hile/core`、`typeorm`。生成代码时必须遵循 Hile 的服务定义与加载规则,并正确使用本包默认服务与 `transaction` 签名。
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 2. 类型与环境变量
|
|
19
|
+
|
|
20
|
+
### 2.1 环境变量(DataSource 默认服务)
|
|
21
|
+
|
|
22
|
+
| 变量 | 说明 |
|
|
23
|
+
|------|------|
|
|
24
|
+
| `TYPEORM_TYPE` | 数据库类型 |
|
|
25
|
+
| `TYPEORM_HOST` | 主机 |
|
|
26
|
+
| `TYPEORM_USERNAME` | 用户名 |
|
|
27
|
+
| `TYPEORM_PASSWORD` | 密码 |
|
|
28
|
+
| `TYPEORM_DATABASE` | 数据库名 |
|
|
29
|
+
| `TYPEORM_PORT` | 端口(字符串会被转为数字) |
|
|
30
|
+
|
|
31
|
+
DataSource 行为:`synchronize: true`;`logging` 在 `NODE_ENV === 'development'` 时为 `true`。
|
|
32
|
+
|
|
33
|
+
### 2.2 类型(生成代码时须遵循)
|
|
34
|
+
|
|
35
|
+
- 使用 `typeorm` 的 `DataSource`、`DataSourceOptions`、`QueryRunner`。
|
|
36
|
+
- **transaction** 签名:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
function transaction<T>(
|
|
40
|
+
datasource: DataSource,
|
|
41
|
+
callback: (
|
|
42
|
+
runner: QueryRunner,
|
|
43
|
+
rollback: (roll: () => unknown | Promise<unknown>) => number
|
|
44
|
+
) => Promise<T>
|
|
45
|
+
): Promise<T>;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- 默认服务:`defineService(async (shutdown) => { ... return connection; })`,在 `connection.initialize()` 前 `shutdown(() => connection.destroy())`。
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 3. 代码生成模板与规则
|
|
53
|
+
|
|
54
|
+
### 3.1 使用默认 DataSource 服务
|
|
55
|
+
|
|
56
|
+
**模板:**
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { loadService } from '@hile/core'
|
|
60
|
+
import typeormService from '@hile/typeorm'
|
|
61
|
+
|
|
62
|
+
const ds = await loadService(typeormService)
|
|
63
|
+
// 使用 ds 进行 TypeORM 操作
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**规则:**
|
|
67
|
+
- 仅通过 `loadService(默认导出)` 获取 DataSource,不要自行 `new DataSource` 并暴露为“全局数据源”与 Hile 并存。
|
|
68
|
+
- 环境变量在应用启动前必须配置完整,否则 DataSource 初始化可能失败。
|
|
69
|
+
|
|
70
|
+
### 3.2 在事务中执行并注册回滚
|
|
71
|
+
|
|
72
|
+
**模板:**
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { loadService } from '@hile/core'
|
|
76
|
+
import { transaction } from '@hile/typeorm'
|
|
77
|
+
import typeormService from '@hile/typeorm'
|
|
78
|
+
|
|
79
|
+
const ds = await loadService(typeormService)
|
|
80
|
+
|
|
81
|
+
const result = await transaction(ds, async (runner, rollback) => {
|
|
82
|
+
// 使用 runner 进行查询/写入
|
|
83
|
+
rollback(() => { /* 业务级回滚逻辑,失败时 LIFO 执行 */ })
|
|
84
|
+
return value
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**规则:**
|
|
89
|
+
- 第一个参数必须是 `DataSource` 实例(通常来自 `loadService(typeormService)`)。
|
|
90
|
+
- 回调内需要“事务失败时执行”的逻辑通过 `rollback(fn)` 注册,不要依赖在回调外再执行清理。
|
|
91
|
+
|
|
92
|
+
### 3.3 与 @hile/core 的约定
|
|
93
|
+
|
|
94
|
+
- 本包默认导出是 Hile 服务(`defineService`),遵循 core 的 SKILL:服务函数为 `async (shutdown)`,资源创建后立即 `shutdown(() => connection.destroy())`。
|
|
95
|
+
- 其他服务若依赖 DataSource,应在该服务函数内 `loadService(typeormService)` 获取,不要在模块顶层缓存 DataSource。
|
|
96
|
+
|
|
97
|
+
### 3.4 反模式
|
|
98
|
+
|
|
99
|
+
- **不要**在未通过 Hile 加载的情况下,多处自行 `new DataSource` 并与本包默认服务混用。
|
|
100
|
+
- **不要**在 `transaction` 回调外依赖“事务未提交”的状态做后续逻辑;提交/回滚由 `transaction` 内部统一处理。
|
|
101
|
+
- **不要**省略 `shutdown(() => connection.destroy())` 或将其放在 `initialize()` 之后才注册(应在连接初始化前注册,与源码一致)。
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 4. API 速查
|
|
106
|
+
|
|
107
|
+
| 导出 | 说明 |
|
|
108
|
+
|------|------|
|
|
109
|
+
| 默认导出 | `defineService` 返回的 DataSource 服务(需通过 `loadService` 使用) |
|
|
110
|
+
| `transaction(datasource, callback)` | 在 DataSource 上执行事务,回调接收 `(runner, rollback)`,返回 `Promise<T>` |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DataSource, QueryRunner } from 'typeorm';
|
|
2
|
+
/**
|
|
3
|
+
* 创建数据源服务
|
|
4
|
+
* 数据从环境变量中获取
|
|
5
|
+
* 环境变量:
|
|
6
|
+
* - TYPEORM_TYPE: 数据库类型
|
|
7
|
+
* - TYPEORM_HOST: 数据库主机
|
|
8
|
+
* - TYPEORM_USERNAME: 数据库用户名
|
|
9
|
+
* - TYPEORM_PASSWORD: 数据库密码
|
|
10
|
+
* - TYPEORM_DATABASE: 数据库名称
|
|
11
|
+
* - TYPEORM_PORT: 数据库端口
|
|
12
|
+
*/
|
|
13
|
+
declare const _default: import("@hile/core").ServiceRegisterProps<DataSource>;
|
|
14
|
+
export default _default;
|
|
15
|
+
/**
|
|
16
|
+
* 事务处理
|
|
17
|
+
* @param datasource - 数据源
|
|
18
|
+
* @param callback - 回调函数
|
|
19
|
+
* @returns - 返回值
|
|
20
|
+
*/
|
|
21
|
+
export declare function transaction<T>(datasource: DataSource, callback: (runner: QueryRunner, rollback: (roll: () => unknown | Promise<unknown>) => number) => Promise<T>): Promise<T>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { defineService } from '@hile/core';
|
|
2
|
+
import { DataSource } from 'typeorm';
|
|
3
|
+
/**
|
|
4
|
+
* 创建数据源服务
|
|
5
|
+
* 数据从环境变量中获取
|
|
6
|
+
* 环境变量:
|
|
7
|
+
* - TYPEORM_TYPE: 数据库类型
|
|
8
|
+
* - TYPEORM_HOST: 数据库主机
|
|
9
|
+
* - TYPEORM_USERNAME: 数据库用户名
|
|
10
|
+
* - TYPEORM_PASSWORD: 数据库密码
|
|
11
|
+
* - TYPEORM_DATABASE: 数据库名称
|
|
12
|
+
* - TYPEORM_PORT: 数据库端口
|
|
13
|
+
*/
|
|
14
|
+
export default defineService(async (shutdown) => {
|
|
15
|
+
const configs = {
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
type: process.env.TYPEORM_TYPE,
|
|
18
|
+
host: process.env.TYPEORM_HOST,
|
|
19
|
+
username: process.env.TYPEORM_USERNAME,
|
|
20
|
+
password: process.env.TYPEORM_PASSWORD,
|
|
21
|
+
database: process.env.TYPEORM_DATABASE,
|
|
22
|
+
port: typeof process.env.TYPEORM_PORT === 'string'
|
|
23
|
+
? Number(process.env.TYPEORM_PORT)
|
|
24
|
+
: process.env.TYPEORM_PORT,
|
|
25
|
+
};
|
|
26
|
+
const connection = new DataSource({
|
|
27
|
+
...configs,
|
|
28
|
+
synchronize: true,
|
|
29
|
+
logging: process.env.NODE_ENV === 'development',
|
|
30
|
+
});
|
|
31
|
+
shutdown(() => connection.destroy());
|
|
32
|
+
await connection.initialize();
|
|
33
|
+
return connection;
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* 事务处理
|
|
37
|
+
* @param datasource - 数据源
|
|
38
|
+
* @param callback - 回调函数
|
|
39
|
+
* @returns - 返回值
|
|
40
|
+
*/
|
|
41
|
+
export async function transaction(datasource, callback) {
|
|
42
|
+
const rollbacks = [];
|
|
43
|
+
const runner = datasource.createQueryRunner();
|
|
44
|
+
await runner.connect();
|
|
45
|
+
await runner.startTransaction();
|
|
46
|
+
const push = (roll) => rollbacks.push(roll);
|
|
47
|
+
try {
|
|
48
|
+
const res = await callback(runner, push);
|
|
49
|
+
await runner.commitTransaction();
|
|
50
|
+
return res;
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
await runner.rollbackTransaction();
|
|
54
|
+
let i = rollbacks.length;
|
|
55
|
+
while (i--)
|
|
56
|
+
await Promise.resolve(rollbacks[i]());
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
await runner.release();
|
|
61
|
+
}
|
|
62
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hile/typeorm",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "TypeORM DataSource as Hile service with transaction helper",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -b",
|
|
10
|
+
"dev": "tsc -b --watch",
|
|
11
|
+
"test": "vitest run"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"SKILL.md",
|
|
17
|
+
"skill.json"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^25.3.1",
|
|
25
|
+
"vitest": "^4.0.18"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@hile/core": "1.0.11",
|
|
29
|
+
"typeorm": "^0.3.28"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "75eb1011ddecf2b85dadb584dd3876c8b65bfb03"
|
|
32
|
+
}
|
package/skill.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hile/typeorm",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Code generation and contribution rules for @hile/typeorm (TypeORM DataSource as Hile service). Use when editing this package or when the user asks about @hile/typeorm, DataSource, or transaction patterns.",
|
|
5
|
+
"repository": "",
|
|
6
|
+
"skills": [
|
|
7
|
+
{
|
|
8
|
+
"id": "typeorm",
|
|
9
|
+
"name": "@hile/typeorm 开发指南",
|
|
10
|
+
"description": "提供 @hile/typeorm 模块的开发与代码生成规范:DataSource 服务、环境变量、事务封装",
|
|
11
|
+
"path": "SKILL.md"
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"authors": [],
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|