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
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 { 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 { 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: 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:
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.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.