refactor(electrobun): simplify bun backend — extract db-utils, merge handlers

- db-utils.ts: shared openDb() (WAL, busy_timeout, foreign_keys, mkdirSync)
- 5 DB modules use openDb() instead of duplicated PRAGMA boilerplate
- bttask-db shares btmsg-db's Database handle (was duplicate connection)
- misc-handlers.ts: 14 inline handlers extracted from index.ts
- index.ts: 349→195 lines (only window controls remain inline)
- updater.ts: removed dead getLastKnownVersion()
- Net reduction: ~700 lines of duplicated boilerplate
This commit is contained in:
Hibryda 2026-03-23 21:09:57 +01:00
parent 2b1194c809
commit f2e8b07d7f
10 changed files with 291 additions and 239 deletions

View file

@ -6,9 +6,9 @@
import { Database } from "bun:sqlite";
import { homedir } from "os";
import { mkdirSync } from "fs";
import { join } from "path";
import { randomUUID } from "crypto";
import { openDb } from "./db-utils.ts";
// ── DB path ──────────────────────────────────────────────────────────────────
@ -158,15 +158,16 @@ export class BtmsgDb {
private db: Database;
constructor() {
mkdirSync(DATA_DIR, { recursive: true });
this.db = new Database(DB_PATH);
this.db.exec("PRAGMA journal_mode = WAL");
this.db.exec("PRAGMA busy_timeout = 5000");
this.db.exec("PRAGMA foreign_keys = ON");
this.db = openDb(DB_PATH, { busyTimeout: 5000, foreignKeys: true });
this.db.exec(SCHEMA);
this.db.exec(TASK_SCHEMA);
}
/** Expose the underlying Database handle for shared-DB consumers (bttask). */
getHandle(): Database {
return this.db;
}
// ── Agents ───────────────────────────────────────────────────────────────
registerAgent(