An open API service indexing awesome lists of open source software.

https://github.com/brennerm/sveltepocket

Svelte stores and components to query data (with realtime updates) from PocketBase
https://github.com/brennerm/sveltepocket

pocketbase pocketbase-realtime svelte svelte-components svelte5 sveltekit

Last synced: 4 months ago
JSON representation

Svelte stores and components to query data (with realtime updates) from PocketBase

Awesome Lists containing this project

README

        

# SveltePocket

![The Svelte and Pocketbase logo shaking hands](https://github.com/brennerm/sveltepocket/blob/main/logo.png?raw=true)

Svelte 5-ready stores and components to bring data from any Pocketbase instance into your Svelte application (even with realtime updates 🤫).

## Installation

```bash
npm install @shipitdev/sveltepocket
```

## Setup

Call the init method to pass your Pocketbase client SDK instance. This will be used by all stores and components so make sure it's authenticated according to your needs.

```svelte

import { init } from '@shipitdev/sveltepocket';
import Pocketbase from 'pocketbase';

const pb = new Pocketbase(POCKETBASE_URL);
init(pb);

```

## Stores

The stores provide an low level API to query data from your Pocketbase instance and are the best choice when you need to pre/postprocess the data instead of just rendering it onto the page.

### auth

A readable store that holds the current user's authentication status and user record.

```svelte

import { auth } from '@shipitdev/sveltepocket';

{#if $auth.isAuthenticated}

Welcome, {$auth.user.email}!


{/if}
```

### createRecordStore

Creates a readable store that fetches a single record identified by id or filter from a Pocketbase collection.

```svelte

import { createRecordStore } from '@shipitdev/sveltepocket';

const post = createRecordStore('posts', { id: 'YOUR_RECORD_ID' });

{#if $post.record}

{$post.record.title}


{/if}
```

### createRecordsStore

Creates a readable store that fetches multiple records from a Pocketbase collection.

```svelte

import { createRecordsStore } from '@shipitdev/sveltepocket';

const posts = createRecordsStore('posts');

{#if $posts.records}


    {#each $posts.records as post}
  • {post.title}

  • {/each}

{/if}
```

## Components

If you only care about rendering the queried data, these components are the way to go.

You can pass snippets that will be rendered during different states, e.g. when loading the data, when data is not found or when an error occurs.

### \

A component that fetches a single record either by ID or filter from a Pocketbase collection and renders it.

```svelte

{#snippet render(post)}

{post.title}


by {post.expand.author.name}
{/snippet}

{#snippet render(post)}

{post.title}


{/snippet}

```

### \

A component that fetches multiple records from a Pocketbase collection and renders them.

```svelte

{#snippet render(posts)}


    {#each posts as post}
  • {post.title} by {post.expand.author.name}

  • {/each}

{/snippet}

```

## Realtime Updates

All stores and components support the `realtime` parameter. If set to `true`, SveltePocket will setup a subscription to PocketBase's realtime updates and keep the data up to date.

Combined with Svelte's reactivity, your app will rerender automatically when the data changes.

```svelte

{#snippet render(posts)}


    {#each posts as post}
  • {post.title}

  • {/each}

{/snippet}

```

## Type Safety

All stores and components take an optional record type, e.g. generated by [pocketbase-typegen](https://github.com/patmood/pocketbase-typegen).
This gives you full type safety on the returned records.

### Store

```svelte

import { createRecordsStore } from '@shipitdev/sveltepocket';
import type { PostRecord } from './pocketbase-types.d.ts';

const posts = createRecordsStore<PostRecord>('posts');

```

### Components

```svelte

{#snippet render(records: PostRecord[])}
...
{/snippet}

```