2022-11-16 15:25:46 +00:00
|
|
|
import { ConsoleHelper } from "../../ConsoleHelper";
|
|
|
|
import { createPool, Pool } from "mysql2";
|
|
|
|
|
|
|
|
export class Database {
|
|
|
|
private connectionPool:Pool;
|
|
|
|
private static readonly CONNECTION_LIMIT = 128;
|
|
|
|
|
2022-11-17 00:29:07 +00:00
|
|
|
public connected:boolean = false;
|
|
|
|
|
2022-11-16 15:25:46 +00:00
|
|
|
public constructor(databaseAddress:string, databasePort:number = 3306, databaseUsername:string, databasePassword:string, databaseName:string, connectedCallback:Function) {
|
|
|
|
this.connectionPool = createPool({
|
|
|
|
connectionLimit: Database.CONNECTION_LIMIT,
|
|
|
|
host: databaseAddress,
|
|
|
|
port: databasePort,
|
|
|
|
user: databaseUsername,
|
|
|
|
password: databasePassword,
|
|
|
|
database: databaseName
|
|
|
|
});
|
|
|
|
|
|
|
|
const classCreationTime:number = Date.now();
|
2022-11-17 00:29:07 +00:00
|
|
|
let lastQueryFinished = true;
|
2022-11-16 15:25:46 +00:00
|
|
|
const connectionCheckInterval = setInterval(() => {
|
2022-11-17 00:29:07 +00:00
|
|
|
if (lastQueryFinished) {
|
|
|
|
lastQueryFinished = false;
|
|
|
|
this.query("SELECT name FROM osu_info LIMIT 1")
|
2022-11-16 15:25:46 +00:00
|
|
|
.then(data => {
|
2022-11-17 00:29:07 +00:00
|
|
|
if (!this.connected) {
|
|
|
|
this.connected = true;
|
2022-11-19 01:06:03 +00:00
|
|
|
ConsoleHelper.printInfo(`Connected to database. Took ${Date.now() - classCreationTime}ms`);
|
2022-11-17 00:29:07 +00:00
|
|
|
clearInterval(connectionCheckInterval);
|
|
|
|
lastQueryFinished = true;
|
2022-11-16 15:25:46 +00:00
|
|
|
|
2022-11-17 00:29:07 +00:00
|
|
|
connectedCallback();
|
|
|
|
}
|
2022-11-16 15:25:46 +00:00
|
|
|
})
|
2022-11-17 00:29:07 +00:00
|
|
|
.catch(err => {
|
|
|
|
lastQueryFinished = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 16);
|
2022-11-16 15:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public query(query = "", data?:Array<any>) {
|
|
|
|
const limited = query.includes("LIMIT 1");
|
|
|
|
|
2022-11-17 00:29:07 +00:00
|
|
|
return new Promise<any>((resolve, reject) => {
|
2022-11-16 15:25:46 +00:00
|
|
|
this.connectionPool.getConnection((err, connection) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
try {
|
|
|
|
connection.release();
|
|
|
|
} catch (e) {
|
|
|
|
ConsoleHelper.printError("Failed to release mysql connection\n" + err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use old query
|
|
|
|
if (data == null) {
|
|
|
|
connection.query(query, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
connection.release();
|
|
|
|
} else {
|
|
|
|
dataReceived(resolve, data, limited);
|
|
|
|
connection.release();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Use new prepared statements w/ placeholders
|
|
|
|
else {
|
|
|
|
connection.execute(query, data, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
connection.release();
|
|
|
|
} else {
|
|
|
|
dataReceived(resolve, data, limited);
|
|
|
|
connection.release();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataReceived(resolveCallback:(value:unknown) => void, data:any, limited:boolean = false) : void {
|
|
|
|
if (limited) resolveCallback(data[0]);
|
|
|
|
else resolveCallback(data);
|
|
|
|
}
|