import { ClientResponseError } from "pocketbase"; import { type SettingsModel, type SettingsNames } from "@/domain/settings"; import { COLLECTION_NAME_SETTINGS, getPocketBase } from "./_pocketbase"; export const get = async >(name: SettingsNames) => { try { const resp = await getPocketBase().collection(COLLECTION_NAME_SETTINGS).getFirstListItem>(`name='${name}'`, { requestKey: null, }); return resp; } catch (err) { if (err instanceof ClientResponseError && err.status === 404) { return { name: name, content: {} as T, } as SettingsModel; } throw err; } }; export const save = async >(record: MaybeModelRecordWithId>) => { if (record.id) { return await getPocketBase().collection(COLLECTION_NAME_SETTINGS).update>(record.id, record); } return await getPocketBase().collection(COLLECTION_NAME_SETTINGS).create>(record); };