impress/src/server/db.js
Matchu 5a91dd2f2a in-memory cache for modeling query
I'm using my first ever MySQL Store Procedure for clever cleverness in caching the modeling query!

I realized that checking for the latest contribution timestamp is a pretty reliable way of deciding when modeling data was last updated at all. If that timestamp hasn't changed, we can reuse the results!

I figured that, because query roundtrips are a bottleneck in this environment, I didn't want to make that query separately. So, I built a MySQL procedure to do the check on the database side!
2020-09-06 15:49:08 -07:00

28 lines
618 B
JavaScript

const mysql = require("mysql2");
let globalDb;
async function connectToDb({
user = process.env["IMPRESS_MYSQL_USER"],
password = process.env["IMPRESS_MYSQL_PASSWORD"],
} = {}) {
if (globalDb) {
return globalDb;
}
globalDb = mysql
.createConnection({
host: "impress.openneo.net",
user,
password,
database: "openneo_impress",
multipleStatements: true,
})
// We upgrade to promises here, instead of using the mysql2/promise import,
// for compatibility with Honeycomb's automatic tracing.
.promise();
return globalDb;
}
module.exports = connectToDb;