npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pocketbase-store

v0.1.6

Published

Svelte store for pocketbase collections and records

Downloads

111

Readme

pocketbase-store

Overview

This library was created to make using pocketbase realtime data in svelte apps easier, by providing reactive stores for collections and individual records.

Installation

npm install pocketbase-store

Usage

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}

Elements

Collection Store

Overview

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!

Generics

Also you can use basic generics in stores, the example is in Usage section:

Additional methods

I've added some additional methods in this store:

  • getData: Used for getting data from pocketbase. Returns nothing. By default when autoSubGetData = true lunches when initialized, method is async.

  • subscribeOnPocketBase: Used for subscribing on pocketbase. Returns nothing. By default when autoSubGetData = 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 named update_ because update is already used in writable, 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.

Writible

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.

Options

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 calling getFullList method from pocketbase.
  • 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.

Types

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;
};

Item Store

Overview

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.

Generics

Here is supported generics, all I can say.

Additional methods

I've added some additional methods in this store:

  • getData: Used for getting data from pocketbase. Returns nothing. By default when autoSubGetData = true lunches when initialized, method is async.

  • subscribeOnPocketBase: Used for subscribing on pocketbase. Returns nothing. By default when autoSubGetData = true lunches when initialized, method is async.

  • send: Used for refreshing data in the pocketbase.

Options

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.

Types

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`
};

LICENSE

MIT License, do what you want with it, this is your responsibility.