feat: new DatabaseInstance class for universal database management, W.I.P

This commit is contained in:
Jo 2024-01-09 08:28:18 +01:00
parent 123fce455b
commit b7de4d9614
5 changed files with 61 additions and 15 deletions

View file

@ -1,4 +1,4 @@
FROM node:21
FROM oven/bun:latest
# Install netcat
RUN apt-get update && apt-get install -y netcat-traditional
@ -6,9 +6,10 @@ RUN apt-get update && apt-get install -y netcat-traditional
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
COPY package.json ./
copy bun.lockb ./
RUN npm install
RUN bun install
COPY . .

View file

@ -0,0 +1,49 @@
const mysql = require("mysql2");
const { readdir } = require("fs");
const path = require("path");
/**
* Universal database instance to be used for any database operations
*/
class DatabaseInstance {
/**
* Construct the DatabaseInstance using the database credentials
*
* @param {Object} credentials The database credentils in a standard mysql format
*/
constructor(credentials) {
this.pool = mysql.createPool({
database: credentials.database,
user: credentials.user,
password: credentials.password,
host: credentials.host,
port: credentials.port,
enableKeepAlive: true,
waitForConnections: true,
connectionLimit: 10
});
console.log("[JET » DatabaseInstance] Successfully initialized database connection")
this.pool.on("connection", (err) => {
if (err) console.log("[JET » DatabaseInstance] Error occurred during connection");
else console.log("[JET » DatabaseInstance » client] Successfully created new connection");
});
}
/**
*
* @param {String} query
* @param {Array} values
* @returns
*/
async execute(query, values, callback) {
this.pool.execute(query, values, callback);
}
async query(query, callback) {
this.pool.query(query, callback);
}
}
module.exports = { DatabaseInstance };

18
app.js
View file

@ -12,12 +12,17 @@
// Imports
//------------------------------------------------>
// Packages
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const mysql = require("mysql2");
const path = require("path");
// Classes
const { DatabaseInstance } = require("./Structures/Classes/DatabaseInstance");
// Load environment variables when not using docker
if (!process.env.DOCKER) {
const dotenv = require("dotenv");
dotenv.config();
@ -43,11 +48,10 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//------------------------------------------------>
// MySQL connection
// Initialize database
//------------------------------------------------>
//! Make extra sure these values are loaded correctly, if you experience any connection issues.
const db = mysql.createConnection({
const db = new DatabaseInstance({
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
@ -55,14 +59,6 @@ const db = mysql.createConnection({
port: process.env.DB_PORT
});
db.connect((error) => {
if (error) {
console.error("[JET] Error connecting to MySQL database:", error);
} else {
console.log("[JET] Database connected successfully");
}
});
//------------------------------------------------>
// Routing
//------------------------------------------------>

BIN
bun.lockb Executable file

Binary file not shown.

View file

@ -14,4 +14,4 @@ check_database() {
check_database
# Start the Express app
exec npm run start
exec bun run start