db-core/collection-setup

Creating typed collections with createCollection. Adapter selection: queryCollectionOptions (REST/TanStack Query), electricCollectionOptions (ElectricSQL real-time sync), powerSyncCollectionOptions (PowerSync SQLite), rxdbCollectionOptions (RxDB), trailBaseCollectionOptions (TrailBase), localOnlyCollectionOptions, localStorageCollectionOptions. CollectionConfig options: getKey, schema, sync, gcTime, autoIndex (default off), defaultIndexType, syncMode (eager/on-demand, plus progressive for Electric). StandardSchema validation with Zod/Valibot/ArkType. Collection lifecycle (idle/loading/ready/error). Adapter-specific sync patterns including Electric txid tracking, Query direct writes, Query initial data and scoped factories, and PowerSync query-driven sync with onLoad/onLoadSubset hooks.

Install
npx skills add 'https://github.com/TanStack/db/tree/main/packages/db/skills/db-core/collection-setup'
Download bundle ↓
main · 09776a8Scanned 2026-09-17

Contributors

GitHub-linked commit authors for this SKILL.md at the saved revision. Co-authors and history before file renames are not included.

File history ↗
View on GitHub
← Back to SKILL.md

RxDB Adapter Reference

Install

pnpm add @tanstack/rxdb-db-collection rxdb @tanstack/react-db

Required Config

import { createCollection } from '@tanstack/react-db'
import { rxdbCollectionOptions } from '@tanstack/rxdb-db-collection'

const todosCollection = createCollection(
  rxdbCollectionOptions({
    rxCollection: db.todos,
  }),
)
  • rxCollection -- the underlying RxDB RxCollection instance

Optional Config (with defaults)

OptionDefaultDescription
id(none)Unique collection identifier
schema(none)StandardSchema validator (RxDB has its own validation; this adds TanStack DB-side validation)
startSyncfalseStart ingesting RxDB data immediately
syncBatchSize1000Max documents per batch during initial sync from RxDB; only affects initial load, not live updates

The adapter owns onInsert, onUpdate, and onDelete so writes persist to RxDB. Those handlers cannot be overridden in RxDBCollectionConfig.

Key Behavior: String Keys

RxDB primary keys are always strings. The getKey function is derived from the RxDB schema's primaryKey field automatically. All key values will be strings.

RxDB Setup (prerequisite)

import { createRxDatabase } from 'rxdb/plugins/core'
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage'

const db = await createRxDatabase({
  name: 'my-app',
  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'],
    },
  },
})

Backend Sync (optional, RxDB-managed)

Replication is configured directly on the RxDB collection, independent of TanStack DB. Changes from replication flow into the TanStack DB collection via RxDB's change stream automatically.

import { replicateRxCollection } from 'rxdb/plugins/replication'

const replicationState = replicateRxCollection({
  collection: db.todos,
  pull: { handler: myPullHandler },
  push: { handler: myPushHandler },
})

Data Flow

  • Writes via todosCollection.insert/update/delete persist to RxDB
  • Direct RxDB writes (or replication changes) flow into the TanStack collection via change streams
  • Initial sync loads data in batches of syncBatchSize
  • Ongoing updates stream one by one via RxDB's change feed

Indexes

RxDB schema indexes do not affect TanStack DB query performance (queries run in-memory). Indexes may still matter if you query RxDB directly, use filtered replication, or selectively load subsets.

Complete Example

import { createRxDatabase } from 'rxdb/plugins/core'
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage'
import { createCollection, safeRandomUUID } from '@tanstack/react-db'
import { rxdbCollectionOptions } from '@tanstack/rxdb-db-collection'
import { z } from 'zod'

type Todo = { id: string; text: string; completed: boolean }

const db = await createRxDatabase({
  name: 'my-todos',
  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'],
    },
  },
})

const todoSchema = z.object({
  id: z.string(),
  text: z.string().min(1),
  completed: z.boolean(),
})

const todosCollection = createCollection(
  rxdbCollectionOptions({
    rxCollection: db.todos,
    schema: todoSchema,
    startSync: true,
    syncBatchSize: 500,
  }),
)

// Usage
todosCollection.insert({
  id: safeRandomUUID(),
  text: 'Buy milk',
  completed: false,
})
todosCollection.update('some-id', (draft) => {
  draft.completed = true
})
todosCollection.delete('some-id')
Referenced from SKILL.md