# euxzy/lazy-chat architecture

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

- Interactive diagram: https://gitdiagram.com/euxzy/lazy-chat
- Repository: https://github.com/euxzy/lazy-chat

## Overview

##### Project Type & Purpose
- This is a full-stack, SvelteKit-based web application scaffolded via the `sv` CLI.
- It includes a client-side SPA/SSR layer (Svelte components & routes), a Node-based server for HTTP/SSR and WebSocket handling, a relational database layer (via Drizzle ORM), plus E2E and unit testing.

##### Top-Level File Structure & Architectural Hints
- `/src`
- `routes/` — SvelteKit page routes with server endpoints (`+page.server.ts`) and client pages (`.svelte`).
- `lib/` — shared UI components (`ui/`, `layout/`, `chat/`), stores, utilities, and server DB schema (`lib/server/db/schema`).
- `/server` — custom server entrypoints (`index.ts`) and WebSocket logic (`socket.ts`).
- `/plugins/vite/socket.ts` — Vite plugin that provisions the WebSocket plugin during dev/build.
- `drizzle.config.ts` — Drizzle ORM configuration for schema definitions under `lib/server/db/schema`.
- `e2e/` & `src/demo.spec.ts` — Playwright and Vitest tests.
- `vite.config.ts` & `svelte.config.js` — bundler and framework configs.

a. Client (Browser)
- SvelteKit app: SSR entrypoints + client hydration.
- UI Components: chat bubbles, layout, buttons, etc.
- Stores: Socket store for real-time data.
b. Dev/Build Toolchain
- Vite: bundles code & applies `socket` plugin.
- Husky pre-commit/pre-push hooks.
- Prettier/ESLint.
c. Server
- SvelteKit Node adapter: handles HTTP/SSR & endpoints in `routes` folder.
- `/server/index.ts`: custom server bootstrap if present.
- WebSocket server: `server/socket.ts` + Vite plugin wiring.
- Drizzle ORM layer: configured via `drizzle.config.ts`, uses schema under `lib/server/db/schema`.
d. Database
- Postgres (or similar) where Drizzle applies migrations and queries.
e. Tests & CI
- Playwright E2E tests hitting the running server.
- Vitest unit tests for components and utilities.

##### Data Flows & Relationships
- HTTP/SSR: Browser ↔ SvelteKit server (page loads, API calls in `+page.server.ts`).
- WebSocket: Browser’s Socket store ↔ WebSocket endpoint on the server.
- ORM: SvelteKit server logic ↔ Drizzle ORM ↔ Database.
- Build: Source files ↔ Vite + `socket` plugin → production assets.
- CI Hooks: Git operations → Husky → lint/tests → commit/push.

##### Architectural Patterns & Principles
- Component-based UI (Svelte).
- Server-Side Rendering + client hydration (SvelteKit).
- Real-time communication via WebSockets.
- Single source of truth for data via Drizzle ORM schemas.
- Separation of concerns: UI vs store vs server vs database.

##### Essential Elements
- Frontend: SvelteKit routes, components, stores.
- Backend: SvelteKit server endpoints, custom server, socket handler.
- Data Layer: Drizzle config & schema modules, external DB.
- Toolchain: Vite + plugin, ESLint/Prettier, Husky, Vitest/Playwright.
- Flows: SSR HTTP, WebSocket, build pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    Browser["Browser"]:::frontend
    subgraph "Frontend Layer"
        Routes["/src/routes"]:::frontend
        Components["src/lib/components"]:::frontend
        Stores["src/lib/stores/socket.svelte.ts"]:::frontend
        Utils["src/lib/utils.ts"]:::frontend
    end

    %% Server Layer
    subgraph "Server Layer"
        SKServer["SvelteKit Server (Node Adapter)"]:::server
        CustomServer["server/index.ts"]:::server
        WebSocket["server/socket.ts"]:::server
    end

    %% Data Layer
    subgraph "Data Layer"
        DrizzleConfig["drizzle.config.ts"]:::orm
        Schemas["src/lib/server/db/schema"]:::orm
        DB["Database"]:::orm
    end

    %% Dev/Build Tooling
    subgraph "Dev & Build Tooling"
        ViteConfig["vite.config.ts"]:::tooling
        VitePlugin["plugins/vite/socket.ts"]:::tooling
        Husky[".husky hooks"]:::tooling
        Lint["ESLint & Prettier"]:::tooling
        Git["Git Operations"]:::tooling
    end

    %% Testing & CI
    subgraph "Testing & CI"
        E2E["e2e/demo.test.ts"]:::tooling
        Playwright["playwright.config.ts"]:::tooling
        Unit["src/demo.spec.ts"]:::tooling
        RouteTests["src/routes/page.svelte.test.ts"]:::tooling
        VitestSetup["vitest-setup-client.ts"]:::tooling
    end

    %% Dist Output
    Dist["Production Assets (dist)"]:::tooling

    %% Flows
    Browser -->|"HTTP/SSR"| SKServer
    SKServer -->|"handles routes/endpoints"| Routes
    SKServer -->|"imports UI Components"| Components
    SKServer -->|"imports Stores & Utils"| Stores
    SKServer -->|"imports Stores & Utils"| Utils
    Browser -->|"WebSocket"| Stores
    Stores -->|"WebSocket"| WebSocket
    WebSocket -->|"wired by plugin"| VitePlugin
    SKServer -->|"custom bootstrap"| CustomServer
    SKServer -->|"uses ORM"| DrizzleConfig
    DrizzleConfig --> Schemas
    Schemas -->|"queries & migrations"| DB

    Browser -->|"source files"| ViteConfig -->|"bundles"| Dist
    VitePlugin -->|integrates| ViteConfig

    Git -->|"pre-commit/pre-push"| Husky
    Husky -->|"runs"| Lint
    Husky -->|"runs"| E2E
    Husky -->|"runs"| Unit

    E2E -->|"tests against"| SKServer
    Unit -->|"unit tests for"| Components
    RouteTests -->|"unit tests for"| Routes

    %% Click Events
    click Routes "https://github.com/euxzy/lazy-chat/tree/main/src/routes/"
    click Components "https://github.com/euxzy/lazy-chat/tree/main/src/lib/components/"
    click Stores "https://github.com/euxzy/lazy-chat/blob/main/src/lib/stores/socket.svelte.ts"
    click Utils "https://github.com/euxzy/lazy-chat/blob/main/src/lib/utils.ts"
    click CustomServer "https://github.com/euxzy/lazy-chat/blob/main/server/index.ts"
    click WebSocket "https://github.com/euxzy/lazy-chat/blob/main/server/socket.ts"
    click VitePlugin "https://github.com/euxzy/lazy-chat/blob/main/plugins/vite/socket.ts"
    click DrizzleConfig "https://github.com/euxzy/lazy-chat/blob/main/drizzle.config.ts"
    click Schemas "https://github.com/euxzy/lazy-chat/tree/main/src/lib/server/db/schema/"
    click E2E "https://github.com/euxzy/lazy-chat/blob/main/e2e/demo.test.ts"
    click Playwright "https://github.com/euxzy/lazy-chat/blob/main/playwright.config.ts"
    click Unit "https://github.com/euxzy/lazy-chat/blob/main/src/demo.spec.ts"
    click RouteTests "https://github.com/euxzy/lazy-chat/blob/main/src/routes/page.svelte.test.ts"
    click VitestSetup "https://github.com/euxzy/lazy-chat/blob/main/vitest-setup-client.ts"
    click Husky "https://github.com/euxzy/lazy-chat/tree/main/.husky/pre-commit"
    click Lint "https://github.com/euxzy/lazy-chat/blob/main/eslint.config.js"
    click ViteConfig "https://github.com/euxzy/lazy-chat/blob/main/vite.config.ts"

    %% Styles
    classDef frontend fill:#D6E4FF,stroke:#333,stroke-width:1px
    classDef server fill:#D4F1C5,stroke:#333,stroke-width:1px
    classDef orm fill:#FFE6B0,stroke:#333,stroke-width:1px
    classDef tooling fill:#E0E0E0,stroke:#333,stroke-width:1px
```
