32 lines
942 B
JavaScript
32 lines
942 B
JavaScript
import mysql from 'mysql2/promise'
|
|
import { loadP42Secrets } from './secretsLoader.js'
|
|
|
|
export function resolveMysqlCredentials(config = {}) {
|
|
loadP42Secrets()
|
|
const user = process.env.mysql_user
|
|
const password = process.env.mysql_pass
|
|
if(!user || !password) {
|
|
throw new Error('Missing MySQL credentials: set mysql_user and mysql_pass in environment')
|
|
}
|
|
return({
|
|
socketPath: config.socketPath,
|
|
host: config.host,
|
|
port: config.port,
|
|
user,
|
|
password,
|
|
database: config.database ?? 'p42GUI',
|
|
waitForConnections: true,
|
|
connectionLimit: config.connectionLimit ?? 5,
|
|
queueLimit: 0,
|
|
})
|
|
}
|
|
|
|
export async function createMysqlPool(config) {
|
|
return(await mysql.createPool(resolveMysqlCredentials(config)))
|
|
}
|
|
|
|
export async function mysqlExecute(pool, query, values = []) {
|
|
const [rows] = await pool.execute(query, values)
|
|
return(rows)
|
|
}
|