import Dexie, { type EntityTable } from "dexie"; import type { CredentialRef, ServerProfile, SessionLog } from "@remoteconn/shared"; import type { EncryptedCredentialPayload, GlobalSettings, VoiceRecord } from "@/types/app"; import type { PluginPackage, PluginRecord } from "@remoteconn/plugin-runtime"; interface KnownHostEntity { key: string; fingerprint: string; updatedAt: string; } interface SettingEntity { key: string; value: GlobalSettings; } interface PluginDataEntity { key: string; value: unknown; } class RemoteConnDb extends Dexie { public servers!: EntityTable; public credentialRefs!: EntityTable; public credentials!: EntityTable; public sessionLogs!: EntityTable; public knownHosts!: EntityTable; public settings!: EntityTable; public pluginPackages!: EntityTable; public pluginRecords!: EntityTable; public pluginData!: EntityTable; public voiceRecords!: EntityTable; public constructor() { super("remoteconn_db"); this.version(2).stores({ servers: "id, name, host, lastConnectedAt", credentialRefs: "id, type, updatedAt", credentials: "id, refId, updatedAt", sessionLogs: "sessionId, serverId, startAt, status", knownHosts: "key, updatedAt", settings: "key", pluginPackages: "id", pluginRecords: "id, status", pluginData: "key" }); this.version(3).stores({ servers: "id, name, host, lastConnectedAt", credentialRefs: "id, type, updatedAt", credentials: "id, refId, updatedAt", sessionLogs: "sessionId, serverId, startAt, status", knownHosts: "key, updatedAt", settings: "key", pluginPackages: "id", pluginRecords: "id, status", pluginData: "key", voiceRecords: "id, createdAt, serverId" }); } } export const db = new RemoteConnDb(); async function ensureDbOpen(): Promise { if (!db.isOpen()) { await db.open(); } } export async function getSettings(): Promise { await ensureDbOpen(); const row = await db.settings.get("global"); return row?.value ?? null; } export async function setSettings(value: GlobalSettings): Promise { await ensureDbOpen(); await db.settings.put({ key: "global", value }); } export async function getKnownHosts(): Promise> { await ensureDbOpen(); const rows = await db.knownHosts.toArray(); return Object.fromEntries(rows.map((row) => [row.key, row.fingerprint])); } export async function upsertKnownHost(key: string, fingerprint: string): Promise { await ensureDbOpen(); await db.knownHosts.put({ key, fingerprint, updatedAt: new Date().toISOString() }); }