feat: add Model and Controller class, improve structure
This commit is contained in:
parent
b7de4d9614
commit
a86bddf352
10 changed files with 107 additions and 70 deletions
25
Structures/Classes/Controller.js
Normal file
25
Structures/Classes/Controller.js
Normal 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 }
|
|
@ -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 };
|
|
38
Structures/Classes/Model.js
Normal file
38
Structures/Classes/Model.js
Normal 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
26
app.js
|
@ -15,12 +15,11 @@
|
||||||
// Packages
|
// Packages
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const bodyParser = require("body-parser");
|
const bodyParser = require("body-parser");
|
||||||
const cookieParser = require("cookie-parser");
|
|
||||||
const mysql = require("mysql2");
|
const mysql = require("mysql2");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
// Classes
|
// Config
|
||||||
const { DatabaseInstance } = require("./Structures/Classes/DatabaseInstance");
|
const { databaseCredentials } = require("./Structures/Config/databaseCredentials");
|
||||||
|
|
||||||
// Load environment variables when not using docker
|
// Load environment variables when not using docker
|
||||||
if (!process.env.DOCKER) {
|
if (!process.env.DOCKER) {
|
||||||
|
@ -45,32 +44,29 @@ app.set("view engine", "ejs");
|
||||||
|
|
||||||
// Use body-parser and cookie-parser
|
// Use body-parser and cookie-parser
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
app.use(cookieParser());
|
|
||||||
|
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
// Initialize database
|
// Initialize database
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
|
|
||||||
const db = new DatabaseInstance({
|
const db = mysql.createConnection(databaseCredentials);
|
||||||
database: process.env.DB_NAME,
|
|
||||||
user: process.env.DB_USER,
|
db.connect((err) => {
|
||||||
password: process.env.DB_PASSWORD,
|
if (err) console.log(`[JET] Error occurred during initial connection: ${err}`);
|
||||||
host: process.env.DB_HOST,
|
else console.log("[JET] Successfully connected to database");
|
||||||
port: process.env.DB_PORT
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
db.end();
|
||||||
|
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
// Routing
|
// Routing
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
|
|
||||||
const apiRouter = require("./routes/apiRoutes");
|
const apiRouter = require("./routes/apiRoutes");
|
||||||
|
const indexRouter = require("./routes/indexRoutes");
|
||||||
|
|
||||||
app.use("/api", apiRouter);
|
app.use("/api", apiRouter);
|
||||||
|
app.use("/", indexRouter);
|
||||||
// Handle home route
|
|
||||||
app.get("/", (req, res) => {
|
|
||||||
res.render("layout");
|
|
||||||
});
|
|
||||||
|
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
// Start Express
|
// Start Express
|
||||||
|
|
|
@ -8,12 +8,12 @@ services:
|
||||||
environment:
|
environment:
|
||||||
MYSQL_DATABASE: jet
|
MYSQL_DATABASE: jet
|
||||||
MYSQL_USER: jet
|
MYSQL_USER: jet
|
||||||
MYSQL_PASSWORD: ghiuwe§825429h§$%ff
|
MYSQL_PASSWORD: ghiuwe825429hff
|
||||||
MYSQL_ROOT_PASSWORD: hgruwhgvuiwrghiuwe§825429h§$%ff
|
MYSQL_ROOT_PASSWORD: hgruwhgvuiwrghiuwe§825429h§$%ff
|
||||||
ports:
|
ports:
|
||||||
- "3306:3306"
|
- "3306:3306"
|
||||||
volumes:
|
# volumes:
|
||||||
- mysql:/var/lib/mysql
|
# - mysql:/var/lib/mysql
|
||||||
|
|
||||||
# Express app service
|
# Express app service
|
||||||
app:
|
app:
|
||||||
|
@ -28,10 +28,10 @@ services:
|
||||||
DB_HOST: db
|
DB_HOST: db
|
||||||
DB_PORT: 3306
|
DB_PORT: 3306
|
||||||
DB_USER: jet
|
DB_USER: jet
|
||||||
DB_PASSWORD: ghiuwe§825429h§$%ff
|
DB_PASSWORD: ghiuwe§825429hff
|
||||||
DB_NAME: jet
|
DB_NAME: jet
|
||||||
DOCKER: true
|
DOCKER: true
|
||||||
PORT: 4300
|
PORT: 4300
|
||||||
|
|
||||||
volumes:
|
# volumes:
|
||||||
mysql:
|
# mysql:
|
19
model/userModel.js
Normal file
19
model/userModel.js
Normal 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!
|
||||||
|
}
|
||||||
|
}
|
8
routes/indexRoutes.js
Normal file
8
routes/indexRoutes.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/", (req, res) => {
|
||||||
|
res.render("layout");
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
Loading…
Add table
Reference in a new issue