DataFn
@datafn/client

@datafn/svelte

API reference for the DataFn Svelte adapter package.

Package

npm install @datafn/svelte

Peer dependencies: svelte ^3.0.0 || ^4.0.0


toSvelteStore<T>(signal)

Converts a DatafnSignal<T> to a Svelte Readable store.

function toSvelteStore<T>(signal: DatafnSignal<T>): DatafnSvelteStore<T>;

Parameters

ParameterTypeDescription
signalDatafnSignal<T>A signal from a table handle, e.g. client.todos.signal({ ... }).

Returns

A DatafnSvelteStore<T> — a Svelte Readable store that emits signal state.

Example

import { toSvelteStore } from "@datafn/svelte";

const todosStore = toSvelteStore(
  client.todos.signal({ sort: ["-createdAt"] })
);
{#if $todosStore.loading}
  <p>Loading...</p>
{:else}
  {#each $todosStore.data ?? [] as todo}
    <p>{todo.title}</p>
  {/each}
{/if}

DatafnSvelteStore<T>

type DatafnSvelteStore<T> = Readable<{
  data: T;
  loading: boolean;
  error: DatafnError | null;
  refreshing: boolean;
  nextCursor?: string | null;
}>;

Properties

PropertyTypeDescription
dataTCurrent query result value.
loadingbooleantrue during initial fetch.
errorDatafnError | nullNon-null if the last fetch failed.
refreshingbooleantrue while refreshing after mutation/sync changes.
nextCursorstring | nullCursor for next page, or null.

toSvelteStore<T, C>(clientRef, signalFactory)

Overload for client-reference driven signal creation.

function toSvelteStore<T, C>(
  clientRef: ClientRef<C>,
  signalFactory: (client: C) => DatafnSignal<T>,
): DatafnSvelteStore<T>;

Parameters

ParameterTypeDescription
clientRefClientRef<C>A subscribable client reference.
signalFactory(client: C) => DatafnSignal<T>Creates a signal from the current client.

ClientRef<C>

type ClientRef<C> = {
  subscribe(fn: (client: C) => void): () => void;
};