wf-radio/src/database/index.js

38 lines
975 B
JavaScript
Raw Normal View History

const MySQLDatabase = require("./mysql");
const SQLiteDatabase = require("./sqlite");
let db;
async function connectDatabase(config) {
if (config.type === "mysql") {
db = new MySQLDatabase(config);
} else if (config.type === "sqlite") {
db = new SQLiteDatabase(config);
} else {
throw new Error("Invalid database type: ", config.type);
}
await db.connect();
return db;
}
function getDatabase() {
return db;
}
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");
}
db.exec("CREATE TABLE mods ( \
Name tinytext PRIMARY KEY, \
DisplayName tinytext, \
Author tinytext FOREIGN KEY,\
Versions longtext,\
OtherInfos longtext \
)");
}
module.exports = { getDatabase, connectDatabase, initDatabase };