]> git.r.bdr.sh - rbdr/forum/blame - src/lib/stores/supabase.ts
Don't remember what this WIP was about
[rbdr/forum] / src / lib / stores / supabase.ts
CommitLineData
852ee620
RBR
1import { readable } from 'svelte/store';
2import { initialResponse, errorResponse, response } from './response_builder';
3
4import type { Readable } from 'svelte/store';
5import type { PostgrestFilterBuilder } from '@supabase/postgrest-js';
6import type { StoreState } from './response_builder';
7
6ccc6f60
RBR
8export function collection<T>(
9 query: PostgrestFilterBuilder<T>,
10 initialValue: T[]
11): Readable<StoreState<T[]>> {
12 return readable(initialResponse<T[]>(initialValue), (set) => {
13 (async function () {
14 const { data, error } = await query;
15
16 if (error) {
17 return set(errorResponse<T[]>(error));
18 }
19
20 set(response<T[]>(data));
21 })();
22 });
852ee620
RBR
23}
24
6ccc6f60
RBR
25export function single<T>(
26 query: PostgrestFilterBuilder<T>,
27 initialValue: T
28): Readable<StoreState<T>> {
29 return readable(initialResponse<T>(initialValue), (set) => {
30 (async function () {
31 const { data, error } = await query.single();
32
33 if (error) {
34 return set(errorResponse<T>(error));
35 }
36
37 set(response<T>(data));
38 })();
39 });
852ee620 40}