How ski043/learn-orpc works
Project Type and Purpose
• This is a full-stack Next.js application (using the App Router), bootstrapped with create-next-app.
• It offers both REST-style endpoints under app/api, an RPC layer under app/rpc (backed by your custom lib/orpc* modules), and uses Prisma for database access.
• On the client side it demonstrates React-Query usage (app/react-query-example), custom UI components (components/ui), theming, and an authentication flow (app/middlewares/auth.ts, app/router/auth.ts).
Key File-Structure Insights
• Top-level “app” folder: Next.js 13+ App Router pages + serverless routes.
• app/page.tsx + layout.tsx: primary UI and layout.
• app/api/[[…rest]]/route.ts: catch-all REST endpoints.
• app/rpc/[[…rest]]/route.ts: catch-all RPC endpoints.
• app/middlewares/auth.ts: middleware for route protection.
• app/react-query-example: sample page to illustrate React-Query.
• app/router/*.ts: typed route definitions for client side.
• app/schemas: Zod schemas for auth, todo, user.
• “components” folder: reusable UI atoms/molecules, plus a simple-todo component.
• “lib” folder:
• db.ts – Prisma client.
• orpc.ts / orpc.server.ts – client/server glue for RPC.
• providers.tsx – React context providers (AuthProvider, ThemeProvider).
• utils.ts – misc helpers.
• Prisma: prisma/schema.prisma defines the data model.
• Config/build files: next.config.ts, tsconfig.json, postcss.config.mjs, eslint.config.mjs.
a. Frontend (Browser / Next.js React):
• Pages & Layout (app/page.tsx, layout.tsx)
• UI Components (components/ui/*, components/todo)
• React Query & ORPC Client (lib/orpc.ts, app/react-query-example)
• Providers (AuthProvider, ThemeProvider)
b. Next.js Server (Vercel lambdas or Node server):
• Routing Layer (App Router)
• REST API Endpoints (app/api/…)
• RPC Endpoints (app/rpc/…)
• Middleware (app/middlewares/auth.ts)
• Schema validation (app/schemas via Zod)
c. Database Layer:
• Prisma Client (lib/db.ts)
• Underlying database (PostgreSQL, MySQL, etc.)
d. External Services (if any):
• Authentication provider (JWT or session store)
• Monitoring/instrumentation (instrumentation.ts)
Relationships & Data Flow
• Browser sends SSR/SSG page requests to Next.js Server.
• For mutations/queries:
• React Query hooks → REST API routes (app/api)
• ORPC Client → RPC routes (app/rpc)
• Server middleware authenticates requests, validates payloads (Zod schemas), then calls Prisma client.
• Prisma executes SQL against the database and returns results.
• Server responds with JSON or rendered HTML, back to the browser.
Architectural Patterns & Principles
• Feature-oriented folder structure under “app” (App Router).
• Separation of concerns: UI components vs. data layer vs. routing.
• Typed contracts: Zod schemas + TypeScript for end-to-end type safety.
• Hybrid data-fetching strategies: SSR pages, REST, RPC, React-Query caching.
• Infrastructure as code: Next.js configuration, Prisma schema.