How piyushgarg-dev/trpc-monorepo works
Purpose and workspace shape
This is a pnpm/Turborepo TypeScript monorepo for a web client, a standalone API process, and shared backend packages. The README is generic starter text; the tree shows the actual system: Next.js UI → typed RPC → service/database layers, with Google OAuth integration. Workspace orchestration is rooted in package.json, pnpm-workspace.yaml, and turbo.json.
Runtime entry points
• The web application is a Next.js App Router app, entered through apps/web/app/layout.tsx and apps/web/app/page.tsx; global client wiring is in apps/web/providers/global.tsx.
• The API is a Node/TypeScript server with process/bootstrap boundaries in apps/api/src/index.ts and apps/api/src/server.ts.
• Each runtime has explicit environment handling: apps/web/env.js and apps/api/src/env.ts.
Web presentation and RPC consumer
The Next.js app owns rendering, styles, and browser-side RPC connectivity. Its tRPC adapters separate browser and server execution paths: apps/web/trpc/client.ts, apps/web/trpc/create-client.ts, and apps/web/trpc/server.ts. UI primitives under apps/web/components/ui are a reusable presentation layer, rather than the domain boundary.
Primary client flow: page/layout render → global provider establishes application dependencies → tRPC client/server adapter invokes the shared procedure contract.
Typed API contract and request pipeline
packages/trpc is the central transport boundary shared by the web and API runtimes. Server initialization and procedure construction live in packages/trpc/server/trpc.ts and packages/trpc/server/index.ts; request-scoped dependencies are assembled in packages/trpc/server/context.ts. The route tree currently exposes auth and health endpoints at packages/trpc/server/routes/auth/route.ts and packages/trpc/server/routes/health/route.ts.
The API app should act as host/adapter: it starts HTTP serving and mounts or delegates requests to the shared tRPC server, rather than owning domain logic.
Domain services and external identity
Business logic is intended to sit below RPC in packages/services. User-facing domain operations and models are grouped in packages/services/user/index.ts and packages/services/user/model.ts. Google OAuth is isolated behind packages/services/clients/google-oauth.ts, creating a clear external-service boundary. Service configuration is centralized in packages/services/env.ts.
Expected auth flow: auth RPC route → service layer → Google OAuth client and/or user persistence → typed response back through the tRPC client.
Persistence layer
packages/database owns database initialization, schemas, and migrations. The principal data contract is packages/database/schema.ts, with user persistence modeled in packages/database/models/user.ts and exported connection/access code in packages/database/index.ts. Drizzle migration history begins at packages/database/drizzle/0000_dusty_morg.sql; Drizzle configuration is packages/database/drizzle.config.ts.
This creates a boundary in which services consume database models/access APIs, while RPC routes and UI should not directly manage storage details.
Infrastructure and shared operational concerns
The system runs TypeScript across Node and Next.js, uses Drizzle for relational persistence, and includes Docker Compose infrastructure in docker-compose.yml (likely local backing services; exact service definitions are not visible). Shared structured logging is provided by packages/logger/index.ts. Build/task execution is Turbo-based; optional Vercel remote caching is described in the README, but deployment topology is not defined by the supplied tree.
Main entry points: server/index.ts tRPC server · src/index.ts API host · user/index.ts User service · layout.tsx Next.js app
Components 18
Workspace
- Turbo workspace · pnpm/Turbo coordinates monorepo tasks.
turbo.json - Local infrastructure · Local backing services are defined here.
docker-compose.yml
Web runtime
- Next.js app
apps/web/app/layout.tsx - Global providers
apps/web/providers/global.tsx - Web tRPC adapters · Separates browser and server RPC execution.
apps/web/trpc/client.ts - Web environment
apps/web/env.js
API runtime
- API host · Bootstraps HTTP serving and delegates to shared RPC.
apps/api/src/index.ts - API environment
apps/api/src/env.ts
Shared RPC
- tRPC server · Shared typed transport boundary.
packages/trpc/server/index.ts - Request context · Assembles request-scoped dependencies.
packages/trpc/server/context.ts - Auth procedures
packages/trpc/server/routes/auth/route.ts - Health procedures
packages/trpc/server/routes/health/route.ts
Domain and persistence
- User service · User operations and models.
packages/services/user/index.ts - Google OAuth · External identity provider adapter.
packages/services/clients/google-oauth.ts - Database access · Exports database connection and access APIs.
packages/database/index.ts - User persistence model
packages/database/models/user.ts - Database schema
packages/database/schema.ts - Structured logger
packages/logger/index.ts
Connections 18
- Turbo workspace → Next.js app: builds and runs
- Turbo workspace → API host: builds and runs
- Next.js app → Global providers: initializes
- Global providers → Web tRPC adapters: provides RPC dependencies
- Web tRPC adapters → tRPC server: typed procedure calls
- Web environment → Next.js app: configures
- API environment → API host: configures
- API host → tRPC server: hosts and delegates to
- tRPC server → Request context: creates per request
- tRPC server → Auth procedures: routes auth requests
- tRPC server → Health procedures: routes health requests
- Auth procedures → User service: invokes
- User service → Google OAuth: authenticates with
- User service → Database access: persists users
- Database access → User persistence model: accesses
- User persistence model → Database schema: maps to
- Local infrastructure → Database access: provides local backing service
- API host → Structured logger: emits logs