sqlite local

This commit is contained in:
m
2026-09-09 23:12:15 +02:00
parent e1360c19a2
commit 7dd29239b7
21 changed files with 2284 additions and 101 deletions
+22 -1
View File
@@ -1,11 +1,32 @@
import { getDatabase } from '../client';
import { PREFERENCES_KEY } from '../schema';
import { DEVICE_USER_ID_KEY, PREFERENCES_KEY } from '../schema';
import type { UserPreferences } from '../types';
const DEFAULT_PREFERENCES: UserPreferences = {
syncMode: 'full',
};
export async function getDeviceUserId(): Promise<string> {
const db = await getDatabase();
const row = await db.getFirstAsync<{ value: string }>(
'SELECT "value" FROM user_preferences WHERE "key" = ?',
DEVICE_USER_ID_KEY,
);
if (row) return row.value;
await db.runAsync(
`INSERT OR IGNORE INTO user_preferences ("key", "value", updated_at)
VALUES (?, lower(hex(randomblob(16))), ?)`,
DEVICE_USER_ID_KEY,
Date.now(),
);
const seeded = await db.getFirstAsync<{ value: string }>(
'SELECT "value" FROM user_preferences WHERE "key" = ?',
DEVICE_USER_ID_KEY,
);
return seeded!.value;
}
export async function saveUserPreferences(preferences: UserPreferences): Promise<void> {
const db = await getDatabase();
await db.runAsync(