TanStack Query
PowerSync integrates with TanStack Query (formerly React Query) through the@powersync/tanstack-react-query package.
npm: @powersync/tanstack-react-query
useQuery and useSuspenseQuery hooks, bringing many of TanStack’s advanced asynchronous state management features to PowerSync web and React Native applications, including:
-
Loading and error states via
useQuery -
React Suspense support:
useSuspenseQueryautomatically converts certain loading states into Suspense signals, triggering Suspense boundaries in parent components. - Caching queries: Queries are cached with a unique key and reused across the app, so subsequent instances of the same query won’t refire unnecessarily.
- Built-in support for pagination
Example Use Case
When navigating to or refreshing a page, you may notice a brief UI “flicker” (10-50ms). Here are a few ways to manage this with TanStack Query:- First load: When a page loads for the first time, use a loading indicator or a Suspense fallback to handle queries. See the examples.
- Subsequent loads: With TanStack’s query caching, subsequent loads of the same page won’t refire queries, which reduces the flicker effect.
-
Block navigation until components are ready: Using
useSuspenseQuery, you can ensure that navigation from page A to page B only happens after the queries for page B have loaded. You can do this by combininguseSuspenseQuerywith the<Suspense />element and React Router’sv7_startTransitionfuture flag, which blocks navigation until all suspending components are ready.
Usage and Examples
For more examples and usage details, see the package README. The full API Reference can be found here:TanStack React Query | PowerSync JavaScript SDK Docs
TanStack DB
The TanStack DB integration lets you use TanStack DB collections backed by PowerSync. In-memory collections stay in sync with PowerSync’s SQLite database for offline-first, reactive data and backend sync.The PowerSync TanStack DB collection is currently in an Alpha release.
Quick start
Install the TanStackDB-PowerSync collection package with a PowerSync SDK. Then define your schema, initialize the PowerSync database, and create a collection. Optionally connect a backend connector for sync.- npm
- yarn
- pnpm
Features
- Blazing fast in-memory queries — Built on differential data flow, live queries update incrementally instead of re-running entire queries, so they stay fast even for complex queries across multiple collections.
- Reactive data flow — Live queries update automatically when underlying data changes, so components re-render only when necessary.
- Optimistic updates — Mutations apply to local state immediately for instant feedback; TanStack DB keeps optimistic state on top of synced data and rolls back automatically if the server request fails.
- Cross-collection queries — Live queries can join across collections, seamlessly querying PowerSync and other TanStack DB collections simultaneously.
- Schema validation and rich types — Use a custom schema (e.g. Zod) to validate mutations and transform SQLite types into rich JavaScript types such as
Date, boolean, and JSON. You can keep SQLite-compatible input for writes and expose transformed types on read, or accept rich input with a separate deserialization schema for synced data. See Create a TanStack DB collection. - Metadata tracking — Attach custom metadata to insert, update, and delete operations. PowerSync persists it and exposes it in
CrudEntrywhen processing uploads in your connector. See Accessing metadata during upload. - Configuration options —
powerSyncCollectionOptionssupports schema and deserialization schemas, optional serializers,onDeserializationError, andsyncBatchSize. See PowerSync Collection (Configuration Options). - TanStackDB transactions — Batch multiple operations with
PowerSyncTransactorandcreateTransaction, control commit timing, and wait for persistence. See Advanced transactions.
Framework support
PowerSync works with all TanStack DB framework adapters:- React (
@tanstack/react-db) - Vue (
@tanstack/vue-db) - Solid (
@tanstack/solid-db) - Svelte (
@tanstack/svelte-db) - Angular (
@tanstack/angular-db)