X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/d2cd7f1b4c318ac8587ab3dc1dec4a44b6d592fe..852ee620f0a2f6a83cf83eba860ca951b66bb7e2:/src/lib/stores/supabase.ts?ds=inline diff --git a/src/lib/stores/supabase.ts b/src/lib/stores/supabase.ts new file mode 100644 index 0000000..804141f --- /dev/null +++ b/src/lib/stores/supabase.ts @@ -0,0 +1,38 @@ +import { readable } from 'svelte/store'; +import { initialResponse, errorResponse, response } from './response_builder'; + +import type { Readable } from 'svelte/store'; +import type { PostgrestFilterBuilder } from '@supabase/postgrest-js'; +import type { StoreState } from './response_builder'; + +export function collection (query: PostgrestFilterBuilder, initialValue: T[]): Readable> { + + return readable(initialResponse(initialValue), (set) => { + + (async function() { + const { data, error } = await query; + + if (error) { + return set(errorResponse(error)); + } + + set(response(data)); + })() + }); +} + +export function single (query: PostgrestFilterBuilder, initialValue: T): Readable> { + + return readable(initialResponse(initialValue), (set) => { + + (async function() { + const { data, error } = await query.single(); + + if (error) { + return set(errorResponse(error)); + } + + set(response(data)); + })() + }); +}