@zhin.js/database 1.0.44 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/database",
3
- "version": "1.0.44",
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.57"
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.0",
47
- "zhin.js": "1.0.57"
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,287 +0,0 @@
1
- # @zhin.js/database
2
-
3
- ## 1.0.44
4
-
5
- ### Patch Changes
6
-
7
- - zhin.js@1.0.57
8
-
9
- ## 1.0.43
10
-
11
- ### Patch Changes
12
-
13
- - zhin.js@1.0.56
14
-
15
- ## 1.0.42
16
-
17
- ### Patch Changes
18
-
19
- - zhin.js@1.0.55
20
-
21
- ## 1.0.41
22
-
23
- ### Patch Changes
24
-
25
- - 16c8f92: fix: 统一发一次版
26
- - Updated dependencies [16c8f92]
27
- - zhin.js@1.0.54
28
-
29
- ## 1.0.40
30
-
31
- ### Patch Changes
32
-
33
- - daee7f6: fix: database error
34
- - zhin.js@1.0.53
35
-
36
- ## 1.0.39
37
-
38
- ### Patch Changes
39
-
40
- - Updated dependencies [bb6bfa8]
41
- - Updated dependencies [bb6bfa8]
42
- - zhin.js@1.0.52
43
-
44
- ## 1.0.38
45
-
46
- ### Patch Changes
47
-
48
- - zhin.js@1.0.51
49
-
50
- ## 1.0.37
51
-
52
- ### Patch Changes
53
-
54
- - zhin.js@1.0.50
55
-
56
- ## 1.0.36
57
-
58
- ### Patch Changes
59
-
60
- - zhin.js@1.0.49
61
-
62
- ## 1.0.35
63
-
64
- ### Patch Changes
65
-
66
- - zhin.js@1.0.48
67
-
68
- ## 1.0.34
69
-
70
- ### Patch Changes
71
-
72
- - Updated dependencies [de3e352]
73
- - zhin.js@1.0.47
74
-
75
- ## 1.0.33
76
-
77
- ### Patch Changes
78
-
79
- - Updated dependencies [7394603]
80
- - zhin.js@1.0.46
81
-
82
- ## 1.0.32
83
-
84
- ### Patch Changes
85
-
86
- - zhin.js@1.0.45
87
-
88
- ## 1.0.31
89
-
90
- ### Patch Changes
91
-
92
- - zhin.js@1.0.44
93
-
94
- ## 1.0.30
95
-
96
- ### Patch Changes
97
-
98
- - Updated dependencies [72ec4ba]
99
- - zhin.js@1.0.43
100
-
101
- ## 1.0.29
102
-
103
- ### Patch Changes
104
-
105
- - zhin.js@1.0.42
106
-
107
- ## 1.0.28
108
-
109
- ### Patch Changes
110
-
111
- - zhin.js@1.0.41
112
-
113
- ## 1.0.27
114
-
115
- ### Patch Changes
116
-
117
- - Updated dependencies [7ef9057]
118
- - zhin.js@1.0.40
119
-
120
- ## 1.0.26
121
-
122
- ### Patch Changes
123
-
124
- - zhin.js@1.0.39
125
-
126
- ## 1.0.25
127
-
128
- ### Patch Changes
129
-
130
- - Updated dependencies [ab5c54a]
131
- - zhin.js@1.0.38
132
-
133
- ## 1.0.24
134
-
135
- ### Patch Changes
136
-
137
- - zhin.js@1.0.37
138
-
139
- ## 1.0.23
140
-
141
- ### Patch Changes
142
-
143
- - zhin.js@1.0.36
144
-
145
- ## 1.0.22
146
-
147
- ### Patch Changes
148
-
149
- - zhin.js@1.0.35
150
-
151
- ## 1.0.21
152
-
153
- ### Patch Changes
154
-
155
- - zhin.js@1.0.34
156
-
157
- ## 1.0.20
158
-
159
- ### Patch Changes
160
-
161
- - zhin.js@1.0.33
162
-
163
- ## 1.0.19
164
-
165
- ### Patch Changes
166
-
167
- - zhin.js@1.0.32
168
-
169
- ## 1.0.18
170
-
171
- ### Patch Changes
172
-
173
- - zhin.js@1.0.31
174
-
175
- ## 1.0.17
176
-
177
- ### Patch Changes
178
-
179
- - Updated dependencies [460a6c6]
180
- - zhin.js@1.0.30
181
-
182
- ## 1.0.16
183
-
184
- ### Patch Changes
185
-
186
- - zhin.js@1.0.29
187
-
188
- ## 1.0.15
189
-
190
- ### Patch Changes
191
-
192
- - zhin.js@1.0.28
193
-
194
- ## 1.0.14
195
-
196
- ### Patch Changes
197
-
198
- - Updated dependencies [b27e633]
199
- - zhin.js@1.0.27
200
-
201
- ## 1.0.13
202
-
203
- ### Patch Changes
204
-
205
- - 106d357: fix: ai
206
- - Updated dependencies [106d357]
207
- - zhin.js@1.0.26
208
-
209
- ## 1.0.12
210
-
211
- ### Patch Changes
212
-
213
- - 26d2942: fix: ai
214
- - 6b02c41: fix: ai
215
- - Updated dependencies [26d2942]
216
- - Updated dependencies [6b02c41]
217
- - zhin.js@1.0.25
218
-
219
- ## 1.0.11
220
-
221
- ### Patch Changes
222
-
223
- - zhin.js@1.0.24
224
-
225
- ## 1.0.10
226
-
227
- ### Patch Changes
228
-
229
- - Updated dependencies [52ae08a]
230
- - zhin.js@1.0.23
231
-
232
- ## 1.0.9
233
-
234
- ### Patch Changes
235
-
236
- - Updated dependencies [26aba27]
237
- - zhin.js@1.0.22
238
-
239
- ## 1.0.8
240
-
241
- ### Patch Changes
242
-
243
- - zhin.js@1.0.21
244
-
245
- ## 1.0.7
246
-
247
- ### Patch Changes
248
-
249
- - a3b7673: fix: 调整依赖项
250
- - Updated dependencies [5141137]
251
- - zhin.js@1.0.20
252
-
253
- ## 1.0.6
254
-
255
- ### Patch Changes
256
-
257
- - f9faa1d: fix: test release
258
-
259
- ## 1.0.5
260
-
261
- ### Patch Changes
262
-
263
- - d16a69c: fix: test trust publish
264
-
265
- ## 1.0.4
266
-
267
- ### Patch Changes
268
-
269
- - 547028f: fix: 优化包结构,优化客户端支持
270
-
271
- ## 1.0.3
272
-
273
- ### Patch Changes
274
-
275
- - 551c4d2: fix: 插件支持配置文件读取,优化 test 用例
276
-
277
- ## 1.0.2
278
-
279
- ### Patch Changes
280
-
281
- - 47845fb: fix: err
282
-
283
- ## 1.0.1
284
-
285
- ### Patch Changes
286
-
287
- - 727963c: fix: 修复 sqlite 数据错误;优化 console 展示
@@ -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
- }
@@ -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
- }
package/src/base/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './dialect.js';
2
- export * from './database.js';
3
- export * from './model.js';
4
- export * from './thenable.js';
5
- export * from './query-classes.js';
6
- export * from './transaction.js';