Created
January 30, 2022 21:23
-
-
Save SergioJuniorCE/b598f800abe9709628866b5d96b6a4a9 to your computer and use it in GitHub Desktop.
realtime supabase data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { supabase } from '$lib/database'; | |
import { readable, get } from 'svelte/store'; | |
const posts = readable(null, (set) => { | |
supabase | |
.from('posts') | |
.select('*') | |
.then(({ data }) => set(data)); | |
const subscriptions = supabase | |
.from('posts') | |
.on('*', (payload) => { | |
if (payload.eventType === 'INSERT') { | |
set([...get(posts), payload.new]); | |
} | |
}) | |
.subscribe(); | |
return () => { | |
supabase.removeSubscription(subscriptions); | |
}; | |
}); | |
</script> | |
<main> | |
{#if $posts} | |
{#each $posts as { title, featured }} | |
{#if featured === false} | |
<li><a class="h6" style="color: cornflowerblue" href="/posts/{title}">{title}</a></li> | |
{/if} | |
{:else} | |
<p>No posts atm</p> | |
{/each} | |
{:else} | |
<div class="d-flex justify-content-center"> | |
<div class="spinner-border" role="status"> | |
<span class="visually-hidden">Loading...</span> | |
</div> | |
<span> Waiting on posts</span> | |
</div> | |
{/if} | |
</main> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment