@celljs/typeorm 4.6.1 → 4.8.0

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.
Files changed (2) hide show
  1. package/README.zh-cn.md +114 -0
  2. package/package.json +3 -3
@@ -0,0 +1,114 @@
1
+ # Cell - typeorm 组件
2
+
3
+ [TypeORM](https://typeorm.io) 数据源、仓库和 `@Transactional()` 用于 Cell 后端。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @celljs/typeorm mysql2
9
+ ```
10
+
11
+ 数据库驱动程序不是此包的依赖:安装你的数据源使用的那个(`mysql2`、`pg`、`better-sqlite3`、`sql.js` 等)。`mysql2` 是一个可选的对等依赖;当配置的数据源的驱动程序缺失时,应用程序在启动时失败,显示 `CELL_TYPEORM_DRIVER_MISSING` 和安装它的命令。
12
+
13
+ ## 配置数据源
14
+
15
+ `typeorm.ormConfig` 是一个 TypeORM `DataSourceOptions` 的列表(或一个对象)。默认值来自此包的 `cell.yml`,从环境中读取:
16
+
17
+ | 变量 | 默认值 |
18
+ | --- | --- |
19
+ | `DB_TYPE` | `mysql` |
20
+ | `DB_HOST` | `localhost` |
21
+ | `DB_PORT` | `3306` |
22
+ | `DB_SYNC` | `true`(仅 `'true'` 启用 `synchronize`) |
23
+ | `DB_DATABASE` | `test` |
24
+
25
+ 在 `cell.yml` 中写你自己的;配置仅对服务器有效,因此凭证留在服务器上:
26
+
27
+ ```yaml
28
+ typeorm:
29
+ ormConfig:
30
+ - type: postgres
31
+ host: "${env.DB_HOST ?? 'localhost'}"
32
+ port: "${ (env.DB_PORT ?? 5432) * 1 }"
33
+ username: "${env.DB_USER}"
34
+ password: "${env.DB_PASSWORD}"
35
+ database: app
36
+ synchronize: false
37
+ ```
38
+
39
+ 按环境覆盖单个值,使用 `CELL_TYPEORM__ORM_CONFIG__0__HOST=db.internal`。用于测试,`cell.test.yml` 可以切换到 SQLite 内存(`type: sqljs` 和 `sql.js` 包)。数据源在应用程序启动时创建,在停止时销毁。
40
+
41
+ ## 实体
42
+
43
+ 在 DI 模块中注册实体类;数据源的 `entities` 从它们填充:
44
+
45
+ ```ts
46
+ // src/node/module.ts
47
+ import './todo-controller';
48
+ import { autoBind } from '@celljs/core';
49
+ import { autoBindEntities } from '@celljs/typeorm';
50
+ import { Todo } from './todo';
51
+
52
+ autoBindEntities({ Todo }); // 一个对象或类数组
53
+ export default autoBind();
54
+ ```
55
+
56
+ ```ts
57
+ // src/node/todo.ts
58
+ import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
59
+
60
+ @Entity()
61
+ export class Todo {
62
+ @PrimaryGeneratedColumn()
63
+ id: number;
64
+
65
+ @Column()
66
+ title: string;
67
+
68
+ @Column({ default: false })
69
+ done: boolean;
70
+ }
71
+ ```
72
+
73
+ ## 仓库和事务
74
+
75
+ ```ts
76
+ import { Body, Controller, Get, Post } from '@celljs/mvc/lib/node';
77
+ import { OrmContext, Transactional } from '@celljs/typeorm/lib/node';
78
+ import { Todo } from './todo';
79
+
80
+ @Controller('todos')
81
+ export class TodoController {
82
+ @Get()
83
+ @Transactional({ readOnly: true })
84
+ list(): Promise<Todo[]> {
85
+ return OrmContext.getRepository(Todo).find({ order: { id: 'ASC' } });
86
+ }
87
+
88
+ @Post()
89
+ @Transactional()
90
+ create(@Body() body: { title: string }): Promise<Todo> {
91
+ return OrmContext.getRepository(Todo).save(Object.assign(new Todo(), { title: body.title }));
92
+ }
93
+ }
94
+ ```
95
+
96
+ - `@Transactional()` 在事务中运行方法:返回时提交,抛出时回滚。在其中,`OrmContext.getRepository()`、`getTreeRepository()`、`getMongoRepository()` 和 `getEntityManager()` 使用事务的实体管理器,也在方法调用的服务中。
97
+ - `readOnly: true` 使用数据源的管理器而不是事务。`isolation` 设置隔离级别。
98
+ - `propagation`:`Propagation.Required`(默认)加入已活跃的事务;`Propagation.RequiresNew` 始终启动一个。与数据源名称一起传递:`@Transactional({ name: 'default', propagation: Propagation.RequiresNew })`。
99
+ - 它在任何组件方法上工作,在请求外(它创建它需要的上下文)。
100
+
101
+ ## 多个数据源
102
+
103
+ 给 `ormConfig` 的每个条目一个 `name`;没有条目的是 `default`。然后在所有地方命名它:
104
+
105
+ ```ts
106
+ autoBindEntities([AuditLog], 'audit');
107
+
108
+ @Transactional('audit')
109
+ async record() {
110
+ await OrmContext.getRepository(AuditLog, 'audit').save(...);
111
+ }
112
+ ```
113
+
114
+ `DataSourceManager`(`@celljs/typeorm`)给出 TypeORM `DataSource` 对象本身。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/typeorm",
3
- "version": "4.6.1",
3
+ "version": "4.8.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -33,8 +33,8 @@
33
33
  "watch": "tsc -b -w --preserveWatchOutput"
34
34
  },
35
35
  "dependencies": {
36
- "@celljs/core": "4.6.1",
37
- "@celljs/kit": "4.6.1",
36
+ "@celljs/core": "4.8.0",
37
+ "@celljs/kit": "4.8.0",
38
38
  "typeorm": "^0.3.31"
39
39
  },
40
40
  "peerDependencies": {