-
Notifications
You must be signed in to change notification settings - Fork 279
/
mysql-driver.ts
197 lines (170 loc) · 5.29 KB
/
mysql-driver.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
Pool,
PoolOptions,
PoolConnection,
RowDataPacket,
OkPacket,
ResultSetHeader,
} from 'mysql2'
import {
DatabaseConnection,
QueryResult,
} from '../../driver/database-connection.js'
import { Driver, TransactionSettings } from '../../driver/driver.js'
import { CompiledQuery } from '../../query-compiler/compiled-query.js'
import { isFunction, isObject } from '../../util/object-utils.js'
import { MysqlDialectConfig } from './mysql-dialect.js'
const PRIVATE_RELEASE_METHOD = Symbol()
export class MysqlDriver implements Driver {
readonly #config: MysqlDialectConfig
readonly #connections = new WeakMap<PoolConnection, DatabaseConnection>()
#pool?: Pool
constructor(config: MysqlDialectConfig) {
this.#config = config
}
async init(): Promise<void> {
// Import the `mysql2` module here instead at the top of the file
// so that this file can be loaded by node without `mysql2` driver
// installed. As you can see, there IS an import from `mysql2` at the
// top level too, but that's only for types. It doesn't get compiled
// into javascript. You can check the built javascript code.
const poolFactory = await importMysqlPoolFactory()
// Use the `mysql2` module's own pool. All drivers should use the
// pool provided by the database library if possible.
this.#pool = poolFactory(this.#config)
}
async acquireConnection(): Promise<DatabaseConnection> {
const rawConnection = await this.#acquireConnection()
let connection = this.#connections.get(rawConnection)
if (!connection) {
connection = new MysqlConnection(rawConnection)
this.#connections.set(rawConnection, connection)
// The driver must take care of calling `onCreateConnection` when a new
// connection is created. The `mysql2` module doesn't provide an async hook
// for the connection creation. We need to call the method explicitly.
if (this.#config.onCreateConnection) {
await this.#config.onCreateConnection(connection)
}
}
return connection
}
async #acquireConnection(): Promise<PoolConnection> {
return new Promise((resolve, reject) => {
this.#pool!.getConnection(async (err, rawConnection) => {
if (err) {
reject(err)
} else {
resolve(rawConnection)
}
})
})
}
async beginTransaction(
connection: DatabaseConnection,
settings: TransactionSettings
): Promise<void> {
if (settings.isolationLevel) {
// On mysql this sets the isolation level of the next transaction.
await connection.executeQuery({
sql: `set transaction isolation level ${settings.isolationLevel}`,
parameters: [],
})
}
await connection.executeQuery({ sql: 'begin', parameters: [] })
}
async commitTransaction(connection: DatabaseConnection): Promise<void> {
await connection.executeQuery({ sql: 'commit', parameters: [] })
}
async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
await connection.executeQuery({ sql: 'rollback', parameters: [] })
}
async releaseConnection(connection: DatabaseConnection): Promise<void> {
const mysqlConnection = connection as MysqlConnection
mysqlConnection[PRIVATE_RELEASE_METHOD]()
}
async destroy(): Promise<void> {
return new Promise((resolve, reject) => {
this.#pool!.end((err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
}
async function importMysqlPoolFactory(): Promise<
(config: PoolOptions) => Pool
> {
try {
// For this to work with both esm and cjs modules we need
// this hacky crap here.
const mysql = (await import('mysql2')) as any
if (isFunction(mysql.createPool)) {
return mysql.createPool
} else {
// With esm the imported module doesn't match the typings.
return mysql.default.createPool
}
} catch (error) {
throw new Error(
'Mysql client not installed. Please run `npm install mysql2`'
)
}
}
class MysqlConnection implements DatabaseConnection {
readonly #rawConnection: PoolConnection
constructor(rawConnection: PoolConnection) {
this.#rawConnection = rawConnection
}
async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
const result = await this.#executeQuery(compiledQuery)
if (isObject(result) && 'insertId' in result && 'affectedRows' in result) {
if (result.insertId) {
return {
insertedPrimaryKey: result.insertId,
rows: [],
}
} else {
return {
numUpdatedOrDeletedRows: result.affectedRows,
rows: [],
}
}
} else if (Array.isArray(result)) {
return {
rows: result as O[],
}
}
return {
rows: [],
}
}
#executeQuery(
compiledQuery: CompiledQuery
): Promise<
| RowDataPacket[][]
| RowDataPacket[]
| OkPacket
| OkPacket[]
| ResultSetHeader
> {
return new Promise((resolve, reject) => {
this.#rawConnection.query(
compiledQuery.sql,
compiledQuery.parameters,
(err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
}
)
})
}
[PRIVATE_RELEASE_METHOD](): void {
this.#rawConnection.release()
}
}