This library was created to make using pocketbase realtime data in svelte apps easier, by providing reactive stores for collections and individual records.
npm install pocketbase-store
Here is example of basic usage for storing some words and sorting them by name and created fields:
<script lang="ts">
import PocketBase from 'pocketbase-store';
type TestItem = {
id: string;
created: string;
updated: string;
collectionId: string;
collectionName: string;
name: string;
};
const pb = new PocketBase('https://your-pocketbase-url.com');
const testStore = pb.collection('example').store<TestItem>({ sort: '-name,created' });
let value = '';
</script>
<form
on:submit|preventDefault={() => {
testStore.create({ name: value });
value = '';
}}
>
<input type="text" bind:value />
<button type="submit">Add</button>
</form>
{#each $testStore as item (item.id)}
{item.name}
<button on:click={() => testStore.delete(item)}>Delete</button>
<br />
{/each}
This CollectionStore
class is wrapped in pb.collection('test').store()
, here is basic usage:
<script lang="ts">
import PocketBase from 'pocketbase-store';
const pb = new PocketBase('https://your-pocketbase-url.com');
const collectionStore = pb.collection('example').store();
</script>
{#each $collectionStore as item (item.id)}
<!-- ... -->
{/each}
And data will refresh automatically when it changes, thanks for realtime data support in pocketbase!
Also you can use basic generics in stores, the example is in Usage section:
I've added some additional methods in this store:
-
getData
: Used for getting data from pocketbase. Returns nothing. By default whenautoSubGetData = true
lunches when initialized, method is async. -
subscribeOnPocketBase
: Used for subscribing on pocketbase. Returns nothing. By default whenautoSubGetData = true
lunches when initialized, method is async. -
create
: Used for changing data in the store first, and after in pocketbase. Returns nothing. -
update_
: Used for updating data in the store first, and after in pocketbase. Returns nothing. Also namedupdate_
becauseupdate
is already used inwritable
, sadly :(. -
delete
: Used for deleting data in the store first, and after in pocketbase. Returns nothing.
All this methods are async methods, also they changes the store much faster, then waiting for response from pocketbase.
Collection Store is a class, that implements writable
interface. That means, that you can change data in store. But sadly this changes won't be reflected in pocketbase, I want to fix this, in the future, this is planed. Now use update_
method instead.
You can add 2 additional arguments to store
method:
options
:CollectionSendOptions
, you can find it in Types section.initialValue
:T[]
, it's an array of objects that will be stored in the store, instead of callinggetFullList
method from pocketbase.autoSubGetData
:boolean
, if you want to manage getting data and subscribing on pocketbase by methodsgetData
andsubscribeOnPocketBase
, set it tofalse
, by default it'strue
.
type CollectionSendOptions = {
expand?: string; // string like 'name,created', more info in pocketbase admin ui docs
fields?: string; // string like 'name,created' for sorting witch fields to send
filter?: string; // string like 'name = "test"', more info here: https://pocketbase.io/docs/api-rules-and-filters/
sort?: string; // string like '-name,created', more info in pocketbase admin ui docs
expirationTime?: number; // time in milliseconds then data in cache will be deleted (by default 4 seconds)
autoSubGetData?: boolean; // if you want to manage getting data and subscribing on pocketbase by methods `getData` and `subscribeOnPocketBase`, set it to `false`, by default it's `true`
};
// Every T generic type better to have all Record fields
type Record = {
id: string;
created: string;
updated: string;
collectionId: string;
collectionName: string;
};
This ItemStore
class is wrapped in pb.collection('test').storeItem()
, here is basic usage:
<script lang="ts">
import PocketBase from 'pocketbase-store';
const pb = new PocketBase('https://your-pocketbase-url.com');
const itemStore = pb.collection('example').storeItem('RECORD_ID');
</script>
<input type="text" bind:value={$itemStore.name} />
<button on:click={async () => await $itemStore.send()}>Send</button>
Note
When you write something in input, it will be changed in store, and on calling $itemStore.send()
, it will be sent to pocketbase.
Here is supported generics, all I can say.
I've added some additional methods in this store:
-
getData
: Used for getting data from pocketbase. Returns nothing. By default whenautoSubGetData = true
lunches when initialized, method is async. -
subscribeOnPocketBase
: Used for subscribing on pocketbase. Returns nothing. By default whenautoSubGetData = true
lunches when initialized, method is async. -
send
: Used for refreshing data in the pocketbase.
When creating new ItemStore, you can add next arguments:
initialValue (required)
:T | string
, used for initializing new item, can hold string like'RECORD_ID'
and object like{ id: 'RECORD_ID', collectionId: 'COLLECTION_ID', collectionName: 'COLLECTION_NAME', etc.}
.options
:ItemSendOptions
, you can find it in Types section.
type ItemSendOptions = {
expand?: string; // string like 'name,created', more info in pocketbase admin ui docs
fields?: string; // string like 'name,created' for sorting witch fields to send
expirationTime?: number; // time in milliseconds then data in cache will be deleted (by default 4 seconds)
autoSubGetData?: boolean; // if you want to manage getting data and subscribing on pocketbase by methods `getData` and `subscribeOnPocketBase`, set it to `false`, by default it's `true`
};
MIT License, do what you want with it, this is your responsibility.