feat: add Model and Controller class, improve structure

This commit is contained in:
Jo 2024-01-10 17:23:14 +01:00
parent b7de4d9614
commit a86bddf352
10 changed files with 107 additions and 70 deletions

View file

@ -0,0 +1,25 @@
//------------------------------------------------>
// Imports
//------------------------------------------------>
// None
//------------------------------------------------>
// Controller class
//------------------------------------------------>
class Controller {
/**
* Construct the basis of our Controller
*/
constructor() {
this.global = {
pageData: {
name: "The Void",
links: []
}
}
}
}
module.exports = { Controller }

View file

@ -1,49 +0,0 @@
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 };

View file

@ -0,0 +1,38 @@
//------------------------------------------------>
// Imports
//------------------------------------------------>
// Packages
const mysql = require("mysql2");
// Config
const { databaseCredentials } = require("../Config/databaseCredentials");
//------------------------------------------------>
// Model class
//------------------------------------------------>
class Model {
/**
* Construct the Model using the database credentials
*
* @param {Object} credentials The database credentials in a standard mysql format
*/
constructor() {
this.pool = mysql.createPool(databaseCredentials);
this.pool.getConnection(async (err) => {
if (err) console.log(`[JET » Model] Error occurred during connection: ${err}`);
else console.log("[JET » Model » pool] Successfully initialized pool");
});
this.pool.on("connection", async () => {
console.log("[JET » Model » pool] Created new connection");
});
if (!this.initialize) throw new Error("[JET » Model] initialize method not found");
this.initialize();
}
}
module.exports = { Model }

26
app.js
View file

@ -15,12 +15,11 @@
// 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");
// Config
const { databaseCredentials } = require("./Structures/Config/databaseCredentials");
// Load environment variables when not using docker
if (!process.env.DOCKER) {
@ -45,32 +44,29 @@ app.set("view engine", "ejs");
// Use body-parser and cookie-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//------------------------------------------------>
// Initialize database
//------------------------------------------------>
const db = new DatabaseInstance({
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT
const db = mysql.createConnection(databaseCredentials);
db.connect((err) => {
if (err) console.log(`[JET] Error occurred during initial connection: ${err}`);
else console.log("[JET] Successfully connected to database");
});
db.end();
//------------------------------------------------>
// Routing
//------------------------------------------------>
const apiRouter = require("./routes/apiRoutes");
const indexRouter = require("./routes/indexRoutes");
app.use("/api", apiRouter);
// Handle home route
app.get("/", (req, res) => {
res.render("layout");
});
app.use("/", indexRouter);
//------------------------------------------------>
// Start Express

View file

@ -8,12 +8,12 @@ services:
environment:
MYSQL_DATABASE: jet
MYSQL_USER: jet
MYSQL_PASSWORD: ghiuwe§825429h§$%ff
MYSQL_PASSWORD: ghiuwe825429hff
MYSQL_ROOT_PASSWORD: hgruwhgvuiwrghiuwe§825429h§$%ff
ports:
- "3306:3306"
volumes:
- mysql:/var/lib/mysql
# volumes:
# - mysql:/var/lib/mysql
# Express app service
app:
@ -28,10 +28,10 @@ services:
DB_HOST: db
DB_PORT: 3306
DB_USER: jet
DB_PASSWORD: ghiuwe§825429h§$%ff
DB_PASSWORD: ghiuwe§825429hff
DB_NAME: jet
DOCKER: true
PORT: 4300
volumes:
mysql:
# volumes:
# mysql:

19
model/userModel.js Normal file
View file

@ -0,0 +1,19 @@
//------------------------------------------------>
// Imports
//------------------------------------------------>
const { Model } = require("../Structures/Classes/Model");
//------------------------------------------------>
// UserModel
//------------------------------------------------>
class UserModel extends Model {
initialize() {
// The initialize method is required in each instance of the Model class!!!
}
async getUserById() {
// Additional classes are optional and can be used to interact with the DB Pool, this.pool!
}
}

View file

8
routes/indexRoutes.js Normal file
View file

@ -0,0 +1,8 @@
const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
res.render("layout");
});
module.exports = router;