こんな感じ
Hoge.js
const mysql = require('mysql');
let connection = mysql.createConnection({
host: 'mysql',
// 接続情報は環境変数に入れている想定
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
});
connection.connect();
module.exports = class Hoge {
static get connection() {
return connection;
}
}
こういった class を基底クラスとして、 ORM のようなものを実装できる。