trpc-patterns
TRPC with Tanstack Query patterns Applies to files matching: *.tsx.
Installation
Follow the repository instructions for this skill and your agent. Review the files and dependencies before adding it to your workflow.
View source ↗GuidesSkill instructions
TRPC with Tanstack Query Integration
Backend Structure
- Main router: trpc/routers/_app.ts - All routers must be registered here
-
Individual routers:
trpc/routers/*.router.ts- Create feature-specific routers (e.g.,chat.router.ts,vote.router.ts) - Database queries: lib/db/queries.ts - All database operations should be defined here
- Database schema: lib/db/schema.ts - Drizzle ORM schema definitions
Frontend Integration
Setup
- Use
useTRPC()hook from@/trpc/react - Import
useMutation,useQuery,useQueryClientfrom@tanstack/react-query
Query Pattern
const trpc = useTRPC();
const { data, isLoading, refetch } = useQuery({
...trpc.router.procedure.queryOptions(),
enabled: !!condition,
});
Mutation Pattern
const trpc = useTRPC();
const queryClient = useQueryClient();
const mutation = useMutation(
trpc.router.procedure.mutationOptions({
onSuccess: () => {
// Invalidate related queries
queryClient.invalidateQueries({
queryKey: trpc.router.relatedQuery.queryKey(),
});
},
})
);
// Usage
await mutation.mutateAsync({ input: data });
Backend Router Pattern
import { createTRPCRouter, protectedProcedure } from '@/trpc/init';
import { z } from 'zod';
export const featureRouter = createTRPCRouter({
queryName: protectedProcedure.query(async ({ ctx }) => {
return await dbFunction({ userId: ctx.user.id });
}),
mutationName: protectedProcedure
.input(z.object({ /* validation schema */ }))
.mutation(async ({ ctx, input }) => {
// Verify permissions/ownership
// Call database function
return await dbFunction(input);
}),
});
Key Principles
- Always use
protectedProcedurefor authenticated routes - Validate inputs with Zod schemas
- Verify user ownership/permissions in mutations
- Use database functions from lib/db/queries.ts
- Invalidate related queries after mutations
- Handle loading and error states in the UI
Examples
Read the usage examples in the source repository.
View source ↗Source history
- Indexed on
- 2026-01-20
- Index updated
- 2026-02-05
- Branch
- main
Index and sync dates describe this directory. View the source repository for code history.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!