impress-2020/src/server/db.js

33 lines
697 B
JavaScript
Raw Normal View History

const mysql = require("mysql2");
2020-04-22 11:51:36 -07:00
let globalDbs = new Map();
2020-04-22 15:53:59 -07:00
2020-09-02 03:49:58 -07:00
async function connectToDb({
host = "impress.openneo.net",
2020-09-02 03:49:58 -07:00
user = process.env["IMPRESS_MYSQL_USER"],
password = process.env["IMPRESS_MYSQL_PASSWORD"],
database = "openneo_impress",
2020-09-02 03:49:58 -07:00
} = {}) {
if (globalDbs.has(host)) {
return globalDbs.get(host);
2020-04-22 15:53:59 -07:00
}
const db = mysql
.createConnection({
host,
2020-09-02 03:49:58 -07:00
user,
password,
database,
multipleStatements: true,
})
// We upgrade to promises here, instead of using the mysql2/promise import,
// for compatibility with Honeycomb's automatic tracing.
.promise();
2020-04-22 11:51:36 -07:00
globalDbs.set(host, db);
return db;
2020-04-22 11:51:36 -07:00
}
module.exports = connectToDb;