/
MariaDbConnection.ts
63 lines (48 loc) · 1.68 KB
/
MariaDbConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { Connection } from 'mariadb';
import type { Knex } from '@mikro-orm/knex';
import { AbstractSqlConnection, MonkeyPatchable } from '@mikro-orm/knex';
export class MariaDbConnection extends AbstractSqlConnection {
async connect(): Promise<void> {
this.client = this.createKnexClient(this.getPatchedDialect());
}
private getPatchedDialect() {
const { MySqlDialect } = MonkeyPatchable;
MySqlDialect.prototype.driverName = 'mariadb';
MySqlDialect.prototype._driver = () => require('mariadb/callback');
MySqlDialect.prototype.validateConnection = (connection: Connection) => connection.isValid();
return MySqlDialect;
}
getDefaultClientUrl(): string {
return 'mysql://root@127.0.0.1:3306';
}
getConnectionOptions(): Knex.MySqlConnectionConfig {
const ret = super.getConnectionOptions() as Knex.MySqlConnectionConfig;
if (this.config.get('multipleStatements')) {
ret.multipleStatements = this.config.get('multipleStatements');
}
if (this.config.get('forceUtcTimezone')) {
ret.timezone = 'Z';
}
if (this.config.get('timezone')) {
ret.timezone = this.config.get('timezone');
}
ret.bigNumberStrings = true;
ret.supportBigNumbers = true;
// @ts-ignore
ret.checkDuplicate = false;
return ret;
}
protected transformRawResult<T>(res: any, method: 'all' | 'get' | 'run'): T {
if (method === 'run' && ['OkPacket', 'ResultSetHeader'].includes(res[0].constructor.name)) {
return {
insertId: res[0].insertId,
affectedRows: res[0].affectedRows,
rows: [],
} as unknown as T;
}
if (method === 'get') {
return res[0][0];
}
return res[0];
}
}