organisation & minor fixes

This commit is contained in:
2025-12-03 17:24:24 +01:00
parent 2cad7ff6ea
commit 8e20743e8b
30 changed files with 158 additions and 497 deletions

View File

@@ -8,7 +8,6 @@ const databases = new Map();
const DEFAULT_PATHS = {
anilist: path.join(__dirname, '..', 'metadata', 'anilist_anime.db'),
favorites: path.join(os.homedir(), "WaifuBoards", "favorites.db"),
cache: path.join(os.homedir(), "WaifuBoards", "cache.db")
};
@@ -21,7 +20,6 @@ async function ensureExtensionsTable(db) {
title TEXT NOT NULL,
metadata TEXT NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY(ext_name, id)
);
`, (err) => {
@@ -157,7 +155,6 @@ function initDatabase(name = 'anilist', dbPath = null, readOnly = false) {
function getDatabase(name = 'anilist') {
if (!databases.has(name)) {
const readOnly = (name === 'anilist');
return initDatabase(name, null, readOnly);
}
@@ -208,73 +205,11 @@ function closeDatabase(name = null) {
}
}
async function getCachedExtension(extName, id) {
return queryOne(
"SELECT metadata FROM extension WHERE ext_name = ? AND id = ?",
[extName, id]
);
}
async function cacheExtension(extName, id, title, metadata) {
return run(
`
INSERT INTO extension (ext_name, id, title, metadata, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(ext_name, id)
DO UPDATE SET
title = excluded.title,
metadata = excluded.metadata,
updated_at = ?
`,
[extName, id, title, JSON.stringify(metadata), Date.now(), Date.now()]
);
}
async function getExtensionTitle(extName, id) {
const sql = "SELECT title FROM extension WHERE ext_name = ? AND id = ?";
const row = await queryOne(sql, [extName, id], 'anilist');
return row ? row.title : null;
}
async function deleteExtension(extName) {
return run(
"DELETE FROM extension WHERE ext_name = ?",
[extName]
);
}
async function getCache(key) {
return queryOne("SELECT result, created_at, ttl_ms FROM cache WHERE key = ?", [key], "cache");
}
async function setCache(key, result, ttl_ms) {
return run(
`
INSERT INTO cache (key, result, created_at, ttl_ms)
VALUES (?, ?, ?, ?)
ON CONFLICT(key)
DO UPDATE SET result = excluded.result, created_at = excluded.created_at, ttl_ms = excluded.ttl_ms
`,
[key, JSON.stringify(result), Date.now(), ttl_ms],
"cache"
);
}
module.exports = {
initDatabase,
getDatabase,
queryOne,
queryAll,
run,
getCachedExtension,
cacheExtension,
getExtensionTitle,
deleteExtension,
closeDatabase,
getCache,
setCache
closeDatabase
};