]>
Commit | Line | Data |
---|---|---|
1 | import { readable } from 'svelte/store'; | |
2 | import { initialResponse, errorResponse, response } from './response_builder'; | |
3 | ||
4 | import type { Readable } from 'svelte/store'; | |
5 | import type { PostgrestFilterBuilder } from '@supabase/postgrest-js'; | |
6 | import type { StoreState } from './response_builder'; | |
7 | ||
8 | export 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 | ||
24 | export 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 | } |