Restructured and organized the whole project, fixed SQLite database handler (now using better-sqlite3), made first model functions to interact with the database
This commit is contained in:
parent
99591a7191
commit
0ee5eedcfd
|
@ -2,7 +2,6 @@
|
||||||
"port": 8000,
|
"port": 8000,
|
||||||
|
|
||||||
"database": {
|
"database": {
|
||||||
"type": "sqlite",
|
"type": "sqlite"
|
||||||
"filename": "./mydb.sqlite"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
84
database.js
84
database.js
|
@ -1,84 +0,0 @@
|
||||||
class Database {
|
|
||||||
|
|
||||||
async connect() {
|
|
||||||
throw new Error("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
async close() {
|
|
||||||
throw new Error("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
async query(sql, params) {
|
|
||||||
throw new Error("Not implemented");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SQLiteDatabase extends Database {
|
|
||||||
constructor(config) {
|
|
||||||
super();
|
|
||||||
const sqlite3 = require("sqlite3").verbose();
|
|
||||||
const { open } = require("sqlite");
|
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
async connect() {
|
|
||||||
this.db = await open({
|
|
||||||
filename: this.config.file_path,
|
|
||||||
driver: sqlite3.Database,
|
|
||||||
});
|
|
||||||
console.log("Connected to SQLite");
|
|
||||||
}
|
|
||||||
|
|
||||||
async close() {
|
|
||||||
await this.db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
async query(sql, params) {
|
|
||||||
throw new Error("Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class MySQLDatabase extends Database {
|
|
||||||
constructor(config) {
|
|
||||||
super();
|
|
||||||
const mysql = require("mysql2/promise");
|
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async connect() {
|
|
||||||
this.db = await mysql.createConnection({
|
|
||||||
host: this.config.host,
|
|
||||||
user: this.config.user,
|
|
||||||
password: this.config.password,
|
|
||||||
database: this.config.database,
|
|
||||||
});
|
|
||||||
console.log("Connected to MySQL");
|
|
||||||
}
|
|
||||||
|
|
||||||
async close() {
|
|
||||||
await this.db.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
async query(sql, params) {
|
|
||||||
throw new Error("Not implemented");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function connectDatabase(config) {
|
|
||||||
if (config.type === "mysql") {
|
|
||||||
return new MySQLDatabase(config);
|
|
||||||
} else if (config.type === "sqlite") {
|
|
||||||
return new SQLiteDatabase(config);
|
|
||||||
} else {
|
|
||||||
throw new Error("Invalid database type: ", config.type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = { Database, MySQLDatabase, SQLiteDatabase };
|
|
||||||
module.exports = { connectDatabase };
|
|
|
@ -4,6 +4,7 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "node server.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
|
@ -11,13 +12,13 @@
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"packageManager": "pnpm@10.5.2",
|
"packageManager": "pnpm@10.5.2",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"better-sqlite3": "^11.8.1",
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1"
|
||||||
"sqlite3": "^5.1.7"
|
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"ignoredBuiltDependencies": [
|
"onlyBuiltDependencies": [
|
||||||
"sqlite3"
|
"better-sqlite3"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
773
pnpm-lock.yaml
773
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
12
server.js
12
server.js
|
@ -4,12 +4,12 @@ const express = require("express");
|
||||||
const app = express();
|
const app = express();
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const { MySQLDatabase, SQLiteDatabase, connectDatabase } = require('./database');
|
const { getDatabase } = require('./src/database/index');
|
||||||
|
|
||||||
|
|
||||||
const config_folder = path.join(__dirname, "config");
|
const config_folder = "config";
|
||||||
const config_file_name = "config.json"
|
const config_file_name = "config.json"
|
||||||
const data_folder = path.join(__dirname, "db");
|
const data_folder = "data";
|
||||||
const sqlite_file_name = "sqlite.db"
|
const sqlite_file_name = "sqlite.db"
|
||||||
|
|
||||||
const default_port = 8000
|
const default_port = 8000
|
||||||
|
@ -34,14 +34,14 @@ const port = config.port || default_port;
|
||||||
console.debug("Port: ", port);
|
console.debug("Port: ", port);
|
||||||
|
|
||||||
// Database connection
|
// Database connection
|
||||||
db = connectDatabase(config.database);
|
db = getDatabase(config.database);
|
||||||
|
|
||||||
// --- Routing ---
|
// --- Routing ---
|
||||||
|
|
||||||
app.use("/mods", require("./routes/mods"));
|
app.use("/mods", require("./src/routes/mods"));
|
||||||
|
|
||||||
// --- Launch ---
|
// --- Launch ---
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log("Server listening on port " + port);
|
console.log("Server listening on port " + port + "...");
|
||||||
})
|
})
|
15
src/database/index.js
Normal file
15
src/database/index.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
const MySQLDatabase = require("./mysql");
|
||||||
|
const SQLiteDatabase = require("./sqlite");
|
||||||
|
|
||||||
|
function getDatabase(config) {
|
||||||
|
if (config.type === "mysql") {
|
||||||
|
return new MySQLDatabase(config);
|
||||||
|
} else if (config.type === "sqlite") {
|
||||||
|
return new SQLiteDatabase(config);
|
||||||
|
} else {
|
||||||
|
throw new Error("Invalid database type: ", config.type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = { getDatabase };
|
30
src/database/mysql.js
Normal file
30
src/database/mysql.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
class MySQLDatabase {
|
||||||
|
constructor(config) {
|
||||||
|
const mysql = require("mysql2/promise");
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async connect() {
|
||||||
|
this.db = await mysql.createConnection({
|
||||||
|
host: this.config.host,
|
||||||
|
user: this.config.user,
|
||||||
|
password: this.config.password,
|
||||||
|
database: this.config.database,
|
||||||
|
});
|
||||||
|
console.log("Connected to MySQL");
|
||||||
|
}
|
||||||
|
|
||||||
|
async close() {
|
||||||
|
await this.db.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
async query(sql, params) {
|
||||||
|
throw new Error("Not implemented"); //TODO
|
||||||
|
const [results] = await this.db.execute(sql, params);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = MySQLDatabase;
|
41
src/database/sqlite.js
Normal file
41
src/database/sqlite.js
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
class SQLiteDatabase {
|
||||||
|
constructor(config) {
|
||||||
|
const sqlite = require("better-sqlite3");
|
||||||
|
this.config = config;
|
||||||
|
this.db = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async connect() {
|
||||||
|
try {
|
||||||
|
const db = new sqlite("./data/sqlite.db")
|
||||||
|
// db.pragma("journal_mode = WAL")
|
||||||
|
console.log("Connected to SQLite");
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error connecting to SQLite database: ", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async close() {
|
||||||
|
if (this.db && this.db.open) {
|
||||||
|
this.db.close();
|
||||||
|
console.debug("Closed database connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async query(sql, params = []) {
|
||||||
|
try {
|
||||||
|
if (params.length > 0) {
|
||||||
|
return this.db.prepare(sql).all(params);
|
||||||
|
} else {
|
||||||
|
return this.db.prepare(sql).all();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error executing query: ", err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SQLiteDatabase;
|
25
src/models/mod.js
Normal file
25
src/models/mod.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// const db = require("...");
|
||||||
|
|
||||||
|
async function getAllMods() {
|
||||||
|
return db.query("SELECT * FROM mods");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getModsByName(name) {
|
||||||
|
return db.query("SELECT * FROM mods WHERE name = ?", [name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- WIP ---
|
||||||
|
|
||||||
|
async function createMod(mod_data) {
|
||||||
|
console.log("WARNING: using a WIP function : createMod (models/mods.js)")
|
||||||
|
const { name, description } = mod_data;
|
||||||
|
return db.query("INSERT INTO mods (name, description) VALUES (?, ?)", [name, description]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteMod(name) {
|
||||||
|
console.log("WARNING: using a WIP function : deleteMod (models/mods.js)")
|
||||||
|
return db.query("DELETE FROM mods WHERE name = ?", [name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = { getAllMods, getModsByName }
|
0
src/models/modpacks.js
Normal file
0
src/models/modpacks.js
Normal file
0
src/routes/modpacks.js
Normal file
0
src/routes/modpacks.js
Normal file
|
@ -4,7 +4,7 @@ const router = express.Router();
|
||||||
|
|
||||||
// List mods
|
// List mods
|
||||||
router.get("/list", async (req,res) => {
|
router.get("/list", async (req,res) => {
|
||||||
console.log("Accessing mods list");
|
console.debug("Accessing mods list");
|
||||||
res.send("No list yet");
|
res.send("No list yet");
|
||||||
});
|
});
|
||||||
|
|
0
src/services/modService.js
Normal file
0
src/services/modService.js
Normal file
0
src/services/modpackService.js
Normal file
0
src/services/modpackService.js
Normal file
Loading…
Reference in a new issue