MCPcopy Create free account
hub / github.com/sqlchat/sqlchat / getMySQLConnection

Function getMySQLConnection

src/lib/connectors/mysql/index.ts:8–45  ·  view source on GitHub ↗
(connection: Connection)

Source from the content-addressed store, hash-verified

6const systemDatabases = ["information_schema", "mysql", "performance_schema", "sys"];
7
8const getMySQLConnection = async (connection: Connection): Promise<mysql.Connection> => {
9 const connectionOptions: ConnectionOptions = {
10 host: connection.host,
11 port: parseInt(connection.port),
12 user: connection.username,
13 password: connection.password,
14 database: connection.database,
15 };
16 if (connection.ssl) {
17 connectionOptions.ssl = {
18 ca: connection.ssl?.ca,
19 cert: connection.ssl?.cert,
20 key: connection.ssl?.key,
21 };
22 } else {
23 // rejectUnauthorized=false to infer sslmode=prefer since hosted MySQL venders have SSL enabled.
24 connectionOptions.ssl = {
25 rejectUnauthorized: false,
26 };
27 }
28
29 let conn;
30 if (connection.ssl) {
31 conn = await mysql.createConnection(connectionOptions);
32 } else {
33 try {
34 conn = await mysql.createConnection(connectionOptions);
35 } catch (error) {
36 if (error instanceof Error && error.message.includes("Server does not support secure")) {
37 connectionOptions.ssl = undefined;
38 conn = await mysql.createConnection(connectionOptions);
39 } else {
40 throw error;
41 }
42 }
43 }
44 return conn;
45};
46
47const testConnection = async (connection: Connection): Promise<boolean> => {
48 const conn = await getMySQLConnection(connection);

Callers 4

testConnectionFunction · 0.85
executeFunction · 0.85
getDatabasesFunction · 0.85
getTableSchemaFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected