Binato/server/DatabaseHelper.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-02-13 02:42:59 +00:00
const mysql = require("mysql");
2020-09-07 19:23:06 +01:00
module.exports = class {
2021-02-13 02:42:59 +00:00
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName) {
this.connectionPool = mysql.createPool({
connectionLimit: 128,
host: databaseAddress,
port: databasePort,
user: databaseUsername,
password: databasePassword,
database: databaseName
});
2020-09-07 19:23:06 +01:00
}
2021-02-13 02:42:59 +00:00
async query(sqlQuery) {
return new Promise((resolve, reject) => {
this.connectionPool.getConnection((err, connection) => {
if (err) {
reject(err);
connection.release();
} else {
connection.query(sqlQuery, (err, data) => {
if (err) {
reject(err);
connection.release();
} else {
if (sqlQuery.includes("LIMIT 1")) resolve(data[0]);
else resolve(data);
connection.release();
}
});
}
});
});
2020-09-07 19:23:06 +01:00
}
}