@zhin.js/database 1.0.4 → 1.0.6
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/CHANGELOG.md +12 -0
- package/README.md +1360 -34
- package/lib/base/database.d.ts +71 -13
- package/lib/base/database.d.ts.map +1 -1
- package/lib/base/database.js +128 -4
- package/lib/base/database.js.map +1 -1
- package/lib/base/dialect.d.ts +27 -10
- package/lib/base/dialect.d.ts.map +1 -1
- package/lib/base/dialect.js +32 -0
- package/lib/base/dialect.js.map +1 -1
- package/lib/base/index.d.ts +1 -0
- package/lib/base/index.d.ts.map +1 -1
- package/lib/base/index.js +1 -0
- package/lib/base/index.js.map +1 -1
- package/lib/base/model.d.ts +105 -12
- package/lib/base/model.d.ts.map +1 -1
- package/lib/base/model.js +224 -3
- package/lib/base/model.js.map +1 -1
- package/lib/base/query-classes.d.ts +204 -33
- package/lib/base/query-classes.d.ts.map +1 -1
- package/lib/base/query-classes.js +276 -0
- package/lib/base/query-classes.js.map +1 -1
- package/lib/base/thenable.d.ts +7 -7
- package/lib/base/thenable.d.ts.map +1 -1
- package/lib/base/thenable.js +5 -4
- package/lib/base/thenable.js.map +1 -1
- package/lib/base/transaction.d.ts +46 -0
- package/lib/base/transaction.d.ts.map +1 -0
- package/lib/base/transaction.js +186 -0
- package/lib/base/transaction.js.map +1 -0
- package/lib/dialects/memory.d.ts +12 -7
- package/lib/dialects/memory.d.ts.map +1 -1
- package/lib/dialects/memory.js +7 -4
- package/lib/dialects/memory.js.map +1 -1
- package/lib/dialects/mongodb.d.ts +11 -7
- package/lib/dialects/mongodb.d.ts.map +1 -1
- package/lib/dialects/mongodb.js +18 -15
- package/lib/dialects/mongodb.js.map +1 -1
- package/lib/dialects/mysql.d.ts +35 -6
- package/lib/dialects/mysql.d.ts.map +1 -1
- package/lib/dialects/mysql.js +137 -18
- package/lib/dialects/mysql.js.map +1 -1
- package/lib/dialects/pg.d.ts +35 -6
- package/lib/dialects/pg.d.ts.map +1 -1
- package/lib/dialects/pg.js +137 -18
- package/lib/dialects/pg.js.map +1 -1
- package/lib/dialects/redis.d.ts +11 -6
- package/lib/dialects/redis.d.ts.map +1 -1
- package/lib/dialects/redis.js +11 -8
- package/lib/dialects/redis.js.map +1 -1
- package/lib/dialects/sqlite.d.ts +19 -6
- package/lib/dialects/sqlite.d.ts.map +1 -1
- package/lib/dialects/sqlite.js +63 -10
- package/lib/dialects/sqlite.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/migration.d.ts +132 -0
- package/lib/migration.d.ts.map +1 -0
- package/lib/migration.js +475 -0
- package/lib/migration.js.map +1 -0
- package/lib/registry.d.ts +26 -23
- package/lib/registry.d.ts.map +1 -1
- package/lib/registry.js +1 -5
- package/lib/registry.js.map +1 -1
- package/lib/type/document/database.d.ts +11 -11
- package/lib/type/document/database.d.ts.map +1 -1
- package/lib/type/document/database.js.map +1 -1
- package/lib/type/document/model.d.ts +7 -7
- package/lib/type/document/model.d.ts.map +1 -1
- package/lib/type/document/model.js.map +1 -1
- package/lib/type/keyvalue/database.d.ts +11 -11
- package/lib/type/keyvalue/database.d.ts.map +1 -1
- package/lib/type/keyvalue/database.js.map +1 -1
- package/lib/type/keyvalue/model.d.ts +2 -2
- package/lib/type/keyvalue/model.d.ts.map +1 -1
- package/lib/type/keyvalue/model.js.map +1 -1
- package/lib/type/related/database.d.ts +48 -13
- package/lib/type/related/database.d.ts.map +1 -1
- package/lib/type/related/database.js +258 -27
- package/lib/type/related/database.js.map +1 -1
- package/lib/type/related/model.d.ts +251 -15
- package/lib/type/related/model.d.ts.map +1 -1
- package/lib/type/related/model.js +647 -22
- package/lib/type/related/model.js.map +1 -1
- package/lib/types.d.ts +475 -37
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +6 -0
- package/lib/types.js.map +1 -1
- package/package.json +14 -5
- package/src/base/database.ts +168 -24
- package/src/base/dialect.ts +49 -10
- package/src/base/index.ts +2 -1
- package/src/base/model.ts +258 -18
- package/src/base/query-classes.ts +471 -63
- package/src/base/thenable.ts +12 -11
- package/src/base/transaction.ts +213 -0
- package/src/dialects/memory.ts +14 -13
- package/src/dialects/mongodb.ts +40 -38
- package/src/dialects/mysql.ts +151 -22
- package/src/dialects/pg.ts +148 -21
- package/src/dialects/redis.ts +40 -38
- package/src/dialects/sqlite.ts +73 -15
- package/src/index.ts +1 -2
- package/src/migration.ts +544 -0
- package/src/registry.ts +33 -33
- package/src/type/document/database.ts +32 -32
- package/src/type/document/model.ts +14 -14
- package/src/type/keyvalue/database.ts +32 -32
- package/src/type/keyvalue/model.ts +18 -18
- package/src/type/related/database.ts +309 -34
- package/src/type/related/model.ts +800 -33
- package/src/types.ts +559 -44
- package/tests/database.test.ts +1738 -0
package/src/base/thenable.ts
CHANGED
|
@@ -3,17 +3,17 @@ import type { Dialect } from "./dialect.js";
|
|
|
3
3
|
import { QueryParams } from "../types.js";
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
export abstract class ThenableQuery<T
|
|
7
|
-
implements PromiseLike<
|
|
6
|
+
export abstract class ThenableQuery<R,S extends Record<string, object>, T extends keyof S,C=any,D=string>
|
|
7
|
+
implements PromiseLike<R>, AsyncIterable<R>
|
|
8
8
|
{
|
|
9
|
-
protected constructor(protected readonly database: Database<C,
|
|
9
|
+
protected constructor(protected readonly database: Database<C,S,D>,protected readonly dialect: Dialect<C,S,D>) {}
|
|
10
10
|
|
|
11
11
|
// Abstract method to get query parameters
|
|
12
|
-
protected abstract getQueryParams(): QueryParams
|
|
12
|
+
protected abstract getQueryParams(): QueryParams<S,T>;
|
|
13
13
|
[Symbol.toStringTag] = 'ThenableQuery';
|
|
14
|
-
then<TResult1 =
|
|
14
|
+
then<TResult1 = R, TResult2 = never>(
|
|
15
15
|
onfulfilled?:
|
|
16
|
-
| ((value:
|
|
16
|
+
| ((value: R) => TResult1 | PromiseLike<TResult1>)
|
|
17
17
|
| undefined
|
|
18
18
|
| null,
|
|
19
19
|
onrejected?:
|
|
@@ -23,7 +23,8 @@ export abstract class ThenableQuery<T = any,C=any,D=string>
|
|
|
23
23
|
): Promise<TResult1 | TResult2> {
|
|
24
24
|
const params = this.getQueryParams();
|
|
25
25
|
const { query, params: queryParams } = this.database.buildQuery(params);
|
|
26
|
-
|
|
26
|
+
// 使用 database.query 以支持日志记录
|
|
27
|
+
return this.database.query<R>(query, queryParams).then(onfulfilled, onrejected);
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
catch<TResult = never>(
|
|
@@ -34,19 +35,19 @@ export abstract class ThenableQuery<T = any,C=any,D=string>
|
|
|
34
35
|
): Promise<any | TResult> {
|
|
35
36
|
const params = this.getQueryParams();
|
|
36
37
|
const { query, params: queryParams } = this.database.buildQuery(params);
|
|
37
|
-
return this.
|
|
38
|
+
return this.database.query(query, queryParams).catch(onrejected);
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
finally(onfinally?: (() => void) | undefined | null): Promise<any> {
|
|
41
42
|
const params = this.getQueryParams();
|
|
42
43
|
const { query, params: queryParams } = this.database.buildQuery(params);
|
|
43
|
-
return this.
|
|
44
|
+
return this.database.query(query, queryParams).finally(onfinally);
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
async *[Symbol.asyncIterator](): AsyncIterator<
|
|
47
|
+
async *[Symbol.asyncIterator](): AsyncIterator<R, void, unknown> {
|
|
47
48
|
const params = this.getQueryParams();
|
|
48
49
|
const { query, params: queryParams } = this.database.buildQuery(params);
|
|
49
|
-
const rows = await this.
|
|
50
|
+
const rows = await this.database.query(query, queryParams);
|
|
50
51
|
for (const row of Array.isArray(rows) ? rows : [rows]) {
|
|
51
52
|
yield row;
|
|
52
53
|
}
|
|
@@ -0,0 +1,213 @@
|
|
|
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
|
+
|
package/src/dialects/memory.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import {Dialect} from '../base/index.js';
|
|
2
|
-
import {MemoryConfig} from "../types.js";
|
|
3
2
|
import {RelatedDatabase} from "../type/related/database.js";
|
|
4
3
|
import {Registry} from "../registry.js";
|
|
5
|
-
import {Database} from "../base/index.js";
|
|
6
4
|
|
|
7
5
|
interface MemoryTable {
|
|
8
6
|
name: string;
|
|
@@ -16,8 +14,8 @@ interface MemoryIndex {
|
|
|
16
14
|
columns: string[];
|
|
17
15
|
unique: boolean;
|
|
18
16
|
}
|
|
19
|
-
|
|
20
|
-
export class MemoryDialect extends Dialect<MemoryConfig,string> {
|
|
17
|
+
export interface MemoryConfig{}
|
|
18
|
+
export class MemoryDialect<S extends Record<string, object>> extends Dialect<MemoryConfig,S, string> {
|
|
21
19
|
private connected = false;
|
|
22
20
|
private tables: Map<string, MemoryTable> = new Map();
|
|
23
21
|
private autoIncrementCounters: Map<string, Map<string, number>> = new Map();
|
|
@@ -96,8 +94,8 @@ export class MemoryDialect extends Dialect<MemoryConfig,string> {
|
|
|
96
94
|
return typeMap[type.toLowerCase()] || 'TEXT';
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
quoteIdentifier(identifier: string): string {
|
|
100
|
-
return `"${identifier}"`;
|
|
97
|
+
quoteIdentifier(identifier: string|symbol|number): string {
|
|
98
|
+
return `"${String(identifier)}"`;
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
getParameterPlaceholder(index: number): string {
|
|
@@ -152,20 +150,20 @@ export class MemoryDialect extends Dialect<MemoryConfig,string> {
|
|
|
152
150
|
return `LIMIT ${limit} OFFSET ${offset}`;
|
|
153
151
|
}
|
|
154
152
|
|
|
155
|
-
formatCreateTable(tableName:
|
|
153
|
+
formatCreateTable(tableName: keyof S, columns: string[]): string {
|
|
156
154
|
return `CREATE TABLE ${this.quoteIdentifier(tableName)} (${columns.join(', ')})`;
|
|
157
155
|
}
|
|
158
156
|
|
|
159
|
-
formatAlterTable(tableName:
|
|
157
|
+
formatAlterTable(tableName: keyof S, alterations: string[]): string {
|
|
160
158
|
return `ALTER TABLE ${this.quoteIdentifier(tableName)} ${alterations.join(', ')}`;
|
|
161
159
|
}
|
|
162
160
|
|
|
163
|
-
formatDropTable(tableName:
|
|
161
|
+
formatDropTable(tableName: keyof S, ifExists?: boolean): string {
|
|
164
162
|
const ifExistsClause = ifExists ? 'IF EXISTS ' : '';
|
|
165
163
|
return `DROP TABLE ${ifExistsClause}${this.quoteIdentifier(tableName)}`;
|
|
166
164
|
}
|
|
167
165
|
|
|
168
|
-
formatDropIndex(indexName: string, tableName:
|
|
166
|
+
formatDropIndex(indexName: string, tableName: keyof S, ifExists?: boolean): string {
|
|
169
167
|
const ifExistsClause = ifExists ? 'IF EXISTS ' : '';
|
|
170
168
|
return `DROP INDEX ${ifExistsClause}${this.quoteIdentifier(indexName)} ON ${this.quoteIdentifier(tableName)}`;
|
|
171
169
|
}
|
|
@@ -876,6 +874,9 @@ export class MemoryDialect extends Dialect<MemoryConfig,string> {
|
|
|
876
874
|
});
|
|
877
875
|
}
|
|
878
876
|
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
877
|
+
export class Memory<S extends Record<string, object>> extends RelatedDatabase<MemoryConfig, S> {
|
|
878
|
+
constructor(config: MemoryConfig = {}) {
|
|
879
|
+
super(new MemoryDialect<S>(config));
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
Registry.register('memory', Memory);
|
package/src/dialects/mongodb.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface MongoDBDialectConfig extends MongoClientOptions {
|
|
|
16
16
|
url: string;
|
|
17
17
|
dbName: string;
|
|
18
18
|
}
|
|
19
|
-
export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryResult> {
|
|
19
|
+
export class MongoDBDialect<S extends Record<string, object> = Record<string, object>> extends Dialect<MongoDBDialectConfig, S, DocumentQueryResult> {
|
|
20
20
|
private client: any = null;
|
|
21
21
|
private db: any = null;
|
|
22
22
|
|
|
@@ -139,24 +139,24 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
139
139
|
/**
|
|
140
140
|
* 构建查询
|
|
141
141
|
*/
|
|
142
|
-
buildQuery<
|
|
142
|
+
buildQuery<T extends keyof S>(params: QueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
143
143
|
switch (params.type) {
|
|
144
144
|
case 'create':
|
|
145
|
-
return this.buildCreateQuery(params as CreateQueryParams<
|
|
145
|
+
return this.buildCreateQuery(params as CreateQueryParams<S,T>);
|
|
146
146
|
case 'select':
|
|
147
|
-
return this.buildSelectQuery(params as SelectQueryParams<
|
|
147
|
+
return this.buildSelectQuery(params as SelectQueryParams<S,T>);
|
|
148
148
|
case 'insert':
|
|
149
|
-
return this.buildInsertQuery(params as InsertQueryParams<
|
|
149
|
+
return this.buildInsertQuery(params as InsertQueryParams<S,T>);
|
|
150
150
|
case 'update':
|
|
151
|
-
return this.buildUpdateQuery(params as UpdateQueryParams<
|
|
151
|
+
return this.buildUpdateQuery(params as UpdateQueryParams<S,T>);
|
|
152
152
|
case 'delete':
|
|
153
|
-
return this.buildDeleteQuery(params as DeleteQueryParams<
|
|
153
|
+
return this.buildDeleteQuery(params as DeleteQueryParams<S,T>);
|
|
154
154
|
case 'alter':
|
|
155
|
-
return this.buildAlterQuery(params as AlterQueryParams<
|
|
155
|
+
return this.buildAlterQuery(params as AlterQueryParams<S,T>);
|
|
156
156
|
case 'drop_table':
|
|
157
|
-
return this.buildDropTableQuery(params as DropTableQueryParams<
|
|
157
|
+
return this.buildDropTableQuery(params as DropTableQueryParams<S,T>);
|
|
158
158
|
case 'drop_index':
|
|
159
|
-
return this.buildDropIndexQuery(params as DropIndexQueryParams);
|
|
159
|
+
return this.buildDropIndexQuery(params as DropIndexQueryParams<S,T>);
|
|
160
160
|
default:
|
|
161
161
|
throw new Error(`不支持的查询类型: ${(params as any).type}`);
|
|
162
162
|
}
|
|
@@ -214,20 +214,20 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
214
214
|
return `${limit},${offset}`;
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
formatCreateTable(tableName:
|
|
218
|
-
return `CREATE TABLE ${tableName} (${columns.join(',')})`;
|
|
217
|
+
formatCreateTable(tableName: keyof S, columns: string[]): string {
|
|
218
|
+
return `CREATE TABLE ${String(tableName)} (${columns.join(',')})`;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
formatAlterTable(tableName:
|
|
222
|
-
return `ALTER TABLE ${tableName} ${alterations.join(',')}`;
|
|
221
|
+
formatAlterTable(tableName: keyof S, alterations: string[]): string {
|
|
222
|
+
return `ALTER TABLE ${String(tableName)} ${alterations.join(',')}`;
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
formatDropTable(tableName:
|
|
226
|
-
return `DROP TABLE ${tableName} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
225
|
+
formatDropTable(tableName: keyof S , ifExists?: boolean): string {
|
|
226
|
+
return `DROP TABLE ${String(tableName)} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
-
formatDropIndex(indexName: string, tableName:
|
|
230
|
-
return `DROP INDEX ${indexName} ON ${tableName} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
229
|
+
formatDropIndex(indexName: string, tableName: keyof S, ifExists?: boolean): string {
|
|
230
|
+
return `DROP INDEX ${indexName} ON ${String(tableName)} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
quoteIdentifier(identifier: string): string {
|
|
@@ -243,10 +243,10 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
245
|
// MongoDB 特定的查询构建方法
|
|
246
|
-
private buildCreateQuery<T extends
|
|
246
|
+
private buildCreateQuery<T extends keyof S>(params: CreateQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
247
247
|
return {
|
|
248
248
|
query: {
|
|
249
|
-
collection: params.tableName,
|
|
249
|
+
collection: String(params.tableName),
|
|
250
250
|
operation: 'createCollection',
|
|
251
251
|
filter: {},
|
|
252
252
|
projection: {}
|
|
@@ -255,7 +255,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
255
255
|
};
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
private buildSelectQuery<T extends
|
|
258
|
+
private buildSelectQuery<T extends keyof S>(params: SelectQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
259
259
|
const filter: Record<string, any> = {};
|
|
260
260
|
|
|
261
261
|
// 转换条件为 MongoDB 查询格式
|
|
@@ -264,7 +264,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
264
264
|
}
|
|
265
265
|
|
|
266
266
|
const query: DocumentQueryResult = {
|
|
267
|
-
collection: params.tableName,
|
|
267
|
+
collection: String(params.tableName),
|
|
268
268
|
operation: 'find',
|
|
269
269
|
filter
|
|
270
270
|
};
|
|
@@ -297,10 +297,10 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
297
297
|
};
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
-
private buildInsertQuery<T extends
|
|
300
|
+
private buildInsertQuery<T extends keyof S>(params: InsertQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
301
301
|
return {
|
|
302
302
|
query: {
|
|
303
|
-
collection: params.tableName,
|
|
303
|
+
collection: String(params.tableName),
|
|
304
304
|
operation: 'insertOne',
|
|
305
305
|
filter: {},
|
|
306
306
|
projection: {}
|
|
@@ -309,7 +309,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
309
309
|
};
|
|
310
310
|
}
|
|
311
311
|
|
|
312
|
-
private buildUpdateQuery<T extends
|
|
312
|
+
private buildUpdateQuery<T extends keyof S>(params: UpdateQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
313
313
|
const filter: Record<string, any> = {};
|
|
314
314
|
|
|
315
315
|
if (params.conditions) {
|
|
@@ -318,7 +318,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
318
318
|
|
|
319
319
|
return {
|
|
320
320
|
query: {
|
|
321
|
-
collection: params.tableName,
|
|
321
|
+
collection: String(params.tableName),
|
|
322
322
|
operation: 'updateMany',
|
|
323
323
|
filter,
|
|
324
324
|
projection: {}
|
|
@@ -327,7 +327,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
327
327
|
};
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
-
private buildDeleteQuery<T extends
|
|
330
|
+
private buildDeleteQuery<T extends keyof S>(params: DeleteQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
331
331
|
const filter: Record<string, any> = {};
|
|
332
332
|
|
|
333
333
|
if (params.conditions) {
|
|
@@ -336,7 +336,7 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
336
336
|
|
|
337
337
|
return {
|
|
338
338
|
query: {
|
|
339
|
-
collection: params.tableName,
|
|
339
|
+
collection: String(params.tableName),
|
|
340
340
|
operation: 'deleteMany',
|
|
341
341
|
filter,
|
|
342
342
|
projection: {}
|
|
@@ -345,10 +345,10 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
345
345
|
};
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
-
private buildAlterQuery<T extends
|
|
348
|
+
private buildAlterQuery<T extends keyof S>(params: AlterQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
349
349
|
return {
|
|
350
350
|
query: {
|
|
351
|
-
collection: params.tableName,
|
|
351
|
+
collection: String(params.tableName),
|
|
352
352
|
operation: 'createIndex',
|
|
353
353
|
filter: {},
|
|
354
354
|
projection: {}
|
|
@@ -357,10 +357,10 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
357
357
|
};
|
|
358
358
|
}
|
|
359
359
|
|
|
360
|
-
private buildDropTableQuery<T extends
|
|
360
|
+
private buildDropTableQuery<T extends keyof S>(params: DropTableQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
361
361
|
return {
|
|
362
362
|
query: {
|
|
363
|
-
collection: params.tableName,
|
|
363
|
+
collection: String(params.tableName),
|
|
364
364
|
operation: 'dropCollection',
|
|
365
365
|
filter: {},
|
|
366
366
|
projection: {}
|
|
@@ -369,10 +369,10 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
369
369
|
};
|
|
370
370
|
}
|
|
371
371
|
|
|
372
|
-
private buildDropIndexQuery(params: DropIndexQueryParams): BuildQueryResult<DocumentQueryResult> {
|
|
372
|
+
private buildDropIndexQuery<T extends keyof S>(params: DropIndexQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
373
373
|
return {
|
|
374
374
|
query: {
|
|
375
|
-
collection: params.tableName,
|
|
375
|
+
collection: String(params.tableName),
|
|
376
376
|
operation: 'dropIndex',
|
|
377
377
|
filter: {},
|
|
378
378
|
projection: {}
|
|
@@ -527,7 +527,9 @@ export class MongoDBDialect extends Dialect<MongoDBDialectConfig, DocumentQueryR
|
|
|
527
527
|
};
|
|
528
528
|
}
|
|
529
529
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
}
|
|
530
|
+
export class MongoDB<S extends Record<string, object> = Record<string, object>> extends DocumentDatabase<MongoDBDialectConfig, S> {
|
|
531
|
+
constructor(config: MongoDBDialectConfig, definitions?: Database.DefinitionObj<S>) {
|
|
532
|
+
super(new MongoDBDialect<S>(config), definitions);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
Registry.register('mongodb', MongoDB);
|