TanStack DB

repository·main·Indexed 25 days ago

https://github.com/tanstack/db

A reactive, embedded client-side database designed to optimize data loading and interaction performance using normalized collections and real-time reactivity. It supports offline-first patterns via @tanstack/offline-transactions, featuring outbox patterns, automatic retries, and optimistic updates. Compatible with multiple storage backends including IndexedDB, localStorage, and wa-sqlite OPFS, with specific implementations for Web and React Native.

Tokens
274.9K
Snippets
703
Records
1.3K
Agent score
84%

What's inside TanStack DB

  1. Overview of TanStack DB

    main

    TanStack DB is a reactive client-side data store designed for high-performance data management. It provides normalized collections, sub-millisecond live queries using differential dataflow (d2ts), and instant optimistic mutations with automatic rollback capabilities.

    It is designed to work with various data sources, including:

    • REST APIs (via TanStack Query)
    • Sync engines (e.g., ElectricSQL, PowerSync, RxDB, TrailBase)
    • Local storage

    It features framework adapters for React, Vue, Svelte, Solid, and Angular.

  2. Explore Offline Transaction patterns in TanStack DB

    main

    The Offline Transactions example demonstrates several key patterns for building resilient, offline-capable applications using @tanstack/offline-transactions:

    • Outbox pattern: Mutations are persisted to local storage before attempting to sync with the server.
    • Automatic retry: Failed operations automatically retry using exponential backoff once connectivity is restored.
    • Optimistic updates: The UI updates immediately to reflect changes while mutations are processed in the background.
    • Multi-tab coordination: Uses leader election to ensure that only one browser tab manages the offline storage at a time.
    • Collection-level persistence: When using the wa-sqlite route, data is stored in a real SQLite database in the browser via OPFS, allowing data to survive page reloads even without server synchronization.
  3. Understand @tanstack/db-sqlite-persistence-core

    main

    The @tanstack/db-sqlite-persistence-core package provides shared SQLite persistence primitives for TanStack DB. It includes generic persisted collection wrapper utilities, shared persistence/coordinator protocol types, the SQLite core persistence adapter, validation/storage key helpers, and shared error types.

    Important: This package does not include a concrete SQLite engine binding. To use it, you must provide a runtime SQLiteDriver implementation, typically obtained from a runtime-specific wrapper package.

  4. Use Live Queries in React with @tanstack/react-db

    main

    The @tanstack/react-db package provides React hooks for interacting with TanStack DB collections in a reactive, live manner. Key hooks include:

    • useLiveQuery: For subscribing to a collection or query and receiving real-time updates.
    • useLiveInfiniteQuery: For handling infinite scrolling/pagination with live updates.
    • useLiveSuspenseQuery: For using React Suspense to handle loading states during live queries.
    • useLiveQueryEffect: For running side effects when query results change.
    • usePacedMutations: For managing mutations with pacing/throttling logic.
  5. Understand TanStack DB Core Concepts

    main

    TanStack DB is a reactive client-side data store designed to load typed data into collections from various backends (REST APIs, sync engines, or local storage). It provides sub-millisecond live queries using differential dataflow and supports instant optimistic mutations with automatic rollback.

    Key Features

    • Live Queries: High-performance queries using a builder pattern (from, where, join, select, groupBy, orderBy, limit).
    • Optimistic Mutations: Instant UI updates using a draft proxy via collection.insert, collection.update, and collection.delete.
    • Advanced Mutation Control: Tools like createOptimisticAction, createTransaction, and createPacedMutations for managing state and synchronization.
    • Framework Integration: Framework-specific packages (@tanstack/react-db, @tanstack/vue-db, @tanstack/svelte-db, @tanstack/solid-db) re-export @tanstack/db and add framework-specific hooks.
  6. Understand TanStack DB Mutations

    main

    TanStack DB uses an optimistic mutation pattern to provide a highly responsive user experience. The lifecycle follows these steps:

    1. Optimistic state applied: Local changes are applied immediately to the collection.
    2. Handler invoked: The appropriate handler (onInsert, onUpdate, onDelete, or a custom mutationFn) is called to persist the change.
    3. Backend persistence: The handler sends the data to your backend.
    4. Sync back: The handler ensures the server writes have synced back to the collection.
    5. Optimistic state dropped: Once synced, the optimistic state is replaced by the confirmed server state.

    If the handler throws an error during persistence, the optimistic state is automatically rolled back.

  7. Use @tanstack/vue-db with Vue

    main
    @tanstack/vue-db provides Vue-specific composables for interacting with TanStack DB. It allows you to integrate TanStack DB's data management capabilities directly into Vue components using the Composition API. For core database logic and architecture, refer to the main TanStack/db repository.