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>(
9 query: PostgrestFilterBuilder<T>,
11 ): Readable<StoreState<T[]>> {
12 return readable(initialResponse<T[]>(initialValue), (set) => {
14 const { data, error } = await query;
17 return set(errorResponse<T[]>(error));
20 set(response<T[]>(data));
25 export function single<T>(
26 query: PostgrestFilterBuilder<T>,
28 ): Readable<StoreState<T>> {
29 return readable(initialResponse<T>(initialValue), (set) => {
31 const { data, error } = await query.single();
34 return set(errorResponse<T>(error));
37 set(response<T>(data));