trpc-drizzle-patterns
Skill1 starsChanged 4 months agoMITView on GitHub
Canonical patterns for the Hono + tRPC + Drizzle + Better Auth backend — procedures, routers, context, schema, and how types flow to clients. Use when writing or reviewing any code in apps/api (TypeScript backend) or consuming the API from web/mobile.
In this repository4 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- feature-developmentSkill
- tanstack-router-patternsSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 43
Every input is a Zod schema: No unvalidated input.
- Very specific: line 63
Never import from apps/api directly in web/mobile code, and never duplicate API types by hand.
- Distinctive: line 68
Do not mock the database or internal modules.
- Very specific: line 69
Test names describe behavior: "returns UNAUTHORIZED when no session".
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
tRPC + Drizzle Patterns (Hono backend)
Context and procedures
apps/api/src/trpc.ts is the hub. It defines:
export interface Context {
req: Request;
session: Session | null; // Session = typeof auth.$Infer.Session (Better Auth)
}createContext resolves the session once per request via auth.api.getSession({ headers: req.headers }).
publicProcedure— no auth requirement.protectedProcedure— throwsTRPCError({ code: 'UNAUTHORIZED' })when there is no session, and narrowsctx.sessionto non-null for everything chained after it. Use it for anything user-specific; never re-check the session manually inside a protected handler.
Feature routers
Each feature owns a router in apps/api/src/features/<name>/router.ts:
import { z } from 'zod';
import { router, publicProcedure, protectedProcedure } from '../../trpc';
import { listThings } from './service';
export const thingRouter = router({
list: publicProcedure.query(() => listThings()),
create: protectedProcedure
.input(z.object({ name: z.string().min(1) }))
.mutation(({ ctx, input }) => createThing(ctx.session.user.id, input)),
});Rules:
- Every input is a Zod schema. No unvalidated
input. - Routers stay thin — business logic lives in the feature's
service.ts, with explicit return types on every exported function. - Services throw
TRPCErrorwith the right code (NOT_FOUND,FORBIDDEN, ...). No try/catch in services or routers — errors propagate to the tRPC error handler. - Feature routers are registered in
appRouterby the feature generator via the// forge:feature-imports/// forge:feature-routersanchors intrpc.ts. Never register by hand; never delete the anchors.
Drizzle
- Tables live in
apps/api/src/db/schema.ts; auth tables are separate inapps/api/src/auth/authSchema.ts(owned by Better Auth — don't hand-edit). - The project is either postgres (
drizzle-orm/pg-core:pgTable,serial, ...) or sqlite (drizzle-orm/sqlite-core:sqliteTable,integer, ...) — check the existing imports inschema.tsand stay consistent. - Migrations:
drizzle.config.tsatapps/api/is already wired to the right dialect; generate migrations with drizzle-kit intosrc/db/migrations/. - Better Auth uses
drizzleAdapter(db, { provider: 'pg' | 'sqlite' })insrc/auth/auth.ts— the provider must match the database.
How types reach clients (the type bridge)
AppRouter is exported from trpc.ts and re-exported by packages/shared/src/types/trpc.ts. Clients import it only as:
import type { AppRouter } from '@<project>/shared/types';Never import from apps/api directly in web/mobile code, and never duplicate API types by hand. On the web side, apps/web/src/lib/trpc.ts exposes trpc = createTRPCReact<AppRouter>(); wrap the app in <trpc.Provider> (client from createTRPCClient()) when adding the first trpc.<feature>.<proc>.useQuery() call.
Testing
- Vitest, tests next to the code (
service.test.ts). Property-based tests with fast-check for pure data transformations. - Do not mock the database or internal modules; only external network calls. For db-touching tests, use a real sqlite database or a test transaction.
- Test names describe behavior:
"returns UNAUTHORIZED when no session".
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

