tRPC Tanstack DB Collection is a powerful integration that combines tRPC with Tanstack DB to provide a seamless, type-safe, real-time data synchronization solution for modern web applications.
- Full Type Safety: Leverages tRPC's powerful inference system for end-to-end type safety
- Real-time Synchronization: Server-Sent Events (SSE) for instant updates across clients
- Optimistic UI Updates: Immediate UI feedback with background synchronization
- Full Backend Control: Unlike solutions like ElectricSQL, you maintain complete ownership of your backend
- Easy Integration: Simple API that works with your existing tRPC routers
This package provides a bridge between your tRPC routers and Tanstack DB collections. It handles:
- Initial Data Loading: Fetches initial data from your tRPC
listprocedure - Real-time Updates: Subscribes to server events via SSE for instant synchronization
- CRUD Operations: Provides optimized create, update, and delete operations
- Conflict Resolution: Handles event deduplication and race conditions
npm install trpc-db-collection @tanstack/react-db @tanstack/store @trpc/server
# or
pnpm add trpc-db-collection @tanstack/react-db @tanstack/store @trpc/server
# or
yarn add trpc-db-collection @tanstack/react-db @tanstack/store @trpc/serverFirst, create a tRPC router that follows the required structure:
// src/lib/trpc/todos.ts
import { router, authedProcedure } from "@/lib/trpc";
import { z } from "zod";
import { eq } from "drizzle-orm";
import {
todosTable,
selectTodoSchema,
createTodoSchema,
updateTodoSchema,
} from "@/db/schema";
import { TrpcSync } from "trpc-db-collection";
type Todo = z.infer<typeof selectTodoSchema>;
const todoRouterSync = new TrpcSync<Todo>();
export const todosRouter = router({
list: authedProcedure.query(async ({ ctx }) => {
return ctx.db
.select()
.from(todosTable)
.where(eq(todosTable.userId, ctx.session.user.id));
}),
create: authedProcedure
.input(createTodoSchema)
.mutation(async ({ ctx, input }) => {
const [newTodo] = await ctx.db
.insert(todosTable)
.values({ ...input, userId: ctx.session.user.id })
.returning();
const eventId = await todoRouterSync.registerEvent({
currentUserId: ctx.session.user.id,
event: { action: "insert", data: newTodo },
});
return { item: newTodo, eventId };
}),
update: authedProcedure
.input(z.object({ id: z.number(), data: updateTodoSchema }))
.mutation(async ({ ctx, input }) => {
const [updatedTodo] = await ctx.db
.update(todosTable)
.set(input.data)
.where(eq(todosTable.id, input.id))
.returning();
const eventId = await todoRouterSync.registerEvent({
currentUserId: ctx.session.user.id,
event: { action: "update", data: updatedTodo },
});
return { item: updatedTodo, eventId };
}),
delete: authedProcedure
.input(z.object({ id: z.number() }))
.mutation(async ({ ctx, input }) => {
const [deletedTodo] = await ctx.db
.delete(todosTable)
.where(eq(todosTable.id, input.id))
.returning();
const eventId = await todoRouterSync.registerEvent({
currentUserId: ctx.session.user.id,
event: { action: "delete", data: deletedTodo },
});
return { item: deletedTodo, eventId };
}),
listen: authedProcedure.subscription(({ ctx }) => {
return todoRouterSync.eventsSubscription({
userId: ctx.session.user.id,
signal: undefined,
lastEventId: null,
});
}),
});// src/lib/collections.ts
import { createCollection } from "@tanstack/react-db";
import { trpc } from "@/lib/trpc";
import { trpcCollectionOptions } from "trpc-db-collection";
export const todosCollection = createCollection(
trpcCollectionOptions({
name: "todos",
trpcRouter: trpc.todos,
rowUpdateMode: "partial", // or 'full'
}),
);// src/routes/todos.tsx
import { useLiveQuery } from '@tanstack/react-db'
import { todosCollection } from '@/lib/collections'
function TodosPage() {
const { data: todos } = useLiveQuery((q) =>
q.from({ todosCollection }).orderBy('createdAt', 'desc')
)
return (
<div>
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} />
))}
</div>
)
}
function TodoItem({ todo }) {
return (
<div>
<input
type="checkbox"
checked={todo.completed}
onChange={() =>
todosCollection.update(todo.id, { completed: !todo.completed })}
/>
{todo.title}
</div>
)
}The library supports local storage persistence to provide offline capabilities and faster initial load times. When enabled, your collection data will be automatically saved to and restored from localStorage, with a Stale while revalidate mechanism: the up to date data will be fetched in background and replace your cache when up.
- Offline Support: Your application can continue to function when the network is unavailable
- Faster Load Times: Data is immediately available from localStorage while the initial sync happens in the background
To enable local storage persistence, you need to configure the trpcCollectionOptions with the localStorage option:
import { trpcCollectionOptions } from "trpc-db-collection";
import { createCollection } from "@tanstack/react-db";
import { trpc } from "/lib/trpc";
const todosCollection = createCollection(
trpcCollectionOptions({
name: "todos",
trpcRouter: trpc.todos,
rowUpdateMode: "partial", // or 'full'
localStorage: true, // Enable local storage persistence
}),
);By default, the library uses JSON for serialization when storing data in localStorage. You can customize this by providing your own serializer, such as superjson:
import { trpcCollectionOptions } from "trpc-db-collection";
import { jsonSerializer } from "trpc-db-collection";
import superjson from "superjson";
// Use default JSON serializer
const collectionConfig = trpcCollectionOptions({
name: "todos",
trpcRouter: todoRouter,
// serializer is optional, defaults to jsonSerializer
});
// Or use superjson for better type support
const collectionConfigWithSuperjson = trpcCollectionOptions({
name: "todos",
trpcRouter: todoRouter,
serializer: superjson,
});The trpcCollectionOptions function accepts:
interface TrpcCollectionConfig<TItem extends TrpcItem> {
name: string;
trpcRouter: RequiredTrpcRouter<TItem>;
rowUpdateMode?: "partial" | "full";
logger?: {
enabled?: boolean;
level?: "debug" | "info" | "error" | "none";
};
// Plus all standard Tanstack DB CollectionConfig options
}The package handles real-time synchronization through:
- Server-Sent Events (SSE): Efficient unidirectional updates from server to clients
- Event Deduplication: Prevents duplicate processing of the same event
- Race Condition Handling: Buffers events during initial sync to maintain consistency
- Optimistic Updates: Immediate UI feedback while waiting for server confirmation
Check out the example project for a complete working implementation. It demonstrates:
- Database schema with Drizzle ORM
- Complete tRPC router implementation
- Collection setup and usage
- Route integration with Tanstack Router
- Authentication with BetterAuth
If you're migrating from ElectricSQL, this package provides a similar real-time experience but with:
- Full backend control (no vendor lock-in)
- Complete type safety (thanks to tRPC)
- Simpler architecture (no separate sync service)
If you're using traditional tRPC queries and mutations:
- Replace
trpc.todos.list.useQuery()withuseLiveQueryfrom Tanstack DB - Replace direct mutation calls with collection operations (
collection.insert(),collection.update(), etc.) - Add the
listenprocedure to your router for real-time updates
For optimal performance with multiple collections, use HTTP/2 to avoid browser connection limits. The example project includes Caddy configuration for this.
The package automatically handles connection cleanup and reconnection logic.
Contributions are welcome! Please open issues for bugs or feature requests, and submit pull requests for improvements.
This project is licensed under the MIT License - see the LICENSE file for details.