diff --git a/Dockerfile b/Dockerfile index 8496da5..8bd562b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 . . diff --git a/Structures/Classes/DatabaseInstance.js b/Structures/Classes/DatabaseInstance.js new file mode 100644 index 0000000..313e2e2 --- /dev/null +++ b/Structures/Classes/DatabaseInstance.js @@ -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 }; \ No newline at end of file diff --git a/app.js b/app.js index f76d2f7..792469f 100644 --- a/app.js +++ b/app.js @@ -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 //------------------------------------------------> diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000..5575077 Binary files /dev/null and b/bun.lockb differ diff --git a/entrypoint.sh b/entrypoint.sh index 2f46014..2b76ede 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -14,4 +14,4 @@ check_database() { check_database # Start the Express app -exec npm run start +exec bun run start