import type { Database } from "bun:sqlite"; /** A repo row as the console edits it (ยง24): every repo the sync found, public or not. */ export type AdminRepo = { name: string; public: number; category: string; homepage: string; /** Shown languages, joined by ", "; '' shows the detected one. */ languages: string; issues_paused: number; /** Hidden paths, one per line; '' hides nothing. */ hidden_paths: string; last_sync_at: number | null; last_sync_error: string | null; }; export type RepoEdit = Omit; export function allRepos(db: Database): AdminRepo[] { return db.query("SELECT * FROM repos ORDER BY name").all(); } export function findRepo(db: Database, name: string): AdminRepo | null { return db.query("SELECT * FROM repos WHERE name = ?").get(name); } export function updateRepo(db: Database, name: string, edit: RepoEdit): void { db.query( `UPDATE repos SET public = ?, category = ?, homepage = ?, languages = ?, issues_paused = ?, hidden_paths = ? WHERE name = ?`, ).run( edit.public, edit.category, edit.homepage, edit.languages, edit.issues_paused, edit.hidden_paths, name, ); } // Every kepr.db table that names a repo, and its column. const REPO_COLUMNS = [ ["repos", "name"], ["repo_text", "repo"], ["release_settings", "repo"], ["downloads", "repo"], ["download_days", "repo"], ["installs", "repo"], ["issues", "repo"], ["blocked_reporters", "repo"], ] as const; /** Moves a renamed repo's settings, texts, issues and counts to its new name, and records it. */ export function moveRepoRows(db: Database, from: string, to: string): void { db.transaction(() => { for (const [table, column] of REPO_COLUMNS) { db.query(`UPDATE ${table} SET ${column} = ? WHERE ${column} = ?`).run(to, from); } db.query("DELETE FROM renamed WHERE old = ?").run(to); db.query("INSERT OR REPLACE INTO renamed (old, new) VALUES (?, ?)").run(from, to); })(); } /** Writes site settings in one transaction; an empty value removes the key, so defaults apply. */ export function saveSettings(db: Database, values: Record): void { db.transaction(() => { for (const [key, value] of Object.entries(values)) { if (value === "") db.query("DELETE FROM settings WHERE key = ?").run(key); else db.query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)").run(key, value); } })(); }