import { getDatabase } from '../client'; import { PREFERENCES_KEY } from '../schema'; import type { UserPreferences } from '../types'; const DEFAULT_PREFERENCES: UserPreferences = { syncMode: 'full', }; export async function saveUserPreferences(preferences: UserPreferences): Promise { const db = await getDatabase(); await db.runAsync( `INSERT INTO user_preferences ("key", "value", updated_at) VALUES (?, ?, ?) ON CONFLICT("key") DO UPDATE SET "value" = excluded."value", updated_at = excluded.updated_at`, PREFERENCES_KEY, JSON.stringify(preferences), Date.now(), ); } export async function getUserPreferences(): Promise { const db = await getDatabase(); const row = await db.getFirstAsync<{ value: string }>( 'SELECT "value" FROM user_preferences WHERE "key" = ?', PREFERENCES_KEY, ); if (!row) return DEFAULT_PREFERENCES; try { return { ...DEFAULT_PREFERENCES, ...JSON.parse(row.value) } satisfies UserPreferences; } catch { return DEFAULT_PREFERENCES; } }