# nawinsharma/shrinklyy-t3 architecture

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

- Interactive diagram: https://gitdiagram.com/nawinsharma/shrinklyy-t3
- Repository: https://github.com/nawinsharma/shrinklyy-t3

## Overview

##### Main components
- Frontend – Built with Next.js:
- Pages and layouts (e.g., landing page, authentication pages, dashboard, and short link pages found under src/app).
- Client-side hooks (under src/hooks) for functionality such as searching and handling URL short links.
- Backend & API Services:
- Next.js API Routes (in src/app/api) handling authentication ([...nextauth]) and tRPC endpoints ([trpc]).
- tRPC layers (in src/trpc and src/server/api/trpc.ts) ensuring type-safe communication between client and server.
- Server business logic, including authentication and business rules, contained within src/server (api routers for short links, authentication, and database communication).
- Database & ORM:
- Database configuration and access handled with Prisma. The prisma directory contains schema definitions and migration files (which interact with PostgreSQL as per the README).
- Authentication:
- NextAuth configuration to facilitate secure login through providers (GitHub, Google, etc.) with endpoints found under src/app/api/auth.
- Validation & Types:
- Input validation using Zod (as indicated in the README) with validations in src/validations for creating and updating short links.
- Shared TypeScript types (in src/types) ensuring consistent data contracts between various parts of the system.

##### Establish relationships and interactions
- The Next.js frontend interacts with the backend either via Next.js API routes (direct REST-like interactions) or through the tRPC layer for type-safe API calls.
- API endpoints forward requests to the server business logic in src/server. Business logic then interacts with Prisma to execute database operations.
- Authentication flows are triggered from the frontend and managed by NextAuth calling the appropriate API routes, integrating with third-party providers.
- The database component (PostgreSQL) is abstracted via Prisma with migrations documented in the prisma directory, ensuring type-safe schema and migration histories.
- Client-side hooks and libraries (src/lib) handle utilities such as URL encoding/decoding and formatting (e.g., timeAgo).

##### Architectural Patterns & Design Principles
- The app leverages a modern full-stack design where Next.js functions as both the frontend renderer and the backend server.
- It follows a modular design separating concerns: views (pages and UI components), business logic (server and API routers), and data access (Prisma with PostgreSQL).
- Type safety across the stack is ensured with TypeScript, Zod for runtime validation, and tRPC for the API layer minimizing boilerplate.
- Clear separation of client-side and server-side logic even within a unified framework (Next.js) is emphasized.

- Rectangles for major system components.
- Annotations where necessary:
- Detail that the Next.js app handles both SSR (server-side rendering) and API routing.
- Use of type safety across the stack (TypeScript, Zod).
- Point out that migrations and schema changes are managed via the Prisma directory.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (Next.js)"
        FE1["Pages & Layouts"]
        FE2["UI Components"]
        FE3["UI Subcomponents"]
        FE4["Client-side Hooks"]
    end

    subgraph "Backend & API Services"
        BE1["Next.js API Auth Routes"]
        BE2["Next.js API tRPC Routes"]
        BE3["Server tRPC Config"]
        BE4["tRPC Integration"]
        BE5["Server Business Logic"]
    end

    subgraph "Authentication"
        AUTH2["Server Auth Logic"]
    end

    subgraph "Database & ORM"
        DB1["Prisma & Migrations"]
        DB2["Database Connection"]
    end

    subgraph "Validation & Types"
        VT1["Validations (Zod)"]
        VT2["Shared Types"]
    end

    subgraph "External Services"
        EXTPRO["Third-Party Auth Providers"]
    end

    %% Frontend to Backend/API interactions
    FE1 -->|"AuthCall"| BE1
    FE4 -->|"tRPCCall"| BE4

    %% Backend internal interactions
    BE2 -->|"APICalls"| BE5
    BE3 -->|"configures"| BE5
    BE4 -->|"routes"| BE5

    %% Authentication flow
    BE1 -->|"delegatesAuth"| AUTH2
    BE1 -->|"OAuth"| EXTPRO

    %% Server Business Logic to Database & Validation/Types
    BE5 -->|"PrismaQueries"| DB1
    BE5 -->|"dbConnection"| DB2
    BE5 -->|"validates"| VT1
    BE5 -->|"usesTypes"| VT2

    %% Frontend usage of shared types
    FE2 -->|"uses"| VT2

    %% Styles
    classDef frontend fill:#FFEBEE,stroke:#C62828,stroke-width:2px;
    classDef backend fill:#E3F2FD,stroke:#1565C0,stroke-width:2px;
    classDef auth fill:#F3E5F5,stroke:#6A1B9A,stroke-width:2px;
    classDef db fill:#E8F5E9,stroke:#2E7D32,stroke-width:2px;
    classDef validation fill:#FFFDE7,stroke:#F9A825,stroke-width:2px;
    classDef external fill:#ECEFF1,stroke:#37474F,stroke-width:2px;

    class FE1,FE2,FE3,FE4 frontend;
    class BE1,BE2,BE3,BE4,BE5 backend;
    class AUTH2 auth;
    class DB1,DB2 db;
    class VT1,VT2 validation;
    class EXTPRO external;

    %% Click Events
    click FE1 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/app"
    click FE2 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/components"
    click FE3 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/components/ui"
    click FE4 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/hooks"
    click BE1 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/app/api/auth"
    click BE2 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/app/api/trpc"
    click BE3 "https://github.com/nawinsharma/shrinklyy-t3/blob/master/src/server/api/trpc.ts"
    click BE4 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/trpc"
    click BE5 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/server"
    click DB1 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/prisma"
    click DB2 "https://github.com/nawinsharma/shrinklyy-t3/blob/master/src/server/db.ts"
    click AUTH2 "https://github.com/nawinsharma/shrinklyy-t3/blob/master/src/server/auth.ts"
    click VT1 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/validations"
    click VT2 "https://github.com/nawinsharma/shrinklyy-t3/tree/master/src/types"
```
