-
Notifications
You must be signed in to change notification settings - Fork 47
/
ConnectionPool.ts
164 lines (159 loc) · 4.26 KB
/
ConnectionPool.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
import {PassThrough, Readable} from 'stream';
import {BaseConnectionPool, Factory, PoolOptions} from '@databases/shared';
import sql, {SQLQuery} from '@databases/sql';
import {createConnection} from 'mysql2/promise';
import Connection from './Connection';
import Transaction from './Transaction';
import {ConnectionPool as IConnectionPool} from './types/Queryable';
import EventHandlers from './types/EventHandlers';
import MySqlDriver from './MySqlDriver';
const factories: Factory<MySqlDriver, Connection, Transaction> = {
createTransaction(driver, transactionParentContext) {
return new Transaction(driver, factories, transactionParentContext);
},
createConnection(driver) {
return new Connection(driver, factories);
},
};
interface MySqlConnectionOptions {
uri: string;
multipleStatements?: boolean;
timezone: 'Z' | 'local';
typeCast?: (
field: {
type: string;
length: number;
db: string;
table: string;
name: string;
string(): string;
buffer(): Buffer;
geometry(): unknown;
},
next: () => any,
) => any;
}
const getConnectionPoolOptions = (
srcConfig: MySqlConnectionOptions,
poolOptions: Omit<
PoolOptions<MySqlDriver>,
'openConnection' | 'closeConnection'
>,
handlers: EventHandlers,
onError: (err: Error) => void,
acquireLockTimeoutMilliseconds: number,
serverTimeZone: 'local' | 'utc' | undefined,
): PoolOptions<MySqlDriver> => {
return {
...poolOptions,
openConnection: async (removeFromPool) => {
const client = await createConnection(srcConfig);
const driver = new MySqlDriver(
client,
handlers,
acquireLockTimeoutMilliseconds,
);
try {
if (serverTimeZone === 'utc') {
await driver.client.query(`SET time_zone = "+00:00";`);
} else if (serverTimeZone === 'local') {
await driver.client.query(`SET time_zone = ?;`, [
Intl.DateTimeFormat().resolvedOptions().timeZone,
]);
}
} catch (ex) {
void driver.dispose();
throw ex;
}
driver.onAddingToPool(removeFromPool, onError);
if (handlers.onConnectionOpened) {
handlers.onConnectionOpened();
}
return driver;
},
closeConnection: async (driver) => {
try {
await driver.dispose();
if (handlers.onConnectionClosed) {
handlers.onConnectionClosed();
}
} catch (ex: any) {
console.warn(ex.message);
}
},
onActive(driver) {
driver.onActive();
},
onIdle(driver) {
driver.onIdle();
},
};
};
export default class ConnectionPool
extends BaseConnectionPool<Connection, Transaction, MySqlDriver>
implements IConnectionPool
{
public readonly sql = sql;
constructor(
srcConfig: MySqlConnectionOptions,
poolOptions: Omit<
PoolOptions<MySqlDriver>,
'openConnection' | 'closeConnection'
>,
handlers: EventHandlers,
onError: (err: Error) => void,
acquireLockTimeoutMilliseconds: number,
serverTimeZone: 'local' | 'utc' | undefined,
) {
super(
getConnectionPoolOptions(
srcConfig,
poolOptions,
handlers,
onError,
acquireLockTimeoutMilliseconds,
serverTimeZone,
),
factories,
);
}
queryNodeStream(
query: SQLQuery,
options: {highWaterMark?: number} = {},
): Readable {
this._throwIfDisposed();
const stream = new PassThrough({
objectMode: true,
});
this._pool
.getConnection()
.then(async (driver) => {
let released = false;
const connectionStream = driver.connection.queryNodeStream(
query,
options,
);
return connectionStream
.on('fields', (fields) => {
stream.emit('fields', fields);
})
.on('error', (err) => {
if (!released) {
released = true;
driver.dispose();
}
stream.emit('error', err);
})
.on('end', () => {
if (!released) {
released = true;
driver.release();
}
stream.emit('end');
})
.pipe(stream);
})
.catch((ex) => stream.emit('error', ex));
return stream;
}
}