@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.
@@ -1,213 +0,0 @@
1
- import {
2
- Transaction,
3
- TransactionContext,
4
- TransactionSelection,
5
- TransactionUpdation,
6
- TransactionDeletion,
7
- Condition,
8
- BuildQueryResult
9
- } from '../types.js';
10
- import type { Database } from './database.js';
11
-
12
- /**
13
- * 事务查询选择器实现
14
- */
15
- class TrxSelection<S extends Record<string, object>, T extends keyof S> implements TransactionSelection<S, T> {
16
- private conditions: Condition<S[T]> = {};
17
- private orderings: { field: keyof S[T]; direction: 'ASC' | 'DESC' }[] = [];
18
- private limitCount?: number;
19
- private offsetCount?: number;
20
-
21
- constructor(
22
- private readonly db: Database<any, S, string>,
23
- private readonly trx: Transaction,
24
- private readonly tableName: T,
25
- private readonly fields?: (keyof S[T])[]
26
- ) {}
27
-
28
- where(condition: Condition<S[T]>): this {
29
- this.conditions = { ...this.conditions, ...condition };
30
- return this;
31
- }
32
-
33
- orderBy(field: keyof S[T], direction: 'ASC' | 'DESC' = 'ASC'): this {
34
- this.orderings.push({ field, direction });
35
- return this;
36
- }
37
-
38
- limit(count: number): this {
39
- this.limitCount = count;
40
- return this;
41
- }
42
-
43
- offset(count: number): this {
44
- this.offsetCount = count;
45
- return this;
46
- }
47
-
48
- then<R>(onfulfilled?: (value: S[T][]) => R | PromiseLike<R>): Promise<R> {
49
- const { query, params } = this.db.buildQuery({
50
- type: 'select',
51
- tableName: this.tableName,
52
- fields: this.fields,
53
- conditions: this.conditions,
54
- orderings: this.orderings,
55
- limitCount: this.limitCount,
56
- offsetCount: this.offsetCount
57
- });
58
-
59
- return this.trx.query<S[T][]>(query, params).then(onfulfilled!);
60
- }
61
- }
62
-
63
- /**
64
- * 事务更新器实现
65
- */
66
- class TrxUpdation<S extends Record<string, object>, T extends keyof S> implements TransactionUpdation<S, T> {
67
- private conditions: Condition<S[T]> = {};
68
-
69
- constructor(
70
- private readonly db: Database<any, S, string>,
71
- private readonly trx: Transaction,
72
- private readonly tableName: T,
73
- private readonly data: Partial<S[T]>
74
- ) {}
75
-
76
- where(condition: Condition<S[T]>): this {
77
- this.conditions = { ...this.conditions, ...condition };
78
- return this;
79
- }
80
-
81
- then<R>(onfulfilled?: (value: number) => R | PromiseLike<R>): Promise<R> {
82
- const { query, params } = this.db.buildQuery({
83
- type: 'update',
84
- tableName: this.tableName,
85
- update: this.data,
86
- conditions: this.conditions
87
- });
88
-
89
- return this.trx.query<{ affectedRows?: number; changes?: number }>(query, params)
90
- .then(result => {
91
- const affected = result.affectedRows ?? result.changes ?? 0;
92
- return onfulfilled ? onfulfilled(affected) : affected as any;
93
- });
94
- }
95
- }
96
-
97
- /**
98
- * 事务删除器实现
99
- */
100
- class TrxDeletion<S extends Record<string, object>, T extends keyof S> implements TransactionDeletion<S, T> {
101
- private conditions: Condition<S[T]> = {};
102
-
103
- constructor(
104
- private readonly db: Database<any, S, string>,
105
- private readonly trx: Transaction,
106
- private readonly tableName: T
107
- ) {}
108
-
109
- where(condition: Condition<S[T]>): this {
110
- this.conditions = { ...this.conditions, ...condition };
111
- return this;
112
- }
113
-
114
- then<R>(onfulfilled?: (value: number) => R | PromiseLike<R>): Promise<R> {
115
- const { query, params } = this.db.buildQuery({
116
- type: 'delete',
117
- tableName: this.tableName,
118
- conditions: this.conditions
119
- });
120
-
121
- return this.trx.query<{ affectedRows?: number; changes?: number }>(query, params)
122
- .then(result => {
123
- const affected = result.affectedRows ?? result.changes ?? 0;
124
- return onfulfilled ? onfulfilled(affected) : affected as any;
125
- });
126
- }
127
- }
128
-
129
- /**
130
- * 增强的事务上下文实现
131
- * 支持链式调用的事务操作
132
- */
133
- export class TransactionContextImpl<S extends Record<string, object>> implements TransactionContext<S> {
134
- constructor(
135
- private readonly db: Database<any, S, string>,
136
- private readonly trx: Transaction
137
- ) {}
138
-
139
- /**
140
- * 提交事务
141
- */
142
- async commit(): Promise<void> {
143
- return this.trx.commit();
144
- }
145
-
146
- /**
147
- * 回滚事务
148
- */
149
- async rollback(): Promise<void> {
150
- return this.trx.rollback();
151
- }
152
-
153
- /**
154
- * 执行原生 SQL
155
- */
156
- async query<T = any>(sql: string, params?: any[]): Promise<T> {
157
- return this.trx.query<T>(sql, params);
158
- }
159
-
160
- /**
161
- * 插入单条数据
162
- */
163
- async insert<T extends keyof S>(tableName: T, data: S[T]): Promise<S[T]> {
164
- const { query, params } = this.db.buildQuery({
165
- type: 'insert',
166
- tableName,
167
- data
168
- });
169
-
170
- await this.trx.query(query, params);
171
- return data;
172
- }
173
-
174
- /**
175
- * 批量插入数据
176
- */
177
- async insertMany<T extends keyof S>(tableName: T, data: S[T][]): Promise<{ affectedRows: number }> {
178
- if (!data.length) {
179
- return { affectedRows: 0 };
180
- }
181
-
182
- const { query, params } = this.db.buildQuery({
183
- type: 'insert_many',
184
- tableName,
185
- data
186
- });
187
-
188
- const result = await this.trx.query<{ affectedRows?: number; changes?: number }>(query, params);
189
- return { affectedRows: result.affectedRows ?? result.changes ?? data.length };
190
- }
191
-
192
- /**
193
- * 查询数据 - 返回链式选择器
194
- */
195
- select<T extends keyof S>(tableName: T, fields?: (keyof S[T])[]): TransactionSelection<S, T> {
196
- return new TrxSelection(this.db, this.trx, tableName, fields);
197
- }
198
-
199
- /**
200
- * 更新数据 - 返回链式更新器
201
- */
202
- update<T extends keyof S>(tableName: T, data: Partial<S[T]>): TransactionUpdation<S, T> {
203
- return new TrxUpdation(this.db, this.trx, tableName, data);
204
- }
205
-
206
- /**
207
- * 删除数据 - 返回链式删除器
208
- */
209
- delete<T extends keyof S>(tableName: T): TransactionDeletion<S, T> {
210
- return new TrxDeletion(this.db, this.trx, tableName);
211
- }
212
- }
213
-