Binato/server/objects/Database.ts

97 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2022-11-16 15:25:46 +00:00
import { ConsoleHelper } from "../../ConsoleHelper";
2023-10-04 15:10:38 +01:00
import { createPool, Pool, RowDataPacket } from "mysql2";
import { DBInDataType } from "../types/DBTypes";
2022-11-16 15:25:46 +00:00
2023-09-10 12:59:22 +01:00
export default class Database {
2022-11-16 15:25:46 +00:00
private connectionPool:Pool;
private static readonly CONNECTION_LIMIT = 128;
2022-11-17 00:29:07 +00:00
public connected:boolean = false;
2023-08-20 13:03:01 +01:00
public constructor(databaseAddress:string, databasePort:number = 3306, databaseUsername:string, databasePassword:string, databaseName:string) {
2022-11-16 15:25:46 +00:00
this.connectionPool = createPool({
connectionLimit: Database.CONNECTION_LIMIT,
host: databaseAddress,
port: databasePort,
user: databaseUsername,
password: databasePassword,
database: databaseName
});
2023-08-20 13:03:01 +01:00
ConsoleHelper.printInfo(`Connected DB connection pool. MAX_CONNECTIONS = ${Database.CONNECTION_LIMIT}`);
2022-11-16 15:25:46 +00:00
}
2023-10-04 15:10:38 +01:00
public execute(query:string, data?:Array<DBInDataType>) {
return new Promise<boolean>((resolve, reject) => {
this.connectionPool.getConnection((err, connection) => {
if (err) {
return reject(err);
}
if (data == null) {
2023-10-04 15:12:35 +01:00
connection.execute(query, (err, result) => {
2023-10-04 15:10:38 +01:00
if (err) {
connection.release();
return reject(err);
}
resolve(result !== undefined);
});
} else {
2023-10-04 15:12:35 +01:00
connection.execute(query, data, (err, result) => {
if (err) {
connection.release();
return reject(err);
}
resolve(result !== undefined);
});
2023-10-04 15:10:38 +01:00
}
});
});
}
public query(query:string, data?:Array<DBInDataType>) {
return new Promise<RowDataPacket[]>((resolve, reject) => {
2022-11-16 15:25:46 +00:00
this.connectionPool.getConnection((err, connection) => {
if (err) {
2023-10-04 15:10:38 +01:00
return reject(err);
2022-11-16 15:25:46 +00:00
} else {
// Use old query
if (data == null) {
2023-10-04 15:10:38 +01:00
connection.query<RowDataPacket[]>(query, (err, rows) => {
connection.release();
2022-11-16 15:25:46 +00:00
if (err) {
2023-10-04 15:10:38 +01:00
return reject(err);
2022-11-16 15:25:46 +00:00
}
2023-10-04 15:10:38 +01:00
resolve(rows);
connection.release();
2022-11-16 15:25:46 +00:00
});
}
// Use new prepared statements w/ placeholders
else {
2023-10-04 15:10:38 +01:00
connection.execute<RowDataPacket[]>(query, data, (err, rows) => {
connection.release();
2022-11-16 15:25:46 +00:00
if (err) {
2023-10-04 15:10:38 +01:00
return reject(err);
2022-11-16 15:25:46 +00:00
}
2023-10-04 15:10:38 +01:00
resolve(rows);
connection.release();
2022-11-16 15:25:46 +00:00
});
}
}
});
});
}
2023-10-04 15:10:38 +01:00
public async querySingle(query:string, data?:Array<DBInDataType>) {
const dbData = await this.query(query, data);
if (dbData != null && dbData.length > 0) {
return dbData[0];
}
return null;
}
2022-11-16 15:25:46 +00:00
}