# kanbn/kan architecture

> Architecture diagram of the GitHub repository kanbn/kan, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kanbn/kan
- Repository: https://github.com/kanbn/kan

## Overview

##### Project Type & Purpose
– This is a full-stack, open-source Trello-like project management system (“Kan”), organized as a pnpm/Turbo monorepo.
– Purpose: Users interact via a Next.js web app (apps/web), backed by a tRPC-based API (packages/api), persisting data in PostgreSQL (packages/db), with authentication (packages/auth), email notifications (packages/email), billing (packages/stripe), and shared utilities (packages/shared).

##### File Structure Highlights
- apps/web – Next.js frontend + API routes (trpc, auth callbacks, Stripe webhooks, file uploads).
- apps/docs – Documentation site (MDX).
- packages/api – Express/Node server using tRPC routers for boards, cards, users, etc.
- packages/db – Drizzle ORM client, schema definitions, repository pattern, migrations.
- packages/auth – Wrapper around Better Auth for session management.
- packages/email – React-Email service for sending templated emails (magic links, invites).
- packages/stripe – Stripe integration library.
- packages/shared – Shared constants and helpers.
- tooling & config – ESLint, Prettier, Tailwind, TypeScript configs, Docker, CI workflows.

##### README Insights
– Tech stack: Next.js, tRPC, Better Auth, Tailwind CSS, Drizzle ORM, React Email.
– Env vars reveal external dependencies: PostgreSQL, SMTP server, S3-compatible storage, Stripe, OAuth providers (Google, GitHub, Discord).
– Development workflow: pnpm workspaces, migrations, dev server via Turborepo scripts.

##### Diagramming the Architecture
a. Main Components
– Frontend (apps/web): Next.js pages & React components
– API Layer: tRPC endpoints (packages/api) behind Next.js API routes
– Authentication Service: Better Auth (packages/auth)
– Database: PostgreSQL (packages/db via Drizzle ORM)
– Email Service: React-Email renderer + SMTP (packages/email)
– Storage: S3-compatible object storage for uploads/avatars
– Billing: Stripe integration (packages/stripe)
– External OAuth Providers: Google, GitHub, Discord
b. Relationships & Data Flows
– User ↔ Frontend: HTTP/HTTPS, WebSocket (if any future realtime)
– Frontend → API Layer: tRPC calls over HTTP (Next.js API routes)
– API Layer ↔ Database: Drizzle ORM queries/inserts via PostgreSQL driver
– API Layer → Auth Service: session creation & validation via Better Auth SDK
– API Layer → Email Service: triggers templated email sends via SMTP
– API Layer → Storage Service: presigned URLs or direct uploads to S3
– Frontend → OAuth Providers: OAuth redirects/callbacks handled via API routes
– Backend → Stripe API: billing & checkout session management
c. Architectural Patterns & Principles
– Monorepo & Turborepo for multi-package orchestration
– Modular tRPC routers per domain (board, card, user, workspace)
– Repository pattern in DB layer for data access abstraction
– Shared libraries (packages/shared) to enforce DRY principles
– Infrastructure-as-code hints via Dockerfile & docker-compose for local dev
d. Key Technologies & Frameworks
– Next.js (SSR/SSG, API routes)
– tRPC & Zod (type-safe RPC)
– Better Auth (secure cookie sessions)
– Drizzle ORM (TypeScript-first SQL)
– React-Email (JSX email templates)
– Stripe Node SDK
– PostgreSQL, SMTP, S3

##### Detail & Depth
– Capture every core service (Auth, Email, Billing, Storage).
– Include how shared code fits in (e.g., shared Zod schemas).
– Show how environment variables map to service endpoints.
– Avoid over-generalization—display all domain-specific tRPC routers and DB repos if space allows.

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Apps Layer
  subgraph "apps"
    subgraph "Frontend (apps/web)"
      Frontend["Next.js Frontend"]:::frontend
      ReactComp["React Components & Views"]:::frontend
      APIRoutes["API Routes (trpc, auth, stripe, upload)"]:::frontend
    end
    subgraph "Documentation (apps/docs)"
      Docs["MDX Documentation"]:::frontend
    end
  end

  %% Packages Layer
  subgraph "packages"
    subgraph "API Layer (packages/api)"
      TRPCServer["tRPC Server & Routers"]:::api
    end
    subgraph "DB Layer (packages/db)"
      DBClient["Drizzle ORM Client"]:::db
      Schema["Schema & Repositories"]:::db
    end
    subgraph "Auth Service (packages/auth)"
      AuthService["Better Auth Service"]:::service
    end
    subgraph "Email Service (packages/email)"
      EmailService["React-Email Service"]:::service
    end
    subgraph "Billing Service (packages/stripe)"
      StripeService["Stripe Integration"]:::service
    end
    subgraph "Shared (packages/shared)"
      Shared["Shared Utils & Constants"]:::shared
    end
  end

  %% External Services
  subgraph "External Services"
      OAuth["OAuth Providers\n(Google, GitHub, Discord)"]:::external
      SMTP["SMTP Server"]:::external
      S3["S3-compatible Storage"]:::external
      Postgres["PostgreSQL Database"]:::external
      StripeAPI["Stripe API"]:::external
  end

  %% Client Interactions
  User["User"]:::external -->|Uses| Frontend
  User -->|Reads docs| Docs

  %% Frontend to Backend
  Frontend -->|tRPC Calls| TRPCServer
  Frontend -->|OAuth Redirects| OAuth
  Frontend -->|Stripe Checkout| StripeService

  %% Backend Interactions
  TRPCServer -->|Queries/Inserts| DBClient
  DBClient -->|Reads/Writes| Postgres
  TRPCServer -->|Session Management| AuthService
  TRPCServer -->|Send Emails| EmailService
  EmailService -->|SMTP Transport| SMTP
  TRPCServer -->|Upload URLs| S3
  TRPCServer -->|Billing Ops| StripeService
  TRPCServer -->|Shared Logic| Shared

  %% Service to External
  StripeService -->|API Calls| StripeAPI

  %% Click Events
  click Frontend "https://github.com/kanbn/kan/tree/main/apps/web/"
  click ReactComp "https://github.com/kanbn/kan/tree/main/apps/web/src/components/"
  click APIRoutes "https://github.com/kanbn/kan/tree/main/apps/web/src/pages/api/"
  click Docs "https://github.com/kanbn/kan/tree/main/apps/docs/"
  click TRPCServer "https://github.com/kanbn/kan/tree/main/packages/api/src/"
  click DBClient "https://github.com/kanbn/kan/blob/main/packages/db/src/client.ts"
  click Schema "https://github.com/kanbn/kan/tree/main/packages/db/src/schema/"
  click AuthService "https://github.com/kanbn/kan/tree/main/packages/auth/src/"
  click EmailService "https://github.com/kanbn/kan/blob/main/packages/email/src/sendEmail.tsx"
  click StripeService "https://github.com/kanbn/kan/blob/main/packages/stripe/src/index.ts"
  click Shared "https://github.com/kanbn/kan/tree/main/packages/shared/src/"

  %% Styles
  classDef frontend fill:#D0E7FF,stroke:#0288D1,color:#000;
  classDef api fill:#E0F2F1,stroke:#00796B,color:#000;
  classDef db fill:#FFF3E0,stroke:#FB8C00,color:#000;
  classDef service fill:#E8F5E9,stroke:#388E3C,color:#000;
  classDef shared fill:#F3E5F5,stroke:#8E24AA,color:#000;
  classDef external fill:#ECEFF1,stroke:#607D8B,color:#000;
```
