2025-03-10 22:51:57 +01:00
|
|
|
const MySQLDatabase = require("./mysql");
|
|
|
|
const SQLiteDatabase = require("./sqlite");
|
|
|
|
|
2025-03-29 00:31:25 +01:00
|
|
|
let db;
|
|
|
|
|
|
|
|
async function connectDatabase(config) {
|
2025-03-10 22:51:57 +01:00
|
|
|
if (config.type === "mysql") {
|
2025-03-29 00:31:25 +01:00
|
|
|
db = new MySQLDatabase(config);
|
2025-03-10 22:51:57 +01:00
|
|
|
} else if (config.type === "sqlite") {
|
2025-03-29 00:31:25 +01:00
|
|
|
db = new SQLiteDatabase(config);
|
2025-03-10 22:51:57 +01:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid database type: ", config.type);
|
|
|
|
}
|
2025-03-29 00:31:25 +01:00
|
|
|
|
|
|
|
await db.connect();
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDatabase() {
|
|
|
|
return db;
|
2025-03-10 22:51:57 +01:00
|
|
|
}
|
|
|
|
|
2025-03-29 12:45:59 +01:00
|
|
|
// Setups the database by creating the tables and the default objects
|
|
|
|
function initDatabase() {
|
|
|
|
if (db == null) {
|
|
|
|
throw new Error("Database is not connected");
|
|
|
|
}
|
|
|
|
|
2025-03-29 12:47:10 +01:00
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS mods ( \
|
2025-03-29 12:45:59 +01:00
|
|
|
Name tinytext PRIMARY KEY, \
|
|
|
|
DisplayName tinytext, \
|
|
|
|
Author tinytext FOREIGN KEY,\
|
|
|
|
Versions longtext,\
|
|
|
|
OtherInfos longtext \
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getDatabase, connectDatabase, initDatabase };
|