@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/src/dialects/mongodb.ts
DELETED
|
@@ -1,535 +0,0 @@
|
|
|
1
|
-
import {Database, Dialect} from '../base/index.js';
|
|
2
|
-
import {
|
|
3
|
-
AlterQueryParams,
|
|
4
|
-
BuildQueryResult,
|
|
5
|
-
CreateQueryParams, DatabaseDialect, DeleteQueryParams,
|
|
6
|
-
DocumentQueryResult, DropIndexQueryParams, DropTableQueryParams, InsertQueryParams,
|
|
7
|
-
QueryParams,
|
|
8
|
-
SelectQueryParams, UpdateQueryParams
|
|
9
|
-
} from "../types.js";
|
|
10
|
-
import {DocumentDatabase} from "../type/document/database.js";
|
|
11
|
-
import {Registry} from "../registry.js";
|
|
12
|
-
import type { MongoClientOptions } from 'mongodb';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export interface MongoDBDialectConfig extends MongoClientOptions {
|
|
16
|
-
url: string;
|
|
17
|
-
dbName: string;
|
|
18
|
-
}
|
|
19
|
-
export class MongoDBDialect<S extends Record<string, object> = Record<string, object>> extends Dialect<MongoDBDialectConfig, S, DocumentQueryResult> {
|
|
20
|
-
private client: any = null;
|
|
21
|
-
private db: any = null;
|
|
22
|
-
|
|
23
|
-
constructor(config: MongoDBDialectConfig) {
|
|
24
|
-
super('mongodb', config);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 检查是否已连接
|
|
29
|
-
*/
|
|
30
|
-
isConnected(): boolean {
|
|
31
|
-
return this.client !== null && this.db !== null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 连接数据库
|
|
36
|
-
*/
|
|
37
|
-
async connect(): Promise<void> {
|
|
38
|
-
return this.start();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 断开连接
|
|
43
|
-
*/
|
|
44
|
-
async disconnect(): Promise<void> {
|
|
45
|
-
return this.stop();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 健康检查
|
|
50
|
-
*/
|
|
51
|
-
async healthCheck(): Promise<boolean> {
|
|
52
|
-
if (!this.isConnected()) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
await this.db.admin().ping();
|
|
57
|
-
return true;
|
|
58
|
-
} catch {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 启动连接
|
|
65
|
-
*/
|
|
66
|
-
async start(): Promise<void> {
|
|
67
|
-
try {
|
|
68
|
-
// 动态导入 mongodb 客户端
|
|
69
|
-
const { MongoClient } = await import('mongodb');
|
|
70
|
-
|
|
71
|
-
this.client = new MongoClient(this.config.url, this.config);
|
|
72
|
-
await this.client.connect();
|
|
73
|
-
this.db = this.client.db(this.config.dbName);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
console.error('forgot install mongodb ?');
|
|
76
|
-
throw new Error(`MongoDB 连接失败: ${error}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 停止连接
|
|
82
|
-
*/
|
|
83
|
-
async stop(): Promise<void> {
|
|
84
|
-
if (this.client) {
|
|
85
|
-
await this.client.close();
|
|
86
|
-
this.client = null;
|
|
87
|
-
this.db = null;
|
|
88
|
-
console.log('MongoDB 连接已关闭');
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* 释放资源
|
|
94
|
-
*/
|
|
95
|
-
async dispose(): Promise<void> {
|
|
96
|
-
return this.stop();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 执行查询
|
|
101
|
-
*/
|
|
102
|
-
async query<T = any>(query: DocumentQueryResult, params: any[] = []): Promise<T> {
|
|
103
|
-
if (!this.db) {
|
|
104
|
-
throw new Error('MongoDB 未连接');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
const collection = this.db.collection(query.collection);
|
|
109
|
-
|
|
110
|
-
switch (query.operation || 'find') {
|
|
111
|
-
case 'find':
|
|
112
|
-
return await this.executeFind(collection, query) as T;
|
|
113
|
-
case 'insertOne':
|
|
114
|
-
return await this.executeInsertOne(collection, query, params) as T;
|
|
115
|
-
case 'insertMany':
|
|
116
|
-
return await this.executeInsertMany(collection, query, params) as T;
|
|
117
|
-
case 'updateOne':
|
|
118
|
-
return await this.executeUpdateOne(collection, query, params) as T;
|
|
119
|
-
case 'updateMany':
|
|
120
|
-
return await this.executeUpdateMany(collection, query, params) as T;
|
|
121
|
-
case 'deleteOne':
|
|
122
|
-
return await this.executeDeleteOne(collection, query) as T;
|
|
123
|
-
case 'deleteMany':
|
|
124
|
-
return await this.executeDeleteMany(collection, query) as T;
|
|
125
|
-
case 'createIndex':
|
|
126
|
-
return await this.executeCreateIndex(collection, query, params) as T;
|
|
127
|
-
case 'dropIndex':
|
|
128
|
-
return await this.executeDropIndex(collection, query, params) as T;
|
|
129
|
-
case 'dropCollection':
|
|
130
|
-
return await this.executeDropCollection(collection) as T;
|
|
131
|
-
default:
|
|
132
|
-
throw new Error(`不支持的 MongoDB 操作: ${query.operation}`);
|
|
133
|
-
}
|
|
134
|
-
} catch (error) {
|
|
135
|
-
throw new Error(`MongoDB 查询执行失败: ${error}`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 构建查询
|
|
141
|
-
*/
|
|
142
|
-
buildQuery<T extends keyof S>(params: QueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
143
|
-
switch (params.type) {
|
|
144
|
-
case 'create':
|
|
145
|
-
return this.buildCreateQuery(params as CreateQueryParams<S,T>);
|
|
146
|
-
case 'select':
|
|
147
|
-
return this.buildSelectQuery(params as SelectQueryParams<S,T>);
|
|
148
|
-
case 'insert':
|
|
149
|
-
return this.buildInsertQuery(params as InsertQueryParams<S,T>);
|
|
150
|
-
case 'update':
|
|
151
|
-
return this.buildUpdateQuery(params as UpdateQueryParams<S,T>);
|
|
152
|
-
case 'delete':
|
|
153
|
-
return this.buildDeleteQuery(params as DeleteQueryParams<S,T>);
|
|
154
|
-
case 'alter':
|
|
155
|
-
return this.buildAlterQuery(params as AlterQueryParams<S,T>);
|
|
156
|
-
case 'drop_table':
|
|
157
|
-
return this.buildDropTableQuery(params as DropTableQueryParams<S,T>);
|
|
158
|
-
case 'drop_index':
|
|
159
|
-
return this.buildDropIndexQuery(params as DropIndexQueryParams<S,T>);
|
|
160
|
-
default:
|
|
161
|
-
throw new Error(`不支持的查询类型: ${(params as any).type}`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// 实现 Dialect 接口的所有必需方法
|
|
166
|
-
mapColumnType(type: string): string {
|
|
167
|
-
switch (type) {
|
|
168
|
-
case 'string':
|
|
169
|
-
return 'String';
|
|
170
|
-
case 'integer':
|
|
171
|
-
return 'Int32';
|
|
172
|
-
case 'float':
|
|
173
|
-
return 'Double';
|
|
174
|
-
case 'boolean':
|
|
175
|
-
return 'Boolean';
|
|
176
|
-
case 'date':
|
|
177
|
-
return 'Date';
|
|
178
|
-
case 'json':
|
|
179
|
-
return 'Object';
|
|
180
|
-
default:
|
|
181
|
-
return type;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
formatBoolean(value: boolean): string {
|
|
186
|
-
return value ? 'true' : 'false';
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
formatDate(value: Date): string {
|
|
190
|
-
return value.toISOString();
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
formatJson(value: any): string {
|
|
194
|
-
return JSON.stringify(value);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
escapeString(value: string): string {
|
|
198
|
-
return value;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
formatDefaultValue(value: any): string {
|
|
202
|
-
return value;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
formatLimit(limit: number): string {
|
|
206
|
-
return `${limit}`;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
formatOffset(offset: number): string {
|
|
210
|
-
return `${offset}`;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
formatLimitOffset(limit: number, offset: number): string {
|
|
214
|
-
return `${limit},${offset}`;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
formatCreateTable(tableName: keyof S, columns: string[]): string {
|
|
218
|
-
return `CREATE TABLE ${String(tableName)} (${columns.join(',')})`;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
formatAlterTable(tableName: keyof S, alterations: string[]): string {
|
|
222
|
-
return `ALTER TABLE ${String(tableName)} ${alterations.join(',')}`;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
formatDropTable(tableName: keyof S , ifExists?: boolean): string {
|
|
226
|
-
return `DROP TABLE ${String(tableName)} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
formatDropIndex(indexName: string, tableName: keyof S, ifExists?: boolean): string {
|
|
230
|
-
return `DROP INDEX ${indexName} ON ${String(tableName)} ${ifExists ? 'IF EXISTS' : ''}`;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
quoteIdentifier(identifier: string): string {
|
|
234
|
-
return identifier;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
getParameterPlaceholder(index: number): string {
|
|
238
|
-
return `$${index}`;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
getStatementTerminator(): string {
|
|
242
|
-
return ';';
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// MongoDB 特定的查询构建方法
|
|
246
|
-
private buildCreateQuery<T extends keyof S>(params: CreateQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
247
|
-
return {
|
|
248
|
-
query: {
|
|
249
|
-
collection: String(params.tableName),
|
|
250
|
-
operation: 'createCollection',
|
|
251
|
-
filter: {},
|
|
252
|
-
projection: {}
|
|
253
|
-
},
|
|
254
|
-
params: []
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private buildSelectQuery<T extends keyof S>(params: SelectQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
259
|
-
const filter: Record<string, any> = {};
|
|
260
|
-
|
|
261
|
-
// 转换条件为 MongoDB 查询格式
|
|
262
|
-
if (params.conditions) {
|
|
263
|
-
this.convertConditionToMongoFilter(params.conditions, filter);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const query: DocumentQueryResult = {
|
|
267
|
-
collection: String(params.tableName),
|
|
268
|
-
operation: 'find',
|
|
269
|
-
filter
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
if (params.orderings) {
|
|
273
|
-
query.sort = {};
|
|
274
|
-
for (const order of params.orderings) {
|
|
275
|
-
query.sort[order.field as string] = order.direction === 'ASC' ? 1 : -1;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
if (params.limitCount) {
|
|
280
|
-
query.limit = params.limitCount;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (params.offsetCount) {
|
|
284
|
-
query.skip = params.offsetCount;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
if (params.fields && params.fields.length > 0) {
|
|
288
|
-
query.projection = {};
|
|
289
|
-
for (const field of params.fields) {
|
|
290
|
-
query.projection[field as string] = 1;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return {
|
|
295
|
-
query,
|
|
296
|
-
params: []
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
private buildInsertQuery<T extends keyof S>(params: InsertQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
301
|
-
return {
|
|
302
|
-
query: {
|
|
303
|
-
collection: String(params.tableName),
|
|
304
|
-
operation: 'insertOne',
|
|
305
|
-
filter: {},
|
|
306
|
-
projection: {}
|
|
307
|
-
},
|
|
308
|
-
params: [params.data]
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
private buildUpdateQuery<T extends keyof S>(params: UpdateQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
313
|
-
const filter: Record<string, any> = {};
|
|
314
|
-
|
|
315
|
-
if (params.conditions) {
|
|
316
|
-
this.convertConditionToMongoFilter(params.conditions, filter);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
return {
|
|
320
|
-
query: {
|
|
321
|
-
collection: String(params.tableName),
|
|
322
|
-
operation: 'updateMany',
|
|
323
|
-
filter,
|
|
324
|
-
projection: {}
|
|
325
|
-
},
|
|
326
|
-
params: [params.update]
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
private buildDeleteQuery<T extends keyof S>(params: DeleteQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
331
|
-
const filter: Record<string, any> = {};
|
|
332
|
-
|
|
333
|
-
if (params.conditions) {
|
|
334
|
-
this.convertConditionToMongoFilter(params.conditions, filter);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return {
|
|
338
|
-
query: {
|
|
339
|
-
collection: String(params.tableName),
|
|
340
|
-
operation: 'deleteMany',
|
|
341
|
-
filter,
|
|
342
|
-
projection: {}
|
|
343
|
-
},
|
|
344
|
-
params: []
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
private buildAlterQuery<T extends keyof S>(params: AlterQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
349
|
-
return {
|
|
350
|
-
query: {
|
|
351
|
-
collection: String(params.tableName),
|
|
352
|
-
operation: 'createIndex',
|
|
353
|
-
filter: {},
|
|
354
|
-
projection: {}
|
|
355
|
-
},
|
|
356
|
-
params: [params.alterations]
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
private buildDropTableQuery<T extends keyof S>(params: DropTableQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
361
|
-
return {
|
|
362
|
-
query: {
|
|
363
|
-
collection: String(params.tableName),
|
|
364
|
-
operation: 'dropCollection',
|
|
365
|
-
filter: {},
|
|
366
|
-
projection: {}
|
|
367
|
-
},
|
|
368
|
-
params: []
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
private buildDropIndexQuery<T extends keyof S>(params: DropIndexQueryParams<S,T>): BuildQueryResult<DocumentQueryResult> {
|
|
373
|
-
return {
|
|
374
|
-
query: {
|
|
375
|
-
collection: String(params.tableName),
|
|
376
|
-
operation: 'dropIndex',
|
|
377
|
-
filter: {},
|
|
378
|
-
projection: {}
|
|
379
|
-
},
|
|
380
|
-
params: [params.indexName]
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
private convertConditionToMongoFilter(condition: any, filter: Record<string, any>): void {
|
|
385
|
-
if (typeof condition !== 'object' || condition === null) {
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
for (const [key, value] of Object.entries(condition)) {
|
|
390
|
-
if (key.startsWith('$')) {
|
|
391
|
-
// 逻辑操作符
|
|
392
|
-
if (key === '$and' && Array.isArray(value)) {
|
|
393
|
-
filter.$and = value.map(cond => {
|
|
394
|
-
const subFilter: Record<string, any> = {};
|
|
395
|
-
this.convertConditionToMongoFilter(cond, subFilter);
|
|
396
|
-
return subFilter;
|
|
397
|
-
});
|
|
398
|
-
} else if (key === '$or' && Array.isArray(value)) {
|
|
399
|
-
filter.$or = value.map(cond => {
|
|
400
|
-
const subFilter: Record<string, any> = {};
|
|
401
|
-
this.convertConditionToMongoFilter(cond, subFilter);
|
|
402
|
-
return subFilter;
|
|
403
|
-
});
|
|
404
|
-
} else if (key === '$not') {
|
|
405
|
-
const subFilter: Record<string, any> = {};
|
|
406
|
-
this.convertConditionToMongoFilter(value, subFilter);
|
|
407
|
-
filter.$not = subFilter;
|
|
408
|
-
} else {
|
|
409
|
-
filter[key] = value;
|
|
410
|
-
}
|
|
411
|
-
} else if (typeof value === 'object' && value !== null) {
|
|
412
|
-
// 比较操作符
|
|
413
|
-
const fieldFilter: Record<string, any> = {};
|
|
414
|
-
for (const [op, opValue] of Object.entries(value)) {
|
|
415
|
-
if (op.startsWith('$')) {
|
|
416
|
-
fieldFilter[op] = opValue;
|
|
417
|
-
} else {
|
|
418
|
-
fieldFilter.$eq = value;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
filter[key] = Object.keys(fieldFilter).length > 0 ? fieldFilter : value;
|
|
422
|
-
} else {
|
|
423
|
-
// 简单相等
|
|
424
|
-
filter[key] = value;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
// MongoDB 执行方法
|
|
430
|
-
private async executeFind(collection: any, query: DocumentQueryResult): Promise<any[]> {
|
|
431
|
-
let cursor = collection.find(query.filter);
|
|
432
|
-
|
|
433
|
-
if (query.sort) {
|
|
434
|
-
cursor = cursor.sort(query.sort);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
if (query.skip) {
|
|
438
|
-
cursor = cursor.skip(query.skip);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
if (query.limit) {
|
|
442
|
-
cursor = cursor.limit(query.limit);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
if (query.projection) {
|
|
446
|
-
cursor = cursor.projection(query.projection);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
return await cursor.toArray();
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
private async executeInsertOne(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
453
|
-
const result = await collection.insertOne(params[0]);
|
|
454
|
-
return [{ insertedId: result.insertedId, ...params[0] }];
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
private async executeInsertMany(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
458
|
-
const result = await collection.insertMany(params);
|
|
459
|
-
return Object.values(result.insertedIds).map((id, index) => ({
|
|
460
|
-
insertedId: id,
|
|
461
|
-
...params[index]
|
|
462
|
-
}));
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
private async executeUpdateOne(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
466
|
-
const result = await collection.updateOne(query.filter, { $set: params[0] });
|
|
467
|
-
return [{ modifiedCount: result.modifiedCount, matchedCount: result.matchedCount }];
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
private async executeUpdateMany(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
471
|
-
const result = await collection.updateMany(query.filter, { $set: params[0] });
|
|
472
|
-
return [{ modifiedCount: result.modifiedCount, matchedCount: result.matchedCount }];
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
private async executeDeleteOne(collection: any, query: DocumentQueryResult): Promise<any[]> {
|
|
476
|
-
const result = await collection.deleteOne(query.filter);
|
|
477
|
-
return [{ deletedCount: result.deletedCount }];
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
private async executeDeleteMany(collection: any, query: DocumentQueryResult): Promise<any[]> {
|
|
481
|
-
const result = await collection.deleteMany(query.filter);
|
|
482
|
-
return [{ deletedCount: result.deletedCount }];
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
private async executeCreateIndex(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
486
|
-
const result = await collection.createIndex(params[0]);
|
|
487
|
-
return [{ indexName: result }];
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
private async executeDropIndex(collection: any, query: DocumentQueryResult, params: any[]): Promise<any[]> {
|
|
491
|
-
const result = await collection.dropIndex(params[0]);
|
|
492
|
-
return [{ result }];
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
private async executeDropCollection(collection: any): Promise<any[]> {
|
|
496
|
-
const result = await collection.drop();
|
|
497
|
-
return [{ result }];
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
get dialectInfo(): DatabaseDialect {
|
|
501
|
-
return {
|
|
502
|
-
name: this.name,
|
|
503
|
-
version: '1.0.0',
|
|
504
|
-
features: [
|
|
505
|
-
'document_storage',
|
|
506
|
-
'indexing',
|
|
507
|
-
'aggregation',
|
|
508
|
-
'transactions',
|
|
509
|
-
'replica_sets',
|
|
510
|
-
'sharding'
|
|
511
|
-
],
|
|
512
|
-
dataTypes: {
|
|
513
|
-
'string': 'String',
|
|
514
|
-
'integer': 'Int32',
|
|
515
|
-
'float': 'Double',
|
|
516
|
-
'boolean': 'Boolean',
|
|
517
|
-
'date': 'Date',
|
|
518
|
-
'json': 'Object'
|
|
519
|
-
},
|
|
520
|
-
identifierQuote: '',
|
|
521
|
-
parameterPlaceholder: '?',
|
|
522
|
-
supportsTransactions: true,
|
|
523
|
-
supportsIndexes: true,
|
|
524
|
-
supportsForeignKeys: false,
|
|
525
|
-
supportsViews: false,
|
|
526
|
-
supportsStoredProcedures: false
|
|
527
|
-
};
|
|
528
|
-
}
|
|
529
|
-
}
|
|
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);
|