This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
/
DB.ts
55 lines (43 loc) · 1.48 KB
/
DB.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
import * as mysql from 'mysql2/promise';
const pool: Array<DB> = [];
class DB {
private readonly connection: mysql.IPromiseConnection;
// can only instantiate via DB.connect method
private constructor(connection: mysql.IPromiseConnection) {
this.connection = connection;
}
public static async connect(options: mysql.IConnectionConfig): Promise<DB> {
const instance = new DB(await mysql.createConnection(options));
pool.push(instance);
return instance;
}
public async query<T>(sql: string): Promise<Array<T>> {
const res = await this.connection.query<T>(sql);
return res[0];
}
public async multiQuery<T>(sql: string): Promise<Array<Array<T>>> {
let isMulti = true;
if (sql.split(';').length === 2) {
isMulti = false;
}
let res = (await this.connection.query<Array<T>>(sql))[0];
if (!isMulti) {
// mysql will return a non-array payload if there's only one statement in the query
// so standardise the res..
// eslint-disable-next-line @typescript-eslint/no-explicit-any
res = [res] as any;
}
return res;
}
public async end(): Promise<void> {
await this.connection.end().catch(() => {});
}
public static async cleanup(): Promise<void> {
await Promise.all(
pool.map(async p => {
await p.end();
}),
);
}
}
export { DB };