tanstack/db/custom-adapter
Claude Code and agent skill by tanstack/db
- community
Checking that the SKILL.md file is still in the repository.
Building custom collection adapters for new backends. SyncConfig interface: sync function receiving begin, write, commit, markReady, fail, reportError, reportHealthy, truncate, metadata primitives and returning cleanup, loadSubset, and optional unloadSubset handlers. ChangeMessage format (insert, update, delete). On-demand LoadSubsetOptions (where, orderBy, limit, offset, cursor). Expression parsing: parseWhereExpression, parseOrderByExpression, extractSimpleComparisons, parseLoadSubsetOptions. Collection options creator pattern. rowUpdateMode (partial vs full). Sync run, subscription lifecycle, and cleanup functions. Persisted sync metadata API (metadata.row and metadata.collection) for storing per-row and per-collection adapter state.
Install
npx vskill@latest install tanstack/db/custom-adapterSKILL.md
The same SKILL.md was published earlier as tanstack/db/custom-adapter.
This skill builds on db-core and db-core/collection-setup. Read those first.
Custom Adapter Authoring
Each call to an adapter's sync() function starts a sync run. The run owns the callbacks and resources installed by that call until its returned cleanup ends them. A sync run may make several backend requests or open a longer-lived provider session, so do not use “request” or “session” as a synonym for the run.
Setup
Core Patterns
ChangeMessage format
On-demand sync with loadSubset
sync() returns the handlers in a SyncConfigRes object. loadSubset() must write fetched rows through begin() → write() → commit() and resolve void (or return true for an immediate synchronous result); it does not return the fetched rows. parseLoadSubsetOptions() returns only filters, sorts, and limit. Read offset and cursor from the original options. cursor contains query expressions (whereFrom and whereCurrent), not an opaque backend cursor; translate or combine those expressions for your API. Return unloadSubset only when loadSubset creates an ongoing resource, such as a per-subset server subscription, that must be released. A Collection subscription installs its logical owner before calling loadSubset. A synchronous throw fails the load but keeps that owner; when the owner is released, core calls unloadSubset for the failed load too. Make unloadSubset safe after partial setup. A must-refetch can call loadSubset again with the same options. Replay first detaches and releases the old acquisition lease, then starts a fresh acquisition if the logical demand and replay are still current. Core releases the current acquisition when the demand ends.
Managing optimistic state duration
Mutation handlers must not resolve until server changes have synced back to the collection. Five strategies:
- Refetch (simplest):
await collection.utils.refetch() - Transaction ID: await a utility that observes the mutation's transaction ID in the sync stream, such as Electric's
await collection.utils.awaitTxId(txid) - ID-based tracking: await specific record ID appearing in sync stream
- Version/timestamp: wait until sync stream catches up to mutation time
- Provider method:
await backend.waitForPendingWrites()
Persisted sync metadata
The metadata API on the sync config allows adapters to store per-row and per-collection metadata that persists across sync transactions. This is useful for tracking resume tokens, cursors, LSNs, or other adapter-specific state.
The metadata object is available on the sync config argument alongside begin, write, and commit. Core supplies it at runtime, but its public type is optional, so strict TypeScript code must guard it or assert its presence. Without persistence the metadata is in-memory only and does not survive reloads. With persistence, it is durable across sessions.
Row metadata writes are tied to the current transaction. Deleting a row also deletes its metadata. An insert sets metadata from message.metadata. A metadata-less insert deletes stale metadata unless metadata.row.set() already queued an explicit value for that key in the same transaction; that queued value wins.
Collection metadata writes staged before truncate() are preserved and commit atomically with the truncate transaction.
Typical usage — resume token:
Expression parsing for predicate push-down
Common Mistakes
CRITICAL Defining loadSubset beside sync()
Wrong:
Correct: return { loadSubset, cleanup } from sync() and apply loaded rows with the sync transaction primitives, as shown above. Add unloadSubset when each loaded subset owns a resource that must be released.
CRITICAL Not calling markReady() in sync implementation
Wrong:
Correct:
markReady() transitions the collection to "ready" status. Without it, live queries never resolve and useLiveSuspenseQuery hangs forever in Suspense.
If initial sync fails before it produces a usable snapshot and you stop retrying, call fail(error) instead. The status becomes error, readiness waits reject with the supplied cause, and dependent live queries move to error. A later markReady() in the same sync run does nothing; only a new sync run can reach ready. Use collection.reload() to start a new sync run.
After the first markReady(), the rows stay. Report a sync problem with reportError(error) while you keep trying; it sets the node's failureReason. Call reportHealthy() when sync works again; it does nothing when nothing is wrong. When you stop trying, call fail(error): after readiness the node shows an error, and only reload() clears it. reportError() and reportHealthy() throw before the first markReady(). Apps read these reports from collection.health and from each live query's health tree. While a retry is in progress before readiness, report nothing.
A throwing sync function crashes the Collection (status: 'crashed'). Reserve that for broken local state, not for network failures.
Source: docs/guides/collection-options-creator.md
HIGH Race condition: subscribing after initial fetch
Wrong:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 93 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingpackages/db/skills/db-core/custom-adapter/SKILL.md
Popularity
- Repository stars
- 3,880
- Repository forks
- 258
- 7-day trend score
- 41
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/tanstack/db/custom-adapter)