agentleFS
LooseWireDevLooseWireDev/mealforge/.agents/skills/trpc-drizzle-patterns/SKILL.md

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.

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
  1. Very specific:

    Every input is a Zod schema: No unvalidated input.

    line 43
  2. Very specific:

    Never import from apps/api directly in web/mobile code, and never duplicate API types by hand.

    line 63
  3. Distinctive:

    Do not mock the database or internal modules.

    line 68
  4. Very specific:

    Test names describe behavior: "returns UNAUTHORIZED when no session".

    line 69

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 — throws TRPCError({ code: 'UNAUTHORIZED' }) when there is no session, and narrows ctx.session to 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 TRPCError with 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 appRouter by the feature generator via the // forge:feature-imports / // forge:feature-routers anchors in trpc.ts. Never register by hand; never delete the anchors.

Drizzle

  • Tables live in apps/api/src/db/schema.ts; auth tables are separate in apps/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 in schema.ts and stay consistent.
  • Migrations: drizzle.config.ts at apps/api/ is already wired to the right dialect; generate migrations with drizzle-kit into src/db/migrations/.
  • Better Auth uses drizzleAdapter(db, { provider: 'pg' | 'sqlite' }) in src/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".

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.