# piyushgarg-dev/trpc-monorepo architecture

> Architecture diagram of the GitHub repository piyushgarg-dev/trpc-monorepo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/piyushgarg-dev/trpc-monorepo
- Repository: https://github.com/piyushgarg-dev/trpc-monorepo

## Overview

#### 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: [`packages/trpc/server/index.ts`](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/index.ts) (tRPC server), [`apps/api/src/index.ts`](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/api/src/index.ts) (API host), [`packages/services/user/index.ts`](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/services/user/index.ts) (User service), [`apps/web/app/layout.tsx`](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/app/layout.tsx) (Next.js app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workspace["Workspace"]
  node_turbo["Turbo workspace<br/>orchestration<br/>[turbo.json]"]
  node_docker["Local infrastructure<br/>compose<br/>[docker-compose.yml]"]
end

subgraph group_web["Web runtime"]
  node_web_app["Next.js app<br/>app-router<br/>[layout.tsx]"]
  node_web_providers["Global providers<br/>client-wiring<br/>[global.tsx]"]
  node_web_trpc["Web tRPC adapters<br/>rpc-client<br/>[client.ts]"]
  node_web_env["Web environment<br/>runtime-env<br/>[env.js]"]
end

subgraph group_api["API runtime"]
  node_api_host["API host<br/>node-server<br/>[index.ts]"]
  node_api_env["API environment<br/>runtime-env<br/>[env.ts]"]
end

subgraph group_rpc["Shared RPC"]
  node_trpc_server["tRPC server<br/>transport-core<br/>[index.ts]"]
  node_trpc_context["Request context<br/>request-scope<br/>[context.ts]"]
  node_auth_route["Auth procedures<br/>rpc-route<br/>[route.ts]"]
  node_health_route["Health procedures<br/>rpc-route<br/>[route.ts]"]
end

subgraph group_backend["Domain and persistence"]
  node_user_service["User service<br/>domain-service<br/>[index.ts]"]
  node_google_oauth{{"Google OAuth<br/>identity-client<br/>[google-oauth.ts]"}}
  node_database[("Database access<br/>persistence-api<br/>[index.ts]")]
  node_user_model["User persistence model<br/>drizzle-model<br/>[user.ts]"]
  node_db_schema[("Database schema<br/>drizzle-schema<br/>[schema.ts]")]
  node_logger["Structured logger<br/>shared-observability<br/>[index.ts]"]
end

node_turbo -->|"builds and runs"| node_web_app
node_turbo -->|"builds and runs"| node_api_host
node_web_app -->|"initializes"| node_web_providers
node_web_providers -->|"provides RPC dependencies"| node_web_trpc
node_web_trpc -->|"typed procedure calls"| node_trpc_server
node_web_env -.->|"configures"| node_web_app
node_api_env -.->|"configures"| node_api_host
node_api_host -->|"hosts and delegates to"| node_trpc_server
node_trpc_server -->|"creates per request"| node_trpc_context
node_trpc_server -->|"routes auth requests"| node_auth_route
node_trpc_server -->|"routes health requests"| node_health_route
node_auth_route -->|"invokes"| node_user_service
node_user_service -->|"authenticates with"| node_google_oauth
node_user_service -->|"persists users"| node_database
node_database -->|"accesses"| node_user_model
node_user_model -->|"maps to"| node_db_schema
node_docker -.->|"provides local backing service"| node_database
node_api_host -->|"emits logs"| node_logger

click node_turbo "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/turbo.json"
click node_docker "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/docker-compose.yml"
click node_web_app "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/app/layout.tsx"
click node_web_providers "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/providers/global.tsx"
click node_web_trpc "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/trpc/client.ts"
click node_web_env "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/env.js"
click node_api_host "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/api/src/index.ts"
click node_api_env "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/api/src/env.ts"
click node_trpc_server "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/index.ts"
click node_trpc_context "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/context.ts"
click node_auth_route "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/routes/auth/route.ts"
click node_health_route "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/routes/health/route.ts"
click node_user_service "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/services/user/index.ts"
click node_google_oauth "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/services/clients/google-oauth.ts"
click node_database "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/index.ts"
click node_user_model "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/models/user.ts"
click node_db_schema "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/schema.ts"
click node_logger "https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/logger/index.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_turbo,node_docker toneBlue
class node_web_app,node_web_providers,node_web_trpc,node_web_env toneAmber
class node_api_host,node_api_env toneMint
class node_trpc_server,node_trpc_context,node_auth_route,node_health_route toneRose
class node_user_service,node_google_oauth,node_database,node_user_model,node_db_schema,node_logger toneIndigo
```

## Components

### Workspace

- [Turbo workspace](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/turbo.json): orchestration · pnpm/Turbo coordinates monorepo tasks. · `turbo.json`
- [Local infrastructure](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/docker-compose.yml): compose · Local backing services are defined here. · `docker-compose.yml`

### Web runtime

- [Next.js app](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/app/layout.tsx): app-router · `apps/web/app/layout.tsx`
- [Global providers](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/providers/global.tsx): client-wiring · `apps/web/providers/global.tsx`
- [Web tRPC adapters](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/trpc/client.ts): rpc-client · Separates browser and server RPC execution. · `apps/web/trpc/client.ts`
- [Web environment](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/web/env.js): runtime-env · `apps/web/env.js`

### API runtime

- [API host](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/api/src/index.ts): node-server · Bootstraps HTTP serving and delegates to shared RPC. · `apps/api/src/index.ts`
- [API environment](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/apps/api/src/env.ts): runtime-env · `apps/api/src/env.ts`

### Shared RPC

- [tRPC server](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/index.ts): transport-core · Shared typed transport boundary. · `packages/trpc/server/index.ts`
- [Request context](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/context.ts): request-scope · Assembles request-scoped dependencies. · `packages/trpc/server/context.ts`
- [Auth procedures](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/routes/auth/route.ts): rpc-route · `packages/trpc/server/routes/auth/route.ts`
- [Health procedures](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/trpc/server/routes/health/route.ts): rpc-route · `packages/trpc/server/routes/health/route.ts`

### Domain and persistence

- [User service](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/services/user/index.ts): domain-service · User operations and models. · `packages/services/user/index.ts`
- [Google OAuth](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/services/clients/google-oauth.ts): identity-client · External identity provider adapter. · `packages/services/clients/google-oauth.ts`
- [Database access](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/index.ts): persistence-api · Exports database connection and access APIs. · `packages/database/index.ts`
- [User persistence model](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/models/user.ts): drizzle-model · `packages/database/models/user.ts`
- [Database schema](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/database/schema.ts): drizzle-schema · `packages/database/schema.ts`
- [Structured logger](https://github.com/piyushgarg-dev/trpc-monorepo/blob/main/packages/logger/index.ts): shared-observability · `packages/logger/index.ts`

## Connections

- 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
