# sqb-codes/url-shortner-mono architecture

> Architecture diagram of the GitHub repository sqb-codes/url-shortner-mono, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sqb-codes/url-shortner-mono
- Repository: https://github.com/sqb-codes/url-shortner-mono

## Overview

This is an Express URL-shortening service. An HTTP client submits a URL through the registered API routes; the controller delegates creation to the service, which allocates and persists an ID, encodes it as Base62, and stores the short code. For redirects, the service checks Redis first and falls back to MongoDB, caching a successful lookup before returning the destination to the controller. Startup connects the database and cache before listening. The README documents these capabilities; connections not explicit in sampled code are omitted.

Main entry points: [`src/services/url.service.js`](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/services/url.service.js) (URL Service), [`src/repositories/url.repository.js`](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/repositories/url.repository.js) (URL Repository), [`src/app.js`](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/app.js) (Express App), [`server.js`](https://github.com/sqb-codes/url-shortner-mono/blob/main/server.js) (Server Startup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_http["HTTP API"]
  node_app["Express App<br/>[app.js]"]
  node_routes["URL Routes<br/>[url.routes.js]"]
  node_controller["URL Controller<br/>[url.controller.js]"]
  node_errors["Error Handler"]
end

subgraph group_logic["Shortening Logic"]
  node_service["URL Service<br/>[url.service.js]"]
  node_encoder["Base62 Encoder<br/>[base62.js]"]
end

subgraph group_persistence["Persistence"]
  node_repository["URL Repository<br/>[url.repository.js]"]
  node_urlmodel["URL Record<br/>[url.model.js]"]
  node_countermodel["ID Counter"]
end

subgraph group_runtime["Runtime Connections"]
  node_dbconfig["Mongo Connection<br/>[db.js]"]
  node_redisconfig["Redis Connection<br/>[redis.js]"]
  node_server["Server Startup<br/>[server.js]"]
end

node_client(("HTTP Client"))
node_mongo[("MongoDB")]
node_redis[("Redis Cache")]

node_client -->|"sends requests"| node_app
node_app -->|"mounts routes"| node_routes
node_app -->|"registers handler"| node_errors
node_routes -->|"dispatches"| node_controller
node_controller -->|"calls"| node_service
node_service -->|"persists and looks up"| node_repository
node_service -->|"encodes ID"| node_encoder
node_service -->|"checks and caches"| node_redis
node_repository -->|"reads and writes"| node_urlmodel
node_repository -->|"increments"| node_countermodel
node_repository -->|"persists records"| node_mongo
node_server -->|"starts"| node_app
node_server -->|"connects"| node_dbconfig
node_server -->|"connects"| node_redisconfig
node_dbconfig -->|"opens connection"| node_mongo
node_redisconfig -->|"opens connection"| node_redis

click node_app "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/app.js"
click node_routes "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/routes/url.routes.js"
click node_controller "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/controllers/url.controller.js"
click node_errors "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/middlewares/error.middleware.js"
click node_service "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/services/url.service.js"
click node_encoder "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/utils/base62.js"
click node_repository "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/repositories/url.repository.js"
click node_urlmodel "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/models/url.model.js"
click node_countermodel "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/models/url.counter.model.js"
click node_dbconfig "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/config/db.js"
click node_redisconfig "https://github.com/sqb-codes/url-shortner-mono/blob/main/src/config/redis.js"
click node_server "https://github.com/sqb-codes/url-shortner-mono/blob/main/server.js"

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_app,node_routes,node_controller,node_errors,node_client toneBlue
class node_service,node_encoder,node_mongo,node_redis toneAmber
class node_repository,node_urlmodel,node_countermodel toneMint
class node_dbconfig,node_redisconfig,node_server toneRose
```

## Components

### HTTP API

- [Express App](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/app.js): `src/app.js`
- [URL Routes](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/routes/url.routes.js): `src/routes/url.routes.js`
- [URL Controller](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/controllers/url.controller.js): `src/controllers/url.controller.js`
- [Error Handler](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/middlewares/error.middleware.js): `src/middlewares/error.middleware.js`

### Shortening Logic

- [URL Service](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/services/url.service.js): `src/services/url.service.js`
- [Base62 Encoder](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/utils/base62.js): `src/utils/base62.js`

### Persistence

- [URL Repository](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/repositories/url.repository.js): `src/repositories/url.repository.js`
- [URL Record](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/models/url.model.js): `src/models/url.model.js`
- [ID Counter](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/models/url.counter.model.js): `src/models/url.counter.model.js`

### Runtime Connections

- [Mongo Connection](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/config/db.js): `src/config/db.js`
- [Redis Connection](https://github.com/sqb-codes/url-shortner-mono/blob/main/src/config/redis.js): `src/config/redis.js`
- [Server Startup](https://github.com/sqb-codes/url-shortner-mono/blob/main/server.js): `server.js`

### Other components

- **HTTP Client**
- **MongoDB**
- **Redis Cache**

## Connections

- HTTP Client → Express App: sends requests (evidence: `README.md`)
- Express App → URL Routes: mounts routes (evidence: `src/app.js`)
- Express App → Error Handler: registers handler (evidence: `src/app.js`)
- URL Routes → URL Controller: dispatches (evidence: `src/routes/url.routes.js`)
- URL Controller → URL Service: calls (evidence: `src/controllers/url.controller.js`)
- URL Service → URL Repository: persists and looks up (evidence: `src/services/url.service.js`)
- URL Service → Base62 Encoder: encodes ID (evidence: `src/services/url.service.js`)
- URL Service → Redis Cache: checks and caches (evidence: `src/services/url.service.js`)
- URL Repository → URL Record: reads and writes (evidence: `src/repositories/url.repository.js`)
- URL Repository → ID Counter: increments (evidence: `src/repositories/url.repository.js`)
- URL Repository → MongoDB: persists records (evidence: `README.md`)
- Server Startup → Express App: starts (evidence: `server.js`)
- Server Startup → Mongo Connection: connects (evidence: `server.js`)
- Server Startup → Redis Connection: connects (evidence: `server.js`)
- Mongo Connection → MongoDB: opens connection (evidence: `src/config/db.js`)
- Redis Connection → Redis Cache: opens connection (evidence: `src/config/redis.js`)
