Skip to content

Conversation

@sonnes
Copy link

@sonnes sonnes commented Jan 25, 2026

🎯 Changes

Adds a new collection package (@tanstack/indexeddb-db-collection) that persists data to IndexedDB with cross-tab synchronization support. This enables offline-first applications with TanStack DB.

I'm a big fan of TanStack DB, and I wanted to be able to use it with IndexedDB. I got Claude to implement it using the existing local storage collection as a reference.

I'm using this in my side projects, and it has been working great.

Examples:

This is my first time contributing to an open source project, so feedback is welcome.

Key Features

  • Persistent Storage: Data survives page reloads and browser restarts
  • Cross-Tab Sync: Changes propagate to other tabs via BroadcastChannel
  • Structured Data: Objects stored directly without JSON serialization

Usage

import { createCollection } from '@tanstack/db'
import { createIndexedDB, indexedDBCollectionOptions } from '@tanstack/indexeddb-db-collection'

// Create database with all stores defined upfront
const db = await createIndexedDB({
  name: 'my-app',
  version: 1,
  stores: ['todos'],
})

// Create collection using the shared database
const todos = createCollection<Todo>(
  indexedDBCollectionOptions({
    db,
    name: 'todos',
    getKey: (todo) => todo.id,
  })
)

await todos.insert({ id: '1', text: 'Learn TanStack DB', completed: false })

Design Decisions

  1. Out-of-line keys: Object store uses explicit keys (not keyPath) for flexibility
  2. Separate version store: Enables efficient change detection without modifying user data
  3. Array keys for versions: [storeName, key] enables range queries by store
  4. BroadcastChannel over storage events: IndexedDB doesn't fire storage events
  5. Microtask queuing: Async IDB operations complete before sync protocol confirmation
  6. Shared database instance: Collections accept a pre-created IDBDatabase instance via createIndexedDB() to avoid multiple version upgrades when adding object stores

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot
Copy link

changeset-bot bot commented Jan 25, 2026

⚠️ No Changeset found

Latest commit: d58d2d4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Add a new collection package that persists data to IndexedDB with
cross-tab synchronization via BroadcastChannel.

Features:
- Persistent storage that survives page reloads
- Cross-tab sync using BroadcastChannel with version tracking
- Utility functions: clearObjectStore, deleteDatabase, exportData, importData
- Comprehensive test suite (108 tests)
@sonnes sonnes force-pushed the feature/indexeddb-db-collection branch from f20507f to d58d2d4 Compare January 25, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant