Skip to content

Instantly share code, notes, and snippets.

@SergioJuniorCE
Created January 30, 2022 21:23
Show Gist options
  • Save SergioJuniorCE/b598f800abe9709628866b5d96b6a4a9 to your computer and use it in GitHub Desktop.
Save SergioJuniorCE/b598f800abe9709628866b5d96b6a4a9 to your computer and use it in GitHub Desktop.
realtime supabase data
<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>&nbsp; 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