@zhin.js/database 1.0.45 → 1.0.47
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/package.json +7 -4
- package/CHANGELOG.md +0 -293
- package/src/base/database.ts +0 -272
- package/src/base/dialect.ts +0 -89
- package/src/base/index.ts +0 -6
- package/src/base/model.ts +0 -329
- package/src/base/query-classes.ts +0 -625
- package/src/base/thenable.ts +0 -55
- package/src/base/transaction.ts +0 -213
- package/src/dialects/memory.ts +0 -882
- package/src/dialects/mongodb.ts +0 -535
- package/src/dialects/mysql.ts +0 -286
- package/src/dialects/pg.ts +0 -284
- package/src/dialects/redis.ts +0 -600
- package/src/dialects/sqlite.ts +0 -317
- package/src/index.ts +0 -19
- package/src/migration.ts +0 -547
- package/src/registry.ts +0 -59
- package/src/type/document/database.ts +0 -284
- package/src/type/document/model.ts +0 -87
- package/src/type/keyvalue/database.ts +0 -262
- package/src/type/keyvalue/model.ts +0 -339
- package/src/type/related/database.ts +0 -674
- package/src/type/related/model.ts +0 -884
- package/src/types.ts +0 -918
- package/tests/database.test.ts +0 -1750
- package/tests/dialects.test.ts +0 -147
- package/tests/migration.test.ts +0 -73
- package/tsconfig.json +0 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/database",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.47",
|
|
4
4
|
"description": "Universal database abstraction layer for Zhin.js - supports SQLite, MySQL, PostgreSQL, MongoDB, Redis",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"development": "./src/index.ts"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"lib"
|
|
18
|
+
],
|
|
16
19
|
"engines": {
|
|
17
20
|
"node": ">=22.5.0"
|
|
18
21
|
},
|
|
@@ -21,7 +24,7 @@
|
|
|
21
24
|
"mysql2": "^3.9.8",
|
|
22
25
|
"pg": "^7.1.2",
|
|
23
26
|
"redis": "latest",
|
|
24
|
-
"zhin.js": "1.0.
|
|
27
|
+
"zhin.js": "1.0.60"
|
|
25
28
|
},
|
|
26
29
|
"peerDependenciesMeta": {
|
|
27
30
|
"mysql2": {
|
|
@@ -43,8 +46,8 @@
|
|
|
43
46
|
"mysql2": "latest",
|
|
44
47
|
"pg": "latest",
|
|
45
48
|
"redis": "latest",
|
|
46
|
-
"typescript": "^5.3
|
|
47
|
-
"zhin.js": "1.0.
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"zhin.js": "1.0.60"
|
|
48
51
|
},
|
|
49
52
|
"repository": {
|
|
50
53
|
"type": "git",
|
package/CHANGELOG.md
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
# @zhin.js/database
|
|
2
|
-
|
|
3
|
-
## 1.0.45
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- zhin.js@1.0.58
|
|
8
|
-
|
|
9
|
-
## 1.0.44
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- zhin.js@1.0.57
|
|
14
|
-
|
|
15
|
-
## 1.0.43
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- zhin.js@1.0.56
|
|
20
|
-
|
|
21
|
-
## 1.0.42
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- zhin.js@1.0.55
|
|
26
|
-
|
|
27
|
-
## 1.0.41
|
|
28
|
-
|
|
29
|
-
### Patch Changes
|
|
30
|
-
|
|
31
|
-
- 16c8f92: fix: 统一发一次版
|
|
32
|
-
- Updated dependencies [16c8f92]
|
|
33
|
-
- zhin.js@1.0.54
|
|
34
|
-
|
|
35
|
-
## 1.0.40
|
|
36
|
-
|
|
37
|
-
### Patch Changes
|
|
38
|
-
|
|
39
|
-
- daee7f6: fix: database error
|
|
40
|
-
- zhin.js@1.0.53
|
|
41
|
-
|
|
42
|
-
## 1.0.39
|
|
43
|
-
|
|
44
|
-
### Patch Changes
|
|
45
|
-
|
|
46
|
-
- Updated dependencies [bb6bfa8]
|
|
47
|
-
- Updated dependencies [bb6bfa8]
|
|
48
|
-
- zhin.js@1.0.52
|
|
49
|
-
|
|
50
|
-
## 1.0.38
|
|
51
|
-
|
|
52
|
-
### Patch Changes
|
|
53
|
-
|
|
54
|
-
- zhin.js@1.0.51
|
|
55
|
-
|
|
56
|
-
## 1.0.37
|
|
57
|
-
|
|
58
|
-
### Patch Changes
|
|
59
|
-
|
|
60
|
-
- zhin.js@1.0.50
|
|
61
|
-
|
|
62
|
-
## 1.0.36
|
|
63
|
-
|
|
64
|
-
### Patch Changes
|
|
65
|
-
|
|
66
|
-
- zhin.js@1.0.49
|
|
67
|
-
|
|
68
|
-
## 1.0.35
|
|
69
|
-
|
|
70
|
-
### Patch Changes
|
|
71
|
-
|
|
72
|
-
- zhin.js@1.0.48
|
|
73
|
-
|
|
74
|
-
## 1.0.34
|
|
75
|
-
|
|
76
|
-
### Patch Changes
|
|
77
|
-
|
|
78
|
-
- Updated dependencies [de3e352]
|
|
79
|
-
- zhin.js@1.0.47
|
|
80
|
-
|
|
81
|
-
## 1.0.33
|
|
82
|
-
|
|
83
|
-
### Patch Changes
|
|
84
|
-
|
|
85
|
-
- Updated dependencies [7394603]
|
|
86
|
-
- zhin.js@1.0.46
|
|
87
|
-
|
|
88
|
-
## 1.0.32
|
|
89
|
-
|
|
90
|
-
### Patch Changes
|
|
91
|
-
|
|
92
|
-
- zhin.js@1.0.45
|
|
93
|
-
|
|
94
|
-
## 1.0.31
|
|
95
|
-
|
|
96
|
-
### Patch Changes
|
|
97
|
-
|
|
98
|
-
- zhin.js@1.0.44
|
|
99
|
-
|
|
100
|
-
## 1.0.30
|
|
101
|
-
|
|
102
|
-
### Patch Changes
|
|
103
|
-
|
|
104
|
-
- Updated dependencies [72ec4ba]
|
|
105
|
-
- zhin.js@1.0.43
|
|
106
|
-
|
|
107
|
-
## 1.0.29
|
|
108
|
-
|
|
109
|
-
### Patch Changes
|
|
110
|
-
|
|
111
|
-
- zhin.js@1.0.42
|
|
112
|
-
|
|
113
|
-
## 1.0.28
|
|
114
|
-
|
|
115
|
-
### Patch Changes
|
|
116
|
-
|
|
117
|
-
- zhin.js@1.0.41
|
|
118
|
-
|
|
119
|
-
## 1.0.27
|
|
120
|
-
|
|
121
|
-
### Patch Changes
|
|
122
|
-
|
|
123
|
-
- Updated dependencies [7ef9057]
|
|
124
|
-
- zhin.js@1.0.40
|
|
125
|
-
|
|
126
|
-
## 1.0.26
|
|
127
|
-
|
|
128
|
-
### Patch Changes
|
|
129
|
-
|
|
130
|
-
- zhin.js@1.0.39
|
|
131
|
-
|
|
132
|
-
## 1.0.25
|
|
133
|
-
|
|
134
|
-
### Patch Changes
|
|
135
|
-
|
|
136
|
-
- Updated dependencies [ab5c54a]
|
|
137
|
-
- zhin.js@1.0.38
|
|
138
|
-
|
|
139
|
-
## 1.0.24
|
|
140
|
-
|
|
141
|
-
### Patch Changes
|
|
142
|
-
|
|
143
|
-
- zhin.js@1.0.37
|
|
144
|
-
|
|
145
|
-
## 1.0.23
|
|
146
|
-
|
|
147
|
-
### Patch Changes
|
|
148
|
-
|
|
149
|
-
- zhin.js@1.0.36
|
|
150
|
-
|
|
151
|
-
## 1.0.22
|
|
152
|
-
|
|
153
|
-
### Patch Changes
|
|
154
|
-
|
|
155
|
-
- zhin.js@1.0.35
|
|
156
|
-
|
|
157
|
-
## 1.0.21
|
|
158
|
-
|
|
159
|
-
### Patch Changes
|
|
160
|
-
|
|
161
|
-
- zhin.js@1.0.34
|
|
162
|
-
|
|
163
|
-
## 1.0.20
|
|
164
|
-
|
|
165
|
-
### Patch Changes
|
|
166
|
-
|
|
167
|
-
- zhin.js@1.0.33
|
|
168
|
-
|
|
169
|
-
## 1.0.19
|
|
170
|
-
|
|
171
|
-
### Patch Changes
|
|
172
|
-
|
|
173
|
-
- zhin.js@1.0.32
|
|
174
|
-
|
|
175
|
-
## 1.0.18
|
|
176
|
-
|
|
177
|
-
### Patch Changes
|
|
178
|
-
|
|
179
|
-
- zhin.js@1.0.31
|
|
180
|
-
|
|
181
|
-
## 1.0.17
|
|
182
|
-
|
|
183
|
-
### Patch Changes
|
|
184
|
-
|
|
185
|
-
- Updated dependencies [460a6c6]
|
|
186
|
-
- zhin.js@1.0.30
|
|
187
|
-
|
|
188
|
-
## 1.0.16
|
|
189
|
-
|
|
190
|
-
### Patch Changes
|
|
191
|
-
|
|
192
|
-
- zhin.js@1.0.29
|
|
193
|
-
|
|
194
|
-
## 1.0.15
|
|
195
|
-
|
|
196
|
-
### Patch Changes
|
|
197
|
-
|
|
198
|
-
- zhin.js@1.0.28
|
|
199
|
-
|
|
200
|
-
## 1.0.14
|
|
201
|
-
|
|
202
|
-
### Patch Changes
|
|
203
|
-
|
|
204
|
-
- Updated dependencies [b27e633]
|
|
205
|
-
- zhin.js@1.0.27
|
|
206
|
-
|
|
207
|
-
## 1.0.13
|
|
208
|
-
|
|
209
|
-
### Patch Changes
|
|
210
|
-
|
|
211
|
-
- 106d357: fix: ai
|
|
212
|
-
- Updated dependencies [106d357]
|
|
213
|
-
- zhin.js@1.0.26
|
|
214
|
-
|
|
215
|
-
## 1.0.12
|
|
216
|
-
|
|
217
|
-
### Patch Changes
|
|
218
|
-
|
|
219
|
-
- 26d2942: fix: ai
|
|
220
|
-
- 6b02c41: fix: ai
|
|
221
|
-
- Updated dependencies [26d2942]
|
|
222
|
-
- Updated dependencies [6b02c41]
|
|
223
|
-
- zhin.js@1.0.25
|
|
224
|
-
|
|
225
|
-
## 1.0.11
|
|
226
|
-
|
|
227
|
-
### Patch Changes
|
|
228
|
-
|
|
229
|
-
- zhin.js@1.0.24
|
|
230
|
-
|
|
231
|
-
## 1.0.10
|
|
232
|
-
|
|
233
|
-
### Patch Changes
|
|
234
|
-
|
|
235
|
-
- Updated dependencies [52ae08a]
|
|
236
|
-
- zhin.js@1.0.23
|
|
237
|
-
|
|
238
|
-
## 1.0.9
|
|
239
|
-
|
|
240
|
-
### Patch Changes
|
|
241
|
-
|
|
242
|
-
- Updated dependencies [26aba27]
|
|
243
|
-
- zhin.js@1.0.22
|
|
244
|
-
|
|
245
|
-
## 1.0.8
|
|
246
|
-
|
|
247
|
-
### Patch Changes
|
|
248
|
-
|
|
249
|
-
- zhin.js@1.0.21
|
|
250
|
-
|
|
251
|
-
## 1.0.7
|
|
252
|
-
|
|
253
|
-
### Patch Changes
|
|
254
|
-
|
|
255
|
-
- a3b7673: fix: 调整依赖项
|
|
256
|
-
- Updated dependencies [5141137]
|
|
257
|
-
- zhin.js@1.0.20
|
|
258
|
-
|
|
259
|
-
## 1.0.6
|
|
260
|
-
|
|
261
|
-
### Patch Changes
|
|
262
|
-
|
|
263
|
-
- f9faa1d: fix: test release
|
|
264
|
-
|
|
265
|
-
## 1.0.5
|
|
266
|
-
|
|
267
|
-
### Patch Changes
|
|
268
|
-
|
|
269
|
-
- d16a69c: fix: test trust publish
|
|
270
|
-
|
|
271
|
-
## 1.0.4
|
|
272
|
-
|
|
273
|
-
### Patch Changes
|
|
274
|
-
|
|
275
|
-
- 547028f: fix: 优化包结构,优化客户端支持
|
|
276
|
-
|
|
277
|
-
## 1.0.3
|
|
278
|
-
|
|
279
|
-
### Patch Changes
|
|
280
|
-
|
|
281
|
-
- 551c4d2: fix: 插件支持配置文件读取,优化 test 用例
|
|
282
|
-
|
|
283
|
-
## 1.0.2
|
|
284
|
-
|
|
285
|
-
### Patch Changes
|
|
286
|
-
|
|
287
|
-
- 47845fb: fix: err
|
|
288
|
-
|
|
289
|
-
## 1.0.1
|
|
290
|
-
|
|
291
|
-
### Patch Changes
|
|
292
|
-
|
|
293
|
-
- 727963c: fix: 修复 sqlite 数据错误;优化 console 展示
|
package/src/base/database.ts
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import type { Dialect } from './dialect.js';
|
|
2
|
-
import { Model } from './model.js';
|
|
3
|
-
import { Definition, QueryParams, AlterDefinition, Condition, BuildQueryResult, Transaction, TransactionOptions, TransactionContext } from '../types.js';
|
|
4
|
-
import * as QueryClasses from './query-classes.js';
|
|
5
|
-
import { TransactionContextImpl } from './transaction.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 查询日志处理器类型
|
|
9
|
-
*/
|
|
10
|
-
export type QueryLogger = (info: {
|
|
11
|
-
sql: string;
|
|
12
|
-
params?: any[];
|
|
13
|
-
duration: number;
|
|
14
|
-
error?: Error;
|
|
15
|
-
}) => void;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 默认查询日志处理器
|
|
19
|
-
*/
|
|
20
|
-
const defaultLogger: QueryLogger = ({ sql, params, duration, error }) => {
|
|
21
|
-
const paramStr = params?.length ? ` [${JSON.stringify(params)}]` : '';
|
|
22
|
-
const status = error ? `❌ ERROR: ${error.message}` : `✅ ${duration}ms`;
|
|
23
|
-
console.log(`[SQL] ${sql}${paramStr} → ${status}`);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 基础数据库抽象类
|
|
28
|
-
* 定义所有数据库类型的通用接口和行为
|
|
29
|
-
*/
|
|
30
|
-
export abstract class Database<D=any,S extends Record<string, object>=Record<string, object>,Q=string> {
|
|
31
|
-
protected hasStarted = false;
|
|
32
|
-
public readonly definitions: Database.Definitions<S>=new Database.Definitions<S>();
|
|
33
|
-
public readonly models: Database.Models<S,D,Q> = new Database.Models<S,D,Q>();
|
|
34
|
-
|
|
35
|
-
private _logger?: QueryLogger;
|
|
36
|
-
private _logging = false;
|
|
37
|
-
|
|
38
|
-
constructor(
|
|
39
|
-
public readonly dialect: Dialect<D,S,Q>,
|
|
40
|
-
definitions?: Database.DefinitionObj<S>,
|
|
41
|
-
) {
|
|
42
|
-
for (const key in definitions) {
|
|
43
|
-
this.definitions.set(key, definitions[key]);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// ============================================================================
|
|
48
|
-
// Query Logging
|
|
49
|
-
// ============================================================================
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 启用查询日志
|
|
53
|
-
* @param handler 自定义日志处理器,不传则使用默认控制台输出
|
|
54
|
-
*/
|
|
55
|
-
enableLogging(handler?: QueryLogger): this {
|
|
56
|
-
this._logging = true;
|
|
57
|
-
this._logger = handler ?? defaultLogger;
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 禁用查询日志
|
|
63
|
-
*/
|
|
64
|
-
disableLogging(): this {
|
|
65
|
-
this._logging = false;
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 查询日志是否启用
|
|
71
|
-
*/
|
|
72
|
-
get isLogging(): boolean {
|
|
73
|
-
return this._logging;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 记录查询日志
|
|
78
|
-
*/
|
|
79
|
-
protected log(sql: string, params?: any[], duration?: number, error?: Error): void {
|
|
80
|
-
if (this._logging && this._logger) {
|
|
81
|
-
this._logger({ sql: String(sql), params, duration: duration ?? 0, error });
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* 数据库是否已启动
|
|
86
|
-
*/
|
|
87
|
-
get isStarted(): boolean {
|
|
88
|
-
return this.dialect !== undefined && this.dialect.isConnected();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 启动数据库
|
|
93
|
-
*/
|
|
94
|
-
async start(): Promise<void> {
|
|
95
|
-
await this.dialect.connect();
|
|
96
|
-
await this.initialize();
|
|
97
|
-
this.hasStarted = true;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 停止数据库
|
|
102
|
-
*/
|
|
103
|
-
async stop(): Promise<void> {
|
|
104
|
-
await this.dialect.disconnect();
|
|
105
|
-
this.hasStarted = false;
|
|
106
|
-
console.log(`Database disconnected from dialect: ${this.dialect.name}`);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 健康检查
|
|
111
|
-
*/
|
|
112
|
-
async healthCheck(): Promise<boolean> {
|
|
113
|
-
return this.dialect.healthCheck();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 执行原生查询
|
|
118
|
-
*/
|
|
119
|
-
async query<U = any>(sql: Q, params?: any[]): Promise<U> {
|
|
120
|
-
if (!this.isStarted) {
|
|
121
|
-
throw new Error('Database not started');
|
|
122
|
-
}
|
|
123
|
-
const start = Date.now();
|
|
124
|
-
try {
|
|
125
|
-
const result = await this.dialect.query<U>(sql, params);
|
|
126
|
-
this.log(String(sql), params, Date.now() - start);
|
|
127
|
-
return result;
|
|
128
|
-
} catch (error) {
|
|
129
|
-
this.log(String(sql), params, Date.now() - start, error as Error);
|
|
130
|
-
throw error;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
abstract buildQuery<T extends keyof S>(params: QueryParams<S,T>): BuildQueryResult<Q>;
|
|
134
|
-
/**
|
|
135
|
-
* 获取数据库方言名称
|
|
136
|
-
*/
|
|
137
|
-
get dialectName(): string {
|
|
138
|
-
if(this.dialect === undefined){
|
|
139
|
-
throw new Error('Database not started');
|
|
140
|
-
}
|
|
141
|
-
return this.dialect.name;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 获取数据库配置
|
|
146
|
-
*/
|
|
147
|
-
get config(): any {
|
|
148
|
-
if(this.dialect === undefined){
|
|
149
|
-
throw new Error('Database not started');
|
|
150
|
-
}
|
|
151
|
-
return this.dialect.config;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* 抽象方法:初始化数据库
|
|
156
|
-
*/
|
|
157
|
-
protected abstract initialize(): Promise<void>;
|
|
158
|
-
define<K extends keyof S>(name: K, definition: Definition<S[K]>) {
|
|
159
|
-
this.definitions.set(name, definition);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* 删除表定义
|
|
163
|
-
*/
|
|
164
|
-
destroy<K extends keyof S>(name: K) {
|
|
165
|
-
this.definitions.delete(name);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* 创建表
|
|
170
|
-
*/
|
|
171
|
-
create<T extends keyof S>(
|
|
172
|
-
name: T,
|
|
173
|
-
definition: Definition<S[T]>
|
|
174
|
-
): QueryClasses.Creation<S,T,D,Q> {
|
|
175
|
-
return new QueryClasses.Creation<S,T,D,Q>(this, name, definition);
|
|
176
|
-
}
|
|
177
|
-
alter<T extends keyof S>(name: T, alterations: AlterDefinition<S[T]>): QueryClasses.Alteration<S,T,D,Q>{
|
|
178
|
-
return new QueryClasses.Alteration<S,T,D,Q>(this, name, alterations);
|
|
179
|
-
}
|
|
180
|
-
select<T extends keyof S, K extends keyof S[T]>(name: T, fields: Array<K>): QueryClasses.Selection<S,T,K,D,Q>{
|
|
181
|
-
return new QueryClasses.Selection<S,T,K,D,Q>(this, name, fields);
|
|
182
|
-
}
|
|
183
|
-
insert<T extends keyof S>(name: T, data: S[T]): QueryClasses.Insertion<S,T,D,Q>{
|
|
184
|
-
return new QueryClasses.Insertion<S,T,D,Q>(this, name, data);
|
|
185
|
-
}
|
|
186
|
-
update<T extends keyof S>(name: T, update: Partial<S[T]>): QueryClasses.Updation<S,T,D,Q>{
|
|
187
|
-
return new QueryClasses.Updation<S,T,D,Q>(this, name, update);
|
|
188
|
-
}
|
|
189
|
-
delete<T extends keyof S>(name: T, condition: Condition<S[T]>): QueryClasses.Deletion<S,T,D,Q>{
|
|
190
|
-
return new QueryClasses.Deletion<S,T,D,Q>(this, name).where(condition);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* 批量插入
|
|
195
|
-
*/
|
|
196
|
-
insertMany<T extends keyof S>(name: T, data: S[T][]): QueryClasses.BatchInsertion<S,T,D,Q>{
|
|
197
|
-
return new QueryClasses.BatchInsertion<S,T,D,Q>(this, name, data);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 聚合查询
|
|
202
|
-
*/
|
|
203
|
-
aggregate<T extends keyof S>(name: T): QueryClasses.Aggregation<S,T,D,Q>{
|
|
204
|
-
return new QueryClasses.Aggregation<S,T,D,Q>(this, name);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// ============================================================================
|
|
208
|
-
// Transaction Support
|
|
209
|
-
// ============================================================================
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* 是否支持事务
|
|
213
|
-
*/
|
|
214
|
-
supportsTransactions(): boolean {
|
|
215
|
-
return this.dialect.supportsTransactions();
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* 在事务中执行操作
|
|
220
|
-
* 自动处理 commit 和 rollback
|
|
221
|
-
* 支持链式调用:trx.insert(), trx.select(), trx.update(), trx.delete()
|
|
222
|
-
*/
|
|
223
|
-
async transaction<R>(callback: (trx: TransactionContext<S>) => Promise<R>, options?: TransactionOptions): Promise<R> {
|
|
224
|
-
if (!this.isStarted) {
|
|
225
|
-
throw new Error('Database not started');
|
|
226
|
-
}
|
|
227
|
-
const trx = await this.dialect.beginTransaction(options);
|
|
228
|
-
const trxContext = new TransactionContextImpl<S>(this as any, trx);
|
|
229
|
-
try {
|
|
230
|
-
const result = await callback(trxContext);
|
|
231
|
-
await trxContext.commit();
|
|
232
|
-
return result;
|
|
233
|
-
} catch (error) {
|
|
234
|
-
await trxContext.rollback();
|
|
235
|
-
throw error;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* 抽象方法:获取所有模型名称
|
|
241
|
-
*/
|
|
242
|
-
abstract getModelNames(): string[];
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* 抽象方法:清理资源
|
|
246
|
-
*/
|
|
247
|
-
async dispose(): Promise<void> {
|
|
248
|
-
this.models.clear();
|
|
249
|
-
if(this.dialect === undefined){
|
|
250
|
-
throw new Error('Database not started');
|
|
251
|
-
}
|
|
252
|
-
await this.dialect.dispose();
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
export namespace Database {
|
|
256
|
-
export class Definitions<S extends Record<string, object>> extends Map<keyof S, Definition<S[keyof S]>> {
|
|
257
|
-
constructor() {
|
|
258
|
-
super();
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
export class Models<S extends Record<string, object>,D=any,Q=string> extends Map<keyof S, Model<D,S,Q,keyof S>> {
|
|
262
|
-
constructor() {
|
|
263
|
-
super();
|
|
264
|
-
}
|
|
265
|
-
get<K extends keyof S>(key: K): Model<D,S,Q,K> | undefined {
|
|
266
|
-
return super.get(key) as Model<D,S,Q,K> | undefined;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
export type DefinitionObj<S extends Record<string, object>> = {
|
|
270
|
-
[K in keyof S]: Definition<S[K]>;
|
|
271
|
-
};
|
|
272
|
-
}
|
package/src/base/dialect.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// Database Dialect Interface
|
|
3
|
-
// ============================================================================
|
|
4
|
-
|
|
5
|
-
import { Transaction, TransactionOptions, IsolationLevel } from '../types.js';
|
|
6
|
-
|
|
7
|
-
// ============================================================================
|
|
8
|
-
// SQL Builder Base Class
|
|
9
|
-
// ============================================================================
|
|
10
|
-
|
|
11
|
-
export abstract class Dialect<T,S extends Record<string, object>,Q> {
|
|
12
|
-
public readonly name: string;
|
|
13
|
-
public readonly config: T;
|
|
14
|
-
|
|
15
|
-
protected constructor(name: string, config: T) {
|
|
16
|
-
this.name = name;
|
|
17
|
-
this.config = config;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Abstract methods that must be implemented by concrete dialects
|
|
21
|
-
abstract isConnected(): boolean;
|
|
22
|
-
abstract connect(): Promise<void>;
|
|
23
|
-
abstract disconnect(): Promise<void>;
|
|
24
|
-
abstract healthCheck(): Promise<boolean>;
|
|
25
|
-
abstract query<U = any>(query: Q, params?: any[]): Promise<U>;
|
|
26
|
-
abstract mapColumnType(type: string): string;
|
|
27
|
-
abstract quoteIdentifier(identifier: string|number|symbol): string;
|
|
28
|
-
abstract getParameterPlaceholder(index: number): string;
|
|
29
|
-
abstract getStatementTerminator(): string;
|
|
30
|
-
abstract formatBoolean(value: boolean): string;
|
|
31
|
-
abstract formatDate(value: Date): string;
|
|
32
|
-
abstract formatJson(value: any): string;
|
|
33
|
-
abstract escapeString(value: string): string;
|
|
34
|
-
abstract formatDefaultValue(value: any): string;
|
|
35
|
-
abstract formatLimit(limit: number): string;
|
|
36
|
-
abstract formatOffset(offset: number): string;
|
|
37
|
-
abstract formatLimitOffset(limit: number, offset: number): string;
|
|
38
|
-
abstract formatCreateTable<T extends keyof S>(tableName: T, columns: string[]): string;
|
|
39
|
-
abstract formatAlterTable<T extends keyof S>(tableName: T, alterations: string[]): string;
|
|
40
|
-
abstract formatDropTable<T extends keyof S>(tableName: T, ifExists?: boolean): string;
|
|
41
|
-
abstract formatDropIndex<T extends keyof S>(indexName: string, tableName: T, ifExists?: boolean): string;
|
|
42
|
-
abstract dispose(): Promise<void>;
|
|
43
|
-
|
|
44
|
-
// ============================================================================
|
|
45
|
-
// Transaction Support (optional, default implementations)
|
|
46
|
-
// ============================================================================
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 是否支持事务
|
|
50
|
-
*/
|
|
51
|
-
supportsTransactions(): boolean {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 开始事务
|
|
57
|
-
*/
|
|
58
|
-
async beginTransaction(options?: TransactionOptions): Promise<Transaction> {
|
|
59
|
-
throw new Error(`Dialect ${this.name} does not support transactions`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 格式化事务隔离级别
|
|
64
|
-
*/
|
|
65
|
-
formatIsolationLevel(level: IsolationLevel): string {
|
|
66
|
-
return level.replace(/_/g, ' ');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// ============================================================================
|
|
70
|
-
// Aggregation Support
|
|
71
|
-
// ============================================================================
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 格式化聚合函数
|
|
75
|
-
*/
|
|
76
|
-
formatAggregate(fn: string, field: string, alias?: string): string {
|
|
77
|
-
const fnUpper = fn.toUpperCase();
|
|
78
|
-
const aliasClause = alias ? ` AS ${this.quoteIdentifier(alias)}` : '';
|
|
79
|
-
return `${fnUpper}(${field === '*' ? '*' : this.quoteIdentifier(field)})${aliasClause}`;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
export namespace Dialect {
|
|
83
|
-
export type Creator<D,S extends Record<string, object>,Q> = (config: D) => Dialect<D,S,Q>;
|
|
84
|
-
export type Constructor<D,S extends Record<string, object>,Q> = new (config: D) => Dialect<D,S,Q>;
|
|
85
|
-
export type Factory<D,S extends Record<string, object>,Q> = Creator<D,S,Q> | Constructor<D,S,Q>;
|
|
86
|
-
export function isConstructor<D,S extends Record<string, object>,Q>(fn: Factory<D,S,Q>): fn is Constructor<D,S,Q> {
|
|
87
|
-
return fn.prototype && fn.prototype.constructor === fn;
|
|
88
|
-
}
|
|
89
|
-
}
|