# tusharathub/kilogram architecture

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

- Interactive diagram: https://gitdiagram.com/tusharathub/kilogram
- Repository: https://github.com/tusharathub/kilogram

## Overview

##### Project type and purpose
- This is a full-stack social media web application (an “Instagram-lite”) built with Next.js (app router), TypeScript, Prisma ORM, Tailwind / Shadcn UI, Clerk for auth, and UploadThing for file uploads.
- Users can sign in/up, create posts (with images), follow others, and receive notifications.

##### File structure
- Top-level:
- next.config.mjs, tsconfig.json, package.json / lock, postcss.config.mjs – project config & build
- prisma/schema.prisma – database schema for NeonDB via Prisma
- components.json – probably design tokens or Shadcn UI config
- src/app (Next.js app router):
- layout.tsx, globals.css – root layout & global styles
- page.tsx – home feed
- /api/uploadthing – serverless endpoints for file upload
- /notifications, /profile/[username] – dynamic routes & UI
- src/actions – server actions (notification, post, profile, user) encapsulating Prisma calls
- src/components – UI primitives, composite components, Shadcn UI wrappers in components/ui
- src/lib – shared utilities: prisma client, UploadThing client, misc utils
- middleware.ts – Next.js middleware (authentication checks, redirects)

##### Key insights from README
- Uses Clerk for authentication & authorization
- UploadThing handles file uploads to a storage backend
- NeonDB is the production PostgreSQL DB accessed via Prisma
- Hosted on Vercel

##### Main components
a. Frontend (Next.js React app)
- App Router pages & React Server Components
- Client Components (forms, buttons, PostCard, NavBar)
- Shadcn UI / Tailwind styling
- Clerk JS SDK for sign-in UI & session management
b. API Layer (Next.js Route Handlers & Server Actions)
- /api/uploadthing routes (core.ts, route.ts)
- src/actions/*.action.ts functions that perform Prisma queries
c. Business Logic & Data Access
- Prisma Client (src/lib/prisma.ts)
- Schema (prisma/schema.prisma)
- NeonDB (hosted PostgreSQL)
d. File Storage
- UploadThing service (cloud storage)
e. External Services
- Clerk (user auth & profile management)
- Vercel (hosting & serverless functions)
- CDN for public assets

##### Relationships & data flows
- User browser ⇄ Next.js frontend
- Clerk widget handles auth, attaches JWT/cookie
- UI components call Server Actions (POST/GET)
- Next.js Server (Route Handlers & Actions) ⇄ Prisma Client
- CRUD on NeonDB
- Next.js Server ⇄ UploadThing API
- Pre-signed URLs or direct uploads
- Notifications subsystem listens for events (e.g., new follow or post) and writes to DB
- Vercel manages serverless deployment of both frontend and API routes

##### Architectural patterns & design principles
- React Server Components & Client Components separation
- Single unified codebase (monorepo style) for frontend & backend
- “Fat” Server Actions layer encapsulating all data access via Prisma
- Stateless serverless functions, horizontal scaling via Vercel
- Externalized auth & storage to minimize custom infrastructure

##### Essential elements to capture
- Authentication flow: Browser ↔ Clerk ↔ Next.js Middleware
- Upload flow: Browser → Next.js /api/uploadthing → UploadThing
- Database operations: Server Actions → Prisma Client → NeonDB
- UI modules: Feed, Profile, Notifications, Post Creation

1) Sketch the user journey: sign in → view feed → create post → follow user → get notification
2) System touchpoints along that journey
3) Related files & folders into logical subsystems

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Browser["User's Browser"]:::client
        ClerkWidget["Clerk JS SDK"]:::client
        UIComponents["UI Components\n(Feed, Profile, Notifications, CreatePost)"]:::client
    end

    subgraph "Server Layer (Next.js on Vercel)"
        subgraph "Next.js App Router"
            Layout["Root Layout"]:::server
            GlobalsCSS["Global Styles"]:::server
            FeedPage["Home Feed Page"]:::server
            NotificationsPage["Notifications Module"]:::server
            ProfilePage["Profile Module (SSR)"]:::server
            ProfileClientComp["ProfilePageClient\n(Client Component)"]:::server
            ProfileLoading["Profile Loading\nState"]:::server
            Middleware["Auth Middleware"]:::server
        end

        subgraph "API Layer"
            UploadCore["UploadThing Core"]:::server
            UploadRoute["UploadThing Route"]:::server
        end

        subgraph "Server Actions"
            NotificationAction["notification.action.ts"]:::server
            PostAction["post.action.ts"]:::server
            ProfileAction["profile.action.ts"]:::server
            UserAction["user.action.ts"]:::server
        end

        PrismaClient["Prisma Client"]:::server
        UploadThingClient["UploadThing Client"]:::server
    end

    subgraph "Data Layer"
        DB["NeonDB (PostgreSQL)"]:::data
        Storage["UploadThing Storage"]:::data
    end

    subgraph "External Services"
        Vercel["Vercel (Hosting)"]:::external
        CDN["CDN (Public Assets)"]:::external
        ClerkService["Clerk"]:::external
    end

    Browser -->|uses| UIComponents
    Browser -->|auth UI| ClerkWidget
    UIComponents -->|HTTP| Layout
    Layout --> FeedPage
    Layout --> NotificationsPage
    Layout --> ProfilePage
    Layout --> ProfileClientComp
    Layout --> ProfileLoading
    FeedPage -->|server action| PostAction
    NotificationsPage -->|server action| NotificationAction
    ProfilePage -->|server action| ProfileAction
    ProfileClientComp -->|server action| UserAction
    Middleware -->|checks auth| Layout
    UploadCore -->|provides endpoints| UploadRoute
    UIComponents -->|upload file| UploadRoute
    UploadRoute -->|calls| UploadThingClient
    UploadThingClient -->|stores file| Storage
    PostAction -->|CRUD| PrismaClient
    ProfileAction -->|CRUD| PrismaClient
    NotificationAction -->|CRUD| PrismaClient
    UserAction -->|CRUD| PrismaClient
    PrismaClient -->|queries| DB
    Browser -->|serves assets| CDN
    Vercel -->|deploys| Layout
    Vercel -->|deploys| UploadRoute

    click Layout "https://github.com/tusharathub/kilogram/blob/main/src/app/layout.tsx"
    click GlobalsCSS "https://github.com/tusharathub/kilogram/blob/main/src/app/globals.css"
    click FeedPage "https://github.com/tusharathub/kilogram/blob/main/src/app/page.tsx"
    click NotificationsPage "https://github.com/tusharathub/kilogram/blob/main/src/app/notifications/page.tsx"
    click ProfilePage "https://github.com/tusharathub/kilogram/blob/main/src/app/profile/[username]/page.tsx"
    click ProfileClientComp "https://github.com/tusharathub/kilogram/blob/main/src/app/profile/[username]/ProfilePageClient.tsx"
    click ProfileLoading "https://github.com/tusharathub/kilogram/blob/main/src/app/profile/[username]/loading.tsx"
    click UploadCore "https://github.com/tusharathub/kilogram/blob/main/src/app/api/uploadthing/core.ts"
    click UploadRoute "https://github.com/tusharathub/kilogram/blob/main/src/app/api/uploadthing/route.ts"
    click NotificationAction "https://github.com/tusharathub/kilogram/blob/main/src/actions/notification.action.ts"
    click PostAction "https://github.com/tusharathub/kilogram/blob/main/src/actions/post.action.ts"
    click ProfileAction "https://github.com/tusharathub/kilogram/blob/main/src/actions/profile.action.ts"
    click UserAction "https://github.com/tusharathub/kilogram/blob/main/src/actions/user.action.ts"
    click PrismaClient "https://github.com/tusharathub/kilogram/blob/main/src/lib/prisma.ts"
    click UploadThingClient "https://github.com/tusharathub/kilogram/blob/main/src/lib/uploadthing.ts"
    click Schema "https://github.com/tusharathub/kilogram/blob/main/prisma/schema.prisma"
    click Middleware "https://github.com/tusharathub/kilogram/blob/main/src/middleware.ts"
    click UIComponents "https://github.com/tusharathub/kilogram/tree/main/src/components/"
    click UIComponentsUI "https://github.com/tusharathub/kilogram/tree/main/src/components/ui/"

    classDef client fill:#E0F7FA,stroke:#006064,color:#004D40
    classDef server fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
    classDef data fill:#FFF3E0,stroke:#EF6C00,color:#E65100
    classDef external fill:#ECEFF1,stroke:#607D8B,color:#37474F
```
