feat: add settings service
This commit is contained in:
parent
ed4dd44a69
commit
7b6aecd385
17
app/services/settings.ts
Normal file
17
app/services/settings.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { pb } from '~/services/pocketbase';
|
||||
import { DEFAULT_SETTINGS, SETTINGS_COLLECTION } from '~/data/settings/constants';
|
||||
import type { AppSettings, SettingKey } from '~/types/settings';
|
||||
|
||||
const settings = () => pb.collection<AppSettings & { id: string }>(SETTINGS_COLLECTION);
|
||||
|
||||
// A single settings record, seeded by the migration; defaults apply if it is missing
|
||||
export async function getSettings(): Promise<AppSettings> {
|
||||
const { items } = await settings().getList(1, 1);
|
||||
return items[0] ?? DEFAULT_SETTINGS;
|
||||
}
|
||||
|
||||
export async function updateSetting(key: SettingKey, value: boolean) {
|
||||
const current = await getSettings();
|
||||
if (!current.id) throw new Error('Settings are not set up yet');
|
||||
return settings().update(current.id, { [key]: value });
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user