1 import { readable } from 'svelte/store';
2 import { initialResponse, errorResponse, response } from './response_builder';
4 import type { Readable } from 'svelte/store';
5 import type { PostgrestFilterBuilder } from '@supabase/postgrest-js';
6 import type { StoreState } from './response_builder';
8 export function collection<T> (query: PostgrestFilterBuilder<T>, initialValue: T[]): Readable<StoreState<T[]>> {
10 return readable(initialResponse<T[]>(initialValue), (set) => {
13 const { data, error } = await query;
16 return set(errorResponse<T[]>(error));
19 set(response<T[]>(data));
24 export function single<T> (query: PostgrestFilterBuilder<T>, initialValue: T): Readable<StoreState<T>> {
26 return readable(initialResponse<T>(initialValue), (set) => {
29 const { data, error } = await query.single();
32 return set(errorResponse<T>(error));
35 set(response<T>(data));