-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.ts
114 lines (94 loc) · 3.33 KB
/
connection.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
import mysql, { OkPacket, RowDataPacket } from "mysql2";
import * as pg from "pg";
import * as T from "./type";
export class SQL {
//connection: mysql.Connection;
private pool: T.Pool | null;
private poolPg: pg.Pool | null;
constructor(
connectionOptions: T.ConnectionOptions,
databaseType: T.DatabaseType
) {
// remove ssl by default
/*if (!connectionOptions.ssl) {
connectionOptions.ssl = { rejectUnauthorized: false };
}*/
// see: https://dev.mysql.com/doc/mysql-port-reference/en/mysql-ports-reference-tables.html#mysql-client-server-ports
if (!connectionOptions.port) {
// default port are different depending on the database type
if (databaseType === "PostgreSQL") {
connectionOptions.port = 5432;
}
if (databaseType === "MySQL") {
connectionOptions.port = 3306;
}
}
if (databaseType === "PostgreSQL") {
// here reference the postgresl stuff
// return "pool" equivalent object
}
// fallback to SQL
if (typeof connectionOptions.multipleStatements === "undefined") {
connectionOptions.multipleStatements = true;
}
if (typeof connectionOptions.timezone === "undefined") {
// https://stackoverflow.com/a/60883634/1659569
// "GMT" throws: Ignoring invalid timezone passed to Connection: GMT. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
connectionOptions.timezone = "+00:00";
}
this.pool = null;
this.poolPg = null;
if (databaseType === "PostgreSQL") {
// https://node-postgres.com/apis/pool
this.poolPg = new pg.Pool({
host: connectionOptions.host,
user: connectionOptions.user || (connectionOptions as any).username,
database: connectionOptions.database,
password: connectionOptions.password,
port: connectionOptions.port,
idleTimeoutMillis: 5000,
});
}
if (databaseType === "MySQL") {
// https://www.npmjs.com/package/mysql2#using-connection-pools
//this.connection = mysql.createConnection(config);
this.pool = mysql.createPool(connectionOptions).promise();
}
}
execQuery = async (query: string): Promise<RowDataPacket | OkPacket> => {
if (this.pool) {
const [response] = await this.pool.query(query);
return response as RowDataPacket | OkPacket;
}
if (this.poolPg) {
try {
const r = await this.poolPg.query(query);
// console.log(r);
// console.log(r.command, r.fields, r.rows, r.rowCount,r.oid);
// mutate
if (["INSERT", "UPDATE", "DELETE"].includes(r.command)) {
const okPacket: OkPacket = {
constructor: {
name: "OkPacket",
},
insertId: r.oid,
affectedRows: r.rowCount || 0,
fieldCount: 0,
changedRows: 0,
serverStatus: 0,
warningCount: 0,
message: "",
procotol41: false,
};
return okPacket;
}
return r.rows as RowDataPacket;
} catch (err) {
return Promise.reject(err);
}
}
throw Error("no pool initialized");
};
}
// stores all connections in a map, can be called on demand
export const databases: Map<string, SQL> = new Map();