File: rxdb-collection.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
Collections
Frameworks
Community
API Reference
Framework
React
Version
Latest
Menu
Getting Started
Guides
Collections
Frameworks
Community
API Reference
On this page
Copy Markdown
RxDB Collection
===============
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:
1. Installation
----------------
Install the RXDB collection packages along with your preferred framework integration.
bash
npm install @tanstack/rxdb-db-collection rxdb @tanstack/react-db
npm install @tanstack/rxdb-db-collection rxdb @tanstack/react-db
### 2. Create an RxDatabase and RxCollection
ts
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 { 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'],
},
},
})
### 3. (optional) sync with a backend
ts
import { replicateRxCollection } from 'rxdb/plugins/replication'
const replicationState = replicateRxCollection({
collection: db.todos,
pull: { handler: myPullHandler },
push: { handler: myPushHandler },
})
import { replicateRxCollection } from 'rxdb/plugins/replication'
const replicationState = replicateRxCollection({
collection: db.todos,
pull: { handler: myPullHandler },
push: { handler: myPushHandler },
})
### 4. Wrap the RxDB collection with TanStack DB
ts
import { createCollection } from '@tanstack/react-db'
import { rxdbCollectionOptions } from '@tanstack/rxdb-db-collection'
const todosCollection = createCollection(
rxdbCollectionOptions({
rxCollection: myDatabase.todos,
startSync: true, // start ingesting RxDB data immediately
})
)
import { createCollection } from '@tanstack/react-db'
import { rxdbCollectionOptions } from '@tanstack/rxdb-db-collection'
const todosCollection = createCollection(
rxdbCollectionOptions({
rxCollection: myDatabase.todos,
startSync: true, // start ingesting RxDB data immediately
})
)
Now todosCollection is a reactive TanStack DB collection driven by RxDB:
Configuration Options
---------------------
The rxdbCollectionOptions function accepts the following options:
Syncing with Backends
---------------------
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.
