]> git.r.bdr.sh - rbdr/forum/blame - src/lib/stores/supabase.ts
Use supabase
[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
8export function collection<T> (query: PostgrestFilterBuilder<T>, initialValue: T[]): Readable<StoreState<T[]>> {
9
10 return readable(initialResponse<T[]>(initialValue), (set) => {
11
12 (async function() {
13 const { data, error } = await query;
14
15 if (error) {
16 return set(errorResponse<T[]>(error));
17 }
18
19 set(response<T[]>(data));
20 })()
21 });
22}
23
24export function single<T> (query: PostgrestFilterBuilder<T>, initialValue: T): Readable<StoreState<T>> {
25
26 return readable(initialResponse<T>(initialValue), (set) => {
27
28 (async function() {
29 const { data, error } = await query.single();
30
31 if (error) {
32 return set(errorResponse<T>(error));
33 }
34
35 set(response<T>(data));
36 })()
37 });
38}