sveltelean Query
Version v0.2.0

Import

import
import { persist, indexedDbStore } from '@svelte-lean/query/persist';

Usage

src/lib/api/modules.ts
// src/lib/api/modules.ts
import { defineQuery, type } from '@svelte-lean/query';
import { indexedDbStore, persist } from '@svelte-lean/query/persist';
import { session } from '$api/session.svelte';
import type { Team } from '$api/types';

export const storage = persist({
	store: indexedDbStore(),
	session: () => (session.userId === null ? null : String(session.userId)), // null: signed out
	buster: 'schema-3', // a new value drops every record
	maxAge: '24h'
});

// Only endpoints that opt in are stored.
export const listAllTeams = defineQuery({
	get: 'api/teams/all',
	result: type<Team[]>(),
	persist: true
});

Size

Production Vite builds of a small consumer entry, Svelte external. A marginal row is the bytes the entry adds over the row it names.

BuildOvergzipbrotliBudget (gzip)
persist (marginal)query3428 B3201 B3700 B

Read from packages/query/artifacts/size.json. Every entry and its method: Performance.

Functions

The module and the IndexedDB store.

Exports

Runtime exports of the entry.

NameTypeDefaultDescription
# persist (init: PersistInit) => PersistModule–The module; only endpoints with persist are stored.
# indexedDbStore (init?: { name?: string }) => PersistStorename 'slq'A PersistStore on IndexedDB.

Options

PersistInit, the argument of persist().

PersistInit

store and session are required.

NameTypeDefaultDescription
# store requiredPersistStore–Where records live.
# session required() => string | null–Reactive. The session the records belong to; null (signed out) reads and writes nothing.
# buster string–Build or schema version; a different value drops every record.
# maxAge Duration'24h'Record lifetime; an endpoint's persist.maxAge wins.
# maxBytes number8 MiBStored text per session partition; the oldest records are evicted first.
# eager QueryEndpoint[]–Restored before ready resolves (at setup and on every session change).
# compress number | false262144gzip values longer than this many characters; false: never.

PersistModule

What persist() returns, beside the Module fields.

NameTypeDefaultDescription
# ready Promise<void>–The eager restore is done (or timed out); never rejects.
# forget () => Promise<void>–Delete this session's partition.

Store

The contract of a store; persistStoreSuite() in ./testing checks an implementation.

PersistStore

Every method is asynchronous.

NameTypeDefaultDescription
# get (keys) => Promise<(PersistRecord | undefined)[]>–Read records by key.
# put (records) => Promise<void>–Write each record unless the stored one has a newer updatedAt.
# delete (keys) => Promise<void>–Delete records.
# list (prefix) => Promise<PersistMeta[]>–Metadata of every key starting with prefix.
# close () => void–Optional: close the store connection.

PersistMeta

A record without its value; PersistRecord adds value.

NameTypeDefaultDescription
# key string–Session digest and entry key.
# endpoint string–The endpoint id.
# updatedAt number–Server commit time of the value.
# writtenAt number–Time of the write (for maxAge and eviction).
# bytes number–Length of the JSON text.
# buster string–The buster the record was written with.

Guide

How to use it, with examples: Persistence. All entry points: Svelte Lean Query.