RxDB collections provide seamless integration between TanStack DB and RxDB, enabling automatic synchronization between your in-memory TanStack DB collections and RxDB's local-first database. Giving you offline-ready persistence, and powerful sync capabilities with a wide range of backends.
The @tanstack/rxdb-db-collection package allows you to create collections that:
Install the RxDB collection packages along with your preferred framework integration.
npm install @tanstack/rxdb-db-collection rxdb @tanstack/react-db
import { createRxDatabase, addRxPlugin } from 'rxdb/plugins/core'
/**
* Here we use the localStorage based storage for RxDB.
* RxDB has a wide range of storages based on Dexie.js, IndexedDB, SQLite, and more.
*/
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage'
// add json-schema validation (optional)
import { wrappedValidateAjvStorage } from 'rxdb/plugins/validate-ajv';
// Enable dev mode (optional, recommended during development)
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode'
addRxPlugin(RxDBDevModePlugin)
type Todo = { id: string; text: string; completed: boolean }
const db = await createRxDatabase({
name: 'my-todos',
storage: wrappedValidateAjvStorage({
storage: getRxStorageLocalstorage()
})
})
await db.addCollections({
todos: {
schema: {
title: 'todos',
version: 0,
type: 'object',
primaryKey: 'id',
properties: {
id: { type: 'string', maxLength: 100 },
text: { type: 'string' },
completed: { type: 'boolean' },
},
required: ['id', 'text', 'completed'],
},
},
})
import { replicateRxCollection } from 'rxdb/plugins/replication'
const replicationState = replicateRxCollection({
collection: db.todos,
pull: { handler: myPullHandler },
push: { handler: myPushHandler },
})
import { createCollection } from '@tanstack/react-db'
import { rxdbCollectionOptions } from '@tanstack/rxdb-db-collection'
const todosCollection = createCollection(
rxdbCollectionOptions({
rxCollection: db.todos,
startSync: true, // start ingesting RxDB data immediately
})
)
Now todosCollection is a reactive TanStack DB collection driven by RxDB:
The rxdbCollectionOptions function accepts the following options:
Replication and sync in RxDB run independently of TanStack DB. You set up replication directly on your RxCollection using RxDB's replication plugins (for CouchDB, GraphQL, WebRTC, REST APIs, etc.).
When replication runs, it pulls and pushes changes to the backend and applies them to the RxDB collection. Since the TanStack DB integration subscribes to the RxDB change stream, any changes applied by replication are automatically reflected in your TanStack DB collection.
This separation of concerns means you configure replication entirely in RxDB, and TanStack DB automatically benefits: your TanStack collections always stay up to date with whatever sync strategy you choose.
Usually not, at least for TanStack DB queries themselves. TanStack DB queries run entirely in memory, so RxDB schema indexes do not affect the performance of TanStack DB's live queries. However, RxDB indexes may still be important if:
Yes, intentionally. RxDB stores data durably on disk. TanStack DB stores data in memory for fast queries and reactivity. This duplication enables high-performance UI queries while retaining local-first persistence and sync.
Synchronization follows a clear separation of responsibilities between RxDB and TanStack DB.
RxDB is responsible for persistence and networking. It stores data durably using a local storage engine (IndexedDB, SQLite, etc.) and handles all replication logic. Replication is configured directly on the RxDB collection and runs independently of TanStack DB. RxDB pulls changes from the backend, applies them locally, resolves conflicts, and pushes local changes back to the backend.
TanStack DB sits on top as an in-memory, reactive query layer. It does not talk to the backend directly and does not participate in replication. Instead, it mirrors the current state of the RxDB collection in memory and provides fast live queries and optimistic mutations for the UI.
This design intentionally forms two independent loops: