TanStack DB
TanStack DB collections that load through a query option and write through the repositories, next to the data hooks.
better-supabase/tanstack-db builds TanStack DB collections that load
through one of your query options and write through
the repositories, and tanstackDbEngine keeps the
data hooks in step with them. TanStack DB is
not a dependency: the options are typed by shape.
Through a query option
import { createCollection } from "@tanstack/react-db";
import { queryCollectionOptions } from "@tanstack/query-db-collection";
import { collectionOptions } from "better-supabase/tanstack-db";
import { betterSupabase } from "./supabase";
import { db, queries, queryClient } from "./client";
export const customers = createCollection(
queryCollectionOptions(
collectionOptions(betterSupabase, db, "customers", {
query: queries.customers.list({ where: { status: "active" } }),
queryClient,
}),
),
);collectionOptions(betterSupabase, db, table, options) returns the options
queryCollectionOptions() reads. The collection loads its rows with the
query option you pass and keys them by the table's
primary key (a JSON array for a composite key).
Writes
customers.insert(row), customers.update(id, draft) and
customers.delete(id) apply optimistically, then run create, update and
delete on the table's repository, so RLS, hooks, events and plugins apply.
A failed write throws a DbException, and TanStack DB rolls the optimistic
change back. After a write the collection refetches its query. To refresh
the other queries that read the table, call
invalidateOnMutation(betterSupabase, queryClient) from
better-supabase/query once at startup.
With the data hooks
tanstackDbEngine runs the generated hooks next to the collections: a change
to a collection (an optimistic write, a refetch) reads every hook on that
table again, and a hook's write refetches the table's collection, so live
queries and hooks show the same rows.
import { createClient } from "better-supabase/client";
import { queryEngine } from "better-supabase/query";
import { realtimeChanges } from "better-supabase/realtime";
import { tanstackDbEngine } from "better-supabase/tanstack-db";
import { customers } from "./collections";
export const bsb = createClient(betterSupabase, {
engine: tanstackDbEngine({
collections: () => ({ customers }),
cache: queryEngine(queryClient, {
live: realtimeChanges(betterSupabase, supabase),
}),
}),
});collections is a function here because the collections read bsb.db; the
engine calls it when a hook first follows a read. cache decides where the
hooks keep their reads, in memory by default.
Live queries
Join and filter collections with TanStack DB's useLiveQuery. To refresh
them when another client changes a table, pass
live: realtimeChanges(betterSupabase, supabase) from
better-supabase/realtime to the engine's cache, as above, or invalidate
the tables from your own subscription with
invalidateTables(queryClient, ["customers"]).
For Realtime, add each table to the publication in a migration:
alter publication supabase_realtime add table public.customers;Delete events carry the replica identity of the deleted row, so the table
needs a primary key or replica identity full.
doctor reports a published table whose delete
events carry no keys as BS306.
Last updated on