]>
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>( | |
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 | }); | |
23 | } | |
24 | ||
25 | export 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 | }); | |
40 | } |