| 12 | } |
| 13 | |
| 14 | export async function createMySQLConnection(config: MySQLConnectionConfig) { |
| 15 | const hostValidation = await validateDatabaseHost(config.host, 'host') |
| 16 | if (!hostValidation.isValid) { |
| 17 | throw new Error(hostValidation.error) |
| 18 | } |
| 19 | |
| 20 | const resolvedIP = hostValidation.resolvedIP ?? config.host |
| 21 | |
| 22 | const connectionConfig: mysql.ConnectionOptions = { |
| 23 | host: config.host, |
| 24 | port: config.port, |
| 25 | database: config.database, |
| 26 | user: config.username, |
| 27 | password: config.password, |
| 28 | stream: () => { |
| 29 | const socket = net.connect({ host: resolvedIP, port: config.port, timeout: 10000 }) |
| 30 | socket.setNoDelay(true) |
| 31 | return socket |
| 32 | }, |
| 33 | } |
| 34 | |
| 35 | if (config.ssl === 'disabled') { |
| 36 | } else if (config.ssl === 'required') { |
| 37 | connectionConfig.ssl = { rejectUnauthorized: true } |
| 38 | } else if (config.ssl === 'preferred') { |
| 39 | connectionConfig.ssl = { rejectUnauthorized: false } |
| 40 | } |
| 41 | |
| 42 | return mysql.createConnection(connectionConfig) |
| 43 | } |
| 44 | |
| 45 | export async function executeQuery( |
| 46 | connection: mysql.Connection, |