@eggjs/dal-plugin 4.0.0-beta.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.
- package/LICENSE +21 -0
- package/README.md +693 -0
- package/dist/app.d.ts +10 -0
- package/dist/app.js +38 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +10 -0
- package/dist/lib/DalModuleLoadUnitHook.d.ts +10 -0
- package/dist/lib/DalModuleLoadUnitHook.js +42 -0
- package/dist/lib/DalTableEggPrototypeHook.d.ts +8 -0
- package/dist/lib/DalTableEggPrototypeHook.js +22 -0
- package/dist/lib/DataSource.d.ts +8 -0
- package/dist/lib/DataSource.js +83 -0
- package/dist/lib/MysqlDataSourceManager.d.ts +11 -0
- package/dist/lib/MysqlDataSourceManager.js +53 -0
- package/dist/lib/SqlMapManager.d.ts +9 -0
- package/dist/lib/SqlMapManager.js +22 -0
- package/dist/lib/TableModelManager.d.ts +9 -0
- package/dist/lib/TableModelManager.js +22 -0
- package/dist/lib/TransactionPrototypeHook.d.ts +9 -0
- package/dist/lib/TransactionPrototypeHook.js +55 -0
- package/dist/lib/TransactionalAOP.d.ts +11 -0
- package/dist/lib/TransactionalAOP.js +28 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +2 -0
- package/package.json +94 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
# @eggjs/tegg-dal-plugin
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
5
|
+
[![npm download][download-image]][download-url]
|
|
6
|
+
[](https://nodejs.org/en/download/)
|
|
7
|
+
|
|
8
|
+
[npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-dal-plugin.svg?style=flat-square
|
|
9
|
+
[npm-url]: https://npmjs.org/package/@eggjs/tegg-dal-plugin
|
|
10
|
+
[snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-dal-plugin/badge.svg?style=flat-square
|
|
11
|
+
[snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-dal-plugin
|
|
12
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-dal-plugin.svg?style=flat-square
|
|
13
|
+
[download-url]: https://npmjs.org/package/@eggjs/tegg-dal-plugin
|
|
14
|
+
|
|
15
|
+
@eggjs/tegg-dal-plugin 支持使用注解的方式来开发 egg 中的 dal。
|
|
16
|
+
|
|
17
|
+
## egg 模式
|
|
18
|
+
|
|
19
|
+
### Install
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
# tegg 注解
|
|
23
|
+
npm i --save @eggjs/tegg
|
|
24
|
+
# tegg 插件
|
|
25
|
+
npm i --save @eggjs/tegg-plugin
|
|
26
|
+
# tegg dal 插件
|
|
27
|
+
npm i --save @eggjs/tegg-dal-plugin
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Prepare
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
// tsconfig.json
|
|
34
|
+
{
|
|
35
|
+
"extends": "@eggjs/tsconfig"
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Config
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
// config/plugin.js
|
|
43
|
+
exports.tegg = {
|
|
44
|
+
package: '@eggjs/tegg-plugin',
|
|
45
|
+
enable: true,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.teggDal = {
|
|
49
|
+
package: '@eggjs/tegg-dal-plugin',
|
|
50
|
+
enable: true,
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## standalone 模式
|
|
55
|
+
|
|
56
|
+
### Install
|
|
57
|
+
|
|
58
|
+
```shell
|
|
59
|
+
# tegg 注解
|
|
60
|
+
npm i --save @eggjs/tegg
|
|
61
|
+
# tegg dal 插件
|
|
62
|
+
npm i --save @eggjs/tegg-dal-plugin
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Prepare
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
// tsconfig.json
|
|
69
|
+
{
|
|
70
|
+
"extends": "@eggjs/tsconfig"
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
### module.yml
|
|
77
|
+
|
|
78
|
+
通过 module.yml 来配置 module 中的 mysql 数据源。
|
|
79
|
+
|
|
80
|
+
```yaml
|
|
81
|
+
dataSource:
|
|
82
|
+
# 数据源名称,可以在 @Table 注解中指定
|
|
83
|
+
# 如果 module 中只有一个 dataSource,@Table 会默认使用这个数据源
|
|
84
|
+
foo:
|
|
85
|
+
connectionLimit: 100
|
|
86
|
+
database: 'test'
|
|
87
|
+
host: '127.0.0.1'
|
|
88
|
+
user: root
|
|
89
|
+
port: 3306
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Table
|
|
93
|
+
|
|
94
|
+
`TableModel` 定义一个表结构,包括表配置、列、索引。
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { Table, Index, Column, ColumnType, IndexType } from '@eggjs/tegg/dal';
|
|
98
|
+
|
|
99
|
+
// 定义了一个表
|
|
100
|
+
@Table({
|
|
101
|
+
comment: 'foo table',
|
|
102
|
+
})
|
|
103
|
+
// 定义了一个唯一索引,列是 name
|
|
104
|
+
@Index({
|
|
105
|
+
keys: ['name'],
|
|
106
|
+
type: IndexType.UNIQUE,
|
|
107
|
+
})
|
|
108
|
+
export class Foo {
|
|
109
|
+
// 定义了主键,类型是 int
|
|
110
|
+
@Column(
|
|
111
|
+
{
|
|
112
|
+
type: ColumnType.INT,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
primaryKey: true,
|
|
116
|
+
}
|
|
117
|
+
)
|
|
118
|
+
id: number;
|
|
119
|
+
|
|
120
|
+
// 定义了 name 列,类型是 varchar
|
|
121
|
+
@Column({
|
|
122
|
+
type: ColumnType.VARCHAR,
|
|
123
|
+
length: 100,
|
|
124
|
+
})
|
|
125
|
+
name: string;
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
详细参数定义如下,具体参数值可以参考 https://dev.mysql.com/doc/refman/8.0/en/create-table.html
|
|
130
|
+
|
|
131
|
+
建表参数,使用方式为 `@Table(parmas?: TableParams)`
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
export interface TableParams {
|
|
135
|
+
// 数据库表名
|
|
136
|
+
name?: string;
|
|
137
|
+
// 数据源名称,如果 module 只有一个 dataSource 则默认使用这个
|
|
138
|
+
dataSourceName?: string;
|
|
139
|
+
comment?: string;
|
|
140
|
+
autoExtendSize?: number;
|
|
141
|
+
autoIncrement?: number;
|
|
142
|
+
avgRowLength?: number;
|
|
143
|
+
characterSet?: string;
|
|
144
|
+
collate?: string;
|
|
145
|
+
compression?: CompressionType;
|
|
146
|
+
encryption?: boolean;
|
|
147
|
+
engine?: string;
|
|
148
|
+
engineAttribute?: string;
|
|
149
|
+
insertMethod?: InsertMethod;
|
|
150
|
+
keyBlockSize?: number;
|
|
151
|
+
maxRows?: number;
|
|
152
|
+
minRows?: number;
|
|
153
|
+
rowFormat?: RowFormat;
|
|
154
|
+
secondaryEngineAttribute?: string;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
建索引参数,使用方式为 `@Index(parmas?: IndexParams)`
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
export interface IndexParams {
|
|
162
|
+
// 索引的列
|
|
163
|
+
keys: string[];
|
|
164
|
+
// 索引名称,如果未指定会用 列名拼接
|
|
165
|
+
// 如 [column1, column2 ]
|
|
166
|
+
// 普通索引为 idx_column1_column2
|
|
167
|
+
// 唯一索引为 uk_column1_column2
|
|
168
|
+
name?: string;
|
|
169
|
+
type?: IndexType;
|
|
170
|
+
storeType?: IndexStoreType;
|
|
171
|
+
comment?: string;
|
|
172
|
+
engineAttribute?: string;
|
|
173
|
+
secondaryEngineAttribute?: string;
|
|
174
|
+
parser?: string;
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
建列参数,使用方式为 `@Column(type: ColumnTypeParams, parmas?: ColumnParams)`
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
export interface ColumnParams {
|
|
182
|
+
// 列名,默认转换规则 userName 至 user_name
|
|
183
|
+
name?: string;
|
|
184
|
+
// 默认值
|
|
185
|
+
default?: string;
|
|
186
|
+
// 是否可控,默认为 false
|
|
187
|
+
canNull?: boolean;
|
|
188
|
+
comment?: string;
|
|
189
|
+
visible?: boolean;
|
|
190
|
+
autoIncrement?: boolean;
|
|
191
|
+
uniqueKey?: boolean;
|
|
192
|
+
primaryKey?: boolean;
|
|
193
|
+
collate?: string;
|
|
194
|
+
columnFormat?: ColumnFormat;
|
|
195
|
+
engineAttribute?: string;
|
|
196
|
+
secondaryEngineAttribute?: string;
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
支持的类型
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
export enum ColumnType {
|
|
204
|
+
// Numeric
|
|
205
|
+
BIT = 'BIT',
|
|
206
|
+
TINYINT = 'TINYINT',
|
|
207
|
+
BOOL = 'BOOL',
|
|
208
|
+
SMALLINT = 'SMALLINT',
|
|
209
|
+
MEDIUMINT = 'MEDIUMINT',
|
|
210
|
+
INT = 'INT',
|
|
211
|
+
BIGINT = 'BIGINT',
|
|
212
|
+
DECIMAL = 'DECIMAL',
|
|
213
|
+
FLOAT = 'FLOAT',
|
|
214
|
+
DOUBLE = 'DOUBLE',
|
|
215
|
+
// Date
|
|
216
|
+
DATE = 'DATE',
|
|
217
|
+
DATETIME = 'DATETIME',
|
|
218
|
+
TIMESTAMP = 'TIMESTAMP',
|
|
219
|
+
TIME = 'TIME',
|
|
220
|
+
YEAR = 'YEAR',
|
|
221
|
+
// String
|
|
222
|
+
CHAR = 'CHAR',
|
|
223
|
+
VARCHAR = 'VARCHAR',
|
|
224
|
+
BINARY = 'BINARY',
|
|
225
|
+
VARBINARY = 'VARBINARY',
|
|
226
|
+
TINYBLOB = 'TINYBLOB',
|
|
227
|
+
TINYTEXT = 'TINYTEXT',
|
|
228
|
+
BLOB = 'BLOB',
|
|
229
|
+
TEXT = 'TEXT',
|
|
230
|
+
MEDIUMBLOB = 'MEDIUMBLOB',
|
|
231
|
+
MEDIUMTEXT = 'MEDIUMTEXT',
|
|
232
|
+
LONGBLOB = 'LONGBLOB',
|
|
233
|
+
LONGTEXT = 'LONGTEXT',
|
|
234
|
+
ENUM = 'ENUM',
|
|
235
|
+
SET = 'SET',
|
|
236
|
+
// JSON
|
|
237
|
+
JSON = 'JSON',
|
|
238
|
+
// Spatial
|
|
239
|
+
GEOMETRY = 'GEOMETRY',
|
|
240
|
+
POINT = 'POINT',
|
|
241
|
+
LINESTRING = 'LINESTRING',
|
|
242
|
+
POLYGON = 'POLYGON',
|
|
243
|
+
MULTIPOINT = 'MULTIPOINT',
|
|
244
|
+
MULTILINESTRING = 'MULTILINESTRING',
|
|
245
|
+
MULTIPOLYGON = 'MULTIPOLYGON',
|
|
246
|
+
GEOMETRYCOLLECTION = 'GEOMETRYCOLLECTION',
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
支持的类型参数,详细可参考 https://dev.mysql.com/doc/refman/8.0/en/data-types.html
|
|
251
|
+
|
|
252
|
+
如果 mysql 类型和 ts 类型对应关系不确定可直接使用 `ColumnTsType` 类型,如
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
import { Table, Index, Column, ColumnType, IndexType, ColumnTsType } from '@eggjs/tegg/dal';
|
|
256
|
+
|
|
257
|
+
// 定义了一个表
|
|
258
|
+
@Table({
|
|
259
|
+
comment: 'foo table',
|
|
260
|
+
})
|
|
261
|
+
// 定义了一个唯一索引,列是 name
|
|
262
|
+
@Index({
|
|
263
|
+
keys: ['name'],
|
|
264
|
+
type: IndexType.UNIQUE,
|
|
265
|
+
})
|
|
266
|
+
export class Foo {
|
|
267
|
+
// 定义了主键,类型是 int
|
|
268
|
+
@Column(
|
|
269
|
+
{
|
|
270
|
+
type: ColumnType.INT,
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
primaryKey: true,
|
|
274
|
+
}
|
|
275
|
+
)
|
|
276
|
+
id: ColumnTsType['INT'];
|
|
277
|
+
|
|
278
|
+
// 定义了 name 列,类型是 varchar
|
|
279
|
+
@Column({
|
|
280
|
+
type: ColumnType.VARCHAR,
|
|
281
|
+
length: 100,
|
|
282
|
+
})
|
|
283
|
+
name: ColumnTsType['VARCHAR'];
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
// Bit 类型,对应 js 中的 Buffer
|
|
289
|
+
export interface BitParams {
|
|
290
|
+
type: ColumnType.BIT;
|
|
291
|
+
// Bit 长度
|
|
292
|
+
length?: number;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Bool 类型,注意在 js 中需要使用 0 或者 1
|
|
296
|
+
export interface BoolParams {
|
|
297
|
+
type: ColumnType.BOOL;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// TinyInt 类型,对应 js 中的 number
|
|
301
|
+
export interface TinyIntParams {
|
|
302
|
+
type: ColumnType.TINYINT;
|
|
303
|
+
length?: number;
|
|
304
|
+
unsigned?: boolean;
|
|
305
|
+
zeroFill?: boolean;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// SmallInt 类型,对应 js 中的 number
|
|
309
|
+
export interface SmallIntParams {
|
|
310
|
+
type: ColumnType.SMALLINT;
|
|
311
|
+
length?: number;
|
|
312
|
+
unsigned?: boolean;
|
|
313
|
+
zeroFill?: boolean;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// MediumInt 类型,对应 js 中的 number
|
|
317
|
+
export interface MediumIntParams {
|
|
318
|
+
type: ColumnType.MEDIUMINT;
|
|
319
|
+
length?: number;
|
|
320
|
+
unsigned?: boolean;
|
|
321
|
+
zeroFill?: boolean;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// MediumInt 类型,对应 js 中的 number
|
|
325
|
+
export interface IntParams {
|
|
326
|
+
type: ColumnType.INT;
|
|
327
|
+
length?: number;
|
|
328
|
+
unsigned?: boolean;
|
|
329
|
+
zeroFill?: boolean;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// BigInt 类型,对应 js 中的 string
|
|
333
|
+
export interface BigIntParams {
|
|
334
|
+
type: ColumnType.BIGINT;
|
|
335
|
+
length?: number;
|
|
336
|
+
unsigned?: boolean;
|
|
337
|
+
zeroFill?: boolean;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Decimal 类型,对应 js 中的 string
|
|
341
|
+
export interface DecimalParams {
|
|
342
|
+
type: ColumnType.DECIMAL;
|
|
343
|
+
length?: number;
|
|
344
|
+
fractionalLength?: number;
|
|
345
|
+
unsigned?: boolean;
|
|
346
|
+
zeroFill?: boolean;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Float 类型,对应 js 中的 number
|
|
350
|
+
export interface FloatParams {
|
|
351
|
+
type: ColumnType.FLOAT;
|
|
352
|
+
length?: number;
|
|
353
|
+
fractionalLength?: number;
|
|
354
|
+
unsigned?: boolean;
|
|
355
|
+
zeroFill?: boolean;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Double 类型,对应 js 中的 number
|
|
359
|
+
export interface DoubleParams {
|
|
360
|
+
type: ColumnType.DOUBLE;
|
|
361
|
+
length?: number;
|
|
362
|
+
fractionalLength?: number;
|
|
363
|
+
unsigned?: boolean;
|
|
364
|
+
zeroFill?: boolean;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Date 类型,对应 js 中的 Date
|
|
368
|
+
export interface DateParams {
|
|
369
|
+
type: ColumnType.DATE;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// DateTime 类型,对应 js 中的 Date
|
|
373
|
+
export interface DateTimeParams {
|
|
374
|
+
type: ColumnType.DATETIME;
|
|
375
|
+
precision?: number;
|
|
376
|
+
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
|
|
377
|
+
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
|
|
378
|
+
autoUpdate?: boolean;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Timestamp 类型,对应 js 中的 Date
|
|
382
|
+
export interface TimestampParams {
|
|
383
|
+
type: ColumnType.TIMESTAMP;
|
|
384
|
+
precision?: number;
|
|
385
|
+
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
|
|
386
|
+
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
|
|
387
|
+
autoUpdate?: boolean;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Times 类型,对应 js 中的 string
|
|
391
|
+
export interface TimeParams {
|
|
392
|
+
type: ColumnType.TIME;
|
|
393
|
+
precision?: number;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Year 类型,对应 js 中的 number
|
|
397
|
+
export interface YearParams {
|
|
398
|
+
type: ColumnType.YEAR;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Char 类型,对应 js 中的 string
|
|
402
|
+
export interface CharParams {
|
|
403
|
+
type: ColumnType.CHAR;
|
|
404
|
+
length?: number;
|
|
405
|
+
characterSet?: string;
|
|
406
|
+
collate?: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// VarChar 类型,对应 js 中的 string
|
|
410
|
+
export interface VarCharParams {
|
|
411
|
+
type: ColumnType.VARCHAR;
|
|
412
|
+
length: number;
|
|
413
|
+
characterSet?: string;
|
|
414
|
+
collate?: string;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Binary 类型,对应 js 中的 Buffer
|
|
418
|
+
export interface BinaryParams {
|
|
419
|
+
type: ColumnType.BINARY;
|
|
420
|
+
length?: number;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// VarBinary 类型,对应 js 中的 Buffer
|
|
424
|
+
export interface VarBinaryParams {
|
|
425
|
+
type: ColumnType.VARBINARY;
|
|
426
|
+
length: number;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// TinyBlob 类型,对应 js 中的 Buffer
|
|
430
|
+
export interface TinyBlobParams {
|
|
431
|
+
type: ColumnType.TINYBLOB;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// TinyText 类型,对应 js 中的 string
|
|
435
|
+
export interface TinyTextParams {
|
|
436
|
+
type: ColumnType.TINYTEXT;
|
|
437
|
+
characterSet?: string;
|
|
438
|
+
collate?: string;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Blob 类型,对应 js 中的 Buffer
|
|
442
|
+
export interface BlobParams {
|
|
443
|
+
type: ColumnType.BLOB;
|
|
444
|
+
length?: number;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Text 类型,对应 js 中的 string
|
|
448
|
+
export interface TextParams {
|
|
449
|
+
type: ColumnType.TEXT;
|
|
450
|
+
length?: number;
|
|
451
|
+
characterSet?: string;
|
|
452
|
+
collate?: string;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// MediumBlob 类型,对应 js 中的 Buffer
|
|
456
|
+
export interface MediumBlobParams {
|
|
457
|
+
type: ColumnType.MEDIUMBLOB;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// LongBlob 类型,对应 js 中的 Buffer
|
|
461
|
+
export interface LongBlobParams {
|
|
462
|
+
type: ColumnType.LONGBLOB;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// MediumText 类型,对应 js 中的 string
|
|
466
|
+
export interface MediumTextParams {
|
|
467
|
+
type: ColumnType.MEDIUMTEXT;
|
|
468
|
+
characterSet?: string;
|
|
469
|
+
collate?: string;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// LongText 类型,对应 js 中的 string
|
|
473
|
+
export interface LongTextParams {
|
|
474
|
+
type: ColumnType.LONGTEXT;
|
|
475
|
+
characterSet?: string;
|
|
476
|
+
collate?: string;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Enum 类型,对应 js 中的 string
|
|
480
|
+
export interface EnumParams {
|
|
481
|
+
type: ColumnType.ENUM;
|
|
482
|
+
enums: string[];
|
|
483
|
+
characterSet?: string;
|
|
484
|
+
collate?: string;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Set 类型,对应 js 中的 string
|
|
488
|
+
export interface SetParams {
|
|
489
|
+
type: ColumnType.SET;
|
|
490
|
+
enums: string[];
|
|
491
|
+
characterSet?: string;
|
|
492
|
+
collate?: string;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Json 类型,对应 js 中的 Object
|
|
496
|
+
export interface JsonParams {
|
|
497
|
+
type: ColumnType.JSON;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Gemotry 类型,对应 Point, Line, Polygon
|
|
501
|
+
export interface GeometryParams {
|
|
502
|
+
type: ColumnType.GEOMETRY;
|
|
503
|
+
SRID?: number;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export interface PointParams {
|
|
507
|
+
type: ColumnType.POINT;
|
|
508
|
+
SRID?: number;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export interface LinestringParams {
|
|
512
|
+
type: ColumnType.LINESTRING;
|
|
513
|
+
SRID?: number;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export interface PolygonParams {
|
|
517
|
+
type: ColumnType.POLYGON;
|
|
518
|
+
SRID?: number;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export interface MultiPointParams {
|
|
522
|
+
type: ColumnType.MULTIPOINT;
|
|
523
|
+
SRID?: number;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export interface MultiLinestringParams {
|
|
527
|
+
type: ColumnType.MULTILINESTRING;
|
|
528
|
+
SRID?: number;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export interface MultiPolygonParams {
|
|
532
|
+
type: ColumnType.MULTIPOLYGON;
|
|
533
|
+
SRID?: number;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// GeometryCollection 对应 Array<Point | Line | Ploygon>
|
|
537
|
+
export interface GeometryCollectionParams {
|
|
538
|
+
type: ColumnType.GEOMETRYCOLLECTION;
|
|
539
|
+
SRID?: number;
|
|
540
|
+
}
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### 目录结构
|
|
544
|
+
|
|
545
|
+
运行 `egg-bin dal gen` 即可生成 `dal` 相关目录,包括 dao、extension、structure
|
|
546
|
+
|
|
547
|
+
```plain
|
|
548
|
+
dal
|
|
549
|
+
├── dao
|
|
550
|
+
│ ├── FooDAO.ts
|
|
551
|
+
│ └── base
|
|
552
|
+
│ └── BaseFooDAO.ts
|
|
553
|
+
├── extension
|
|
554
|
+
│ └── FooExtension.ts
|
|
555
|
+
└── structure
|
|
556
|
+
├── Foo.json
|
|
557
|
+
└── Foo.sql
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
- dao: 表访问类,生成的 BaseDAO 请勿修改,其中包含了根据表结构生成的基础访问方法,如 insert/update/delete 以及根据索引信息生成的 find 方法
|
|
561
|
+
- extension: 扩展文件,如果需要自定义 sql,需要在 extension 文件中定义
|
|
562
|
+
- structure: 建表语句以及表结构
|
|
563
|
+
|
|
564
|
+
### DAO
|
|
565
|
+
|
|
566
|
+
注入 DAO 即可实现对表的访问
|
|
567
|
+
|
|
568
|
+
```ts
|
|
569
|
+
import { SingletonProto, Inject } from '@eggjs/tegg';
|
|
570
|
+
|
|
571
|
+
@SingletonProto()
|
|
572
|
+
export class FooRepository {
|
|
573
|
+
@Inject()
|
|
574
|
+
private readonly fooDAO: FooDAO;
|
|
575
|
+
|
|
576
|
+
async create(foo: Foo) {
|
|
577
|
+
await this.fooDAO.insert(foo);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
#### 自定义 SQL
|
|
583
|
+
|
|
584
|
+
1. 在 extension 中定义自定义 SQL
|
|
585
|
+
|
|
586
|
+
```ts
|
|
587
|
+
// dal/extension/FooExtension.ts
|
|
588
|
+
import { type SqlMap, SqlType } from '@eggjs/tegg/dal';
|
|
589
|
+
|
|
590
|
+
export default {
|
|
591
|
+
findByName: {
|
|
592
|
+
type: SqlType.SELECT,
|
|
593
|
+
sql: 'SELECT {{ allColumns }} FROM egg_foo WHERE name = {{ name }}',
|
|
594
|
+
},
|
|
595
|
+
} as Record<string, SqlMap>;
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
2. 在 dao 中定义自定义方法
|
|
599
|
+
|
|
600
|
+
```ts
|
|
601
|
+
import { SingletonProto, AccessLevel } from '@eggjs/tegg';
|
|
602
|
+
import { BaseFooDAO } from './base/BaseFooDAO';
|
|
603
|
+
import { Foo } from '../../Foo';
|
|
604
|
+
|
|
605
|
+
@SingletonProto({
|
|
606
|
+
accessLevel: AccessLevel.PUBLIC,
|
|
607
|
+
})
|
|
608
|
+
export default class FooDAO extends BaseFooDAO {
|
|
609
|
+
async findByName(name: string): Promise<Foo[]> {
|
|
610
|
+
return this.dataSource.execute('findByName', {
|
|
611
|
+
name,
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
支持的自定义 filter
|
|
618
|
+
|
|
619
|
+
```
|
|
620
|
+
- toPoint
|
|
621
|
+
- toLine
|
|
622
|
+
- toPolygon
|
|
623
|
+
- toGeometry
|
|
624
|
+
- toMultiPoint
|
|
625
|
+
- toMultiLine
|
|
626
|
+
- toMultiPolygon
|
|
627
|
+
- toGeometryCollection
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
支持自定义 block 来简化 sql, 如内置的 allColumns
|
|
631
|
+
|
|
632
|
+
```ts
|
|
633
|
+
export default {
|
|
634
|
+
findByName: {
|
|
635
|
+
type: SqlType.BLOCK,
|
|
636
|
+
sql: 'id, name',
|
|
637
|
+
},
|
|
638
|
+
} as Record<string, SqlMap>;
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
### DataSource
|
|
642
|
+
|
|
643
|
+
DataSource 仅能在 DAO 中使用,可以将 MySQL 返回的数据反序列化为类。支持的方法有
|
|
644
|
+
|
|
645
|
+
```ts
|
|
646
|
+
export interface DataSource<T> {
|
|
647
|
+
// 将返回的行都转换为 T
|
|
648
|
+
execute(sqlName: string, data?: any): Promise<Array<T>>;
|
|
649
|
+
// 将返回的行都转换为 T, 仅返回第一条
|
|
650
|
+
executeScalar(sqlName: string, data?: any): Promise<T | null>;
|
|
651
|
+
// 直接返回 mysql 数据
|
|
652
|
+
executeRaw(sqlName: string, data?: any): Promise<Array<any>>;
|
|
653
|
+
// 直接返回 mysql 数据, 仅返回第一条
|
|
654
|
+
executeRawScalar(sqlName: string, data?: any): Promise<any | null>;
|
|
655
|
+
// 返回分页数据
|
|
656
|
+
paginate(sqlName: string, data: any, currentPage: number, perPageCount: number): Promise<any>;
|
|
657
|
+
// 返回行数
|
|
658
|
+
count(sqlName: string, data?: any): Promise<number>;
|
|
659
|
+
}
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
### 时区问题
|
|
663
|
+
|
|
664
|
+
注意连接配置中的时区必须和数据库的时区完全一致,否则可能出现时间错误的问题。
|
|
665
|
+
|
|
666
|
+
```yaml
|
|
667
|
+
dataSource:
|
|
668
|
+
foo:
|
|
669
|
+
connectionLimit: 100
|
|
670
|
+
database: 'test'
|
|
671
|
+
host: '127.0.0.1'
|
|
672
|
+
user: root
|
|
673
|
+
port: 3306
|
|
674
|
+
timezone: '+08:00'
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
可以通过以下 SQL 来查看数据库时区
|
|
678
|
+
|
|
679
|
+
```sql
|
|
680
|
+
SELECT @@GLOBAL.time_zone;
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
## Unittest
|
|
684
|
+
|
|
685
|
+
可以在 `module.yml` 中开启 forkDb 配置,即可实现 unittest 环境自动创建数据库
|
|
686
|
+
|
|
687
|
+
```yaml
|
|
688
|
+
# module.yml
|
|
689
|
+
dataSource:
|
|
690
|
+
foo:
|
|
691
|
+
# 开启 ci 环境自动创建数据库
|
|
692
|
+
forkDb: true
|
|
693
|
+
```
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Application, ILifecycleBoot } from 'egg';
|
|
2
|
+
export default class DalAppBootHook implements ILifecycleBoot {
|
|
3
|
+
private readonly app;
|
|
4
|
+
private dalTableEggPrototypeHook;
|
|
5
|
+
private dalModuleLoadUnitHook;
|
|
6
|
+
private transactionPrototypeHook;
|
|
7
|
+
constructor(app: Application);
|
|
8
|
+
configWillLoad(): void;
|
|
9
|
+
beforeClose(): Promise<void>;
|
|
10
|
+
}
|