199 lines
11 KiB
Markdown
199 lines
11 KiB
Markdown
# Showy Backend — Documentation Hub
|
|
|
|
> **Version:** 1.0.0 · **Runtime:** Node.js 18+ · **Framework:** Express 4 · **Database:** MySQL 8
|
|
|
|
---
|
|
|
|
## What is Showy?
|
|
|
|
Showy is a self-hosted media library tracker for movies and TV series. The backend is a REST API server that:
|
|
|
|
- Manages a personal library of movies and TV series
|
|
- Fetches metadata, images, and credits from **TMDB API v3**
|
|
- Tracks watch history per movie and per episode
|
|
- Organises entries into collections (TMDB-sourced or user-created)
|
|
- Serves optimised media assets (posters, backdrops, stills) from local storage
|
|
- Provides statistics dashboards and calendar feeds
|
|
- Runs standalone, inside Docker, or embedded in an Electron desktop app
|
|
|
|
---
|
|
|
|
## Architecture at a Glance
|
|
|
|
```
|
|
┌───────────────────────────────────────────────────────────┐
|
|
│ Client (Angular / Electron) │
|
|
└──────────────────────────┬────────────────────────────────┘
|
|
│ HTTP (REST JSON)
|
|
▼
|
|
┌──────────────────────────────────────────────────────────┐
|
|
│ Express Server (:4000) │
|
|
│ │
|
|
│ ┌─────────┐ ┌────────────┐ ┌────────────────────┐ │
|
|
│ │ Routes │──▶│ Controllers│──▶│ Managers │ │
|
|
│ └─────────┘ └────────────┘ │ (EntryMgr, CollMgr, │ │
|
|
│ │ PeopleMgr, MediaMgr)│ │
|
|
│ └─────────┬────────────┘ │
|
|
│ │ │
|
|
│ ┌──────────────────────┼───────────┐ │
|
|
│ ▼ ▼ ▼ │
|
|
│ ┌────────────┐ ┌──────────────┐ ┌─────┐ │
|
|
│ │ Services │ │ TMDB Service │ │Sharp│ │
|
|
│ │ (Entry, │ │ (axios→TMDB) │ │ Opt │ │
|
|
│ │ Collection,│ └──────────────┘ └─────┘ │
|
|
│ │ People...) │ │
|
|
│ └──────┬──────┘ │
|
|
│ │ │
|
|
│ ▼ │
|
|
│ ┌────────────────────┐ │
|
|
│ │ Sequelize ORM │ │
|
|
│ │ (17 models) │ │
|
|
│ └─────────┬──────────┘ │
|
|
└───────────────────┼──────────────────────────────────────┘
|
|
│
|
|
▼
|
|
┌────────────────┐ ┌────────────────────┐
|
|
│ MySQL 8.0 │ │ Local Filesystem │
|
|
│ (Showzy DB) │ │ (images/previews) │
|
|
└────────────────┘ └────────────────────┘
|
|
```
|
|
|
|
---
|
|
|
|
## Quick Start
|
|
|
|
### Prerequisites
|
|
|
|
| Tool | Version |
|
|
| ------------- | --------------------------------------------------------- |
|
|
| Node.js | 18+ |
|
|
| MySQL | 8.0 |
|
|
| pnpm (or npm) | any |
|
|
| TMDB API key | [themoviedb.org](https://www.themoviedb.org/settings/api) |
|
|
|
|
### 1. Install dependencies
|
|
|
|
```bash
|
|
cd backend
|
|
pnpm install
|
|
```
|
|
|
|
### 2. Configure settings
|
|
|
|
```bash
|
|
cp settings.json.example settings.json
|
|
```
|
|
|
|
Edit `settings.json`:
|
|
|
|
```json
|
|
{
|
|
"userDataPath": "C:\\Users\\you\\Documents\\Showy Data",
|
|
"tmdbApiUrl": "https://api.themoviedb.org/3/",
|
|
"tmdbApiKey": "YOUR_KEY_HERE",
|
|
"backPort": 4000
|
|
}
|
|
```
|
|
|
|
### 3. Start MySQL
|
|
|
|
Make sure a MySQL 8 instance is running with a database called `Showzy` and user `root` / `password` (or update `config/database.js`).
|
|
|
|
### 4. Run the server
|
|
|
|
```bash
|
|
# Development (auto-restart on file changes)
|
|
pnpm run backend
|
|
|
|
# Production
|
|
node server.js
|
|
```
|
|
|
|
The server will:
|
|
1. Connect to MySQL
|
|
2. Auto-sync the schema (`ALTER TABLE` if needed)
|
|
3. Seed the genres table
|
|
4. Start listening on the configured port (default `4000`)
|
|
|
|
### 5. Verify
|
|
|
|
```bash
|
|
curl http://localhost:4000/api/v1/ping
|
|
# → { "message": "pong" }
|
|
```
|
|
|
|
---
|
|
|
|
## Documentation Index
|
|
|
|
| Document | Description |
|
|
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| **[Project Setup & Configuration](project_setup.md)** | Tech stack, directory structure, `settings.json` reference, NPM scripts, startup sequence, Docker config, CORS, logging, Electron integration |
|
|
| **[API Reference](api_reference.md)** | All 55+ REST endpoints grouped by resource — HTTP methods, parameter tables, sample responses |
|
|
| **[Database Architecture](db_architecture.md)** | Full schema reference (17 tables), ERD, relationship summary, polymorphic media pattern, hooks, seeding, indexing |
|
|
| **[Service Layer](service_layer.md)** | Controller → Manager → Service architecture, service catalogue, TMDB API mapping, media pipeline, dependency graph |
|
|
| **[Error Handling](error_handling.md)** | Custom error hierarchy, global middleware, response contract, logging integration |
|
|
| **[Agents & Orchestrators](../agents.md)** | Manager services (EntryMgr, CollectionMgr, PeopleMgr, MediaMgr), orchestration flows, importer rate-limit strategy |
|
|
| **[Deployment Guide](deployment.md)** | Docker, standalone, Electron, and production deployment instructions |
|
|
|
|
---
|
|
|
|
## Key Concepts
|
|
|
|
### Entry
|
|
|
|
The core entity — represents either a **movie** or **TV series** (discriminated by `is_tvseries`). Everything revolves around entries: ratings, watch history, seasons, credits, collections, and media assets.
|
|
|
|
### Managers vs. Services
|
|
|
|
- **Services** handle single-responsibility data access (CRUD, queries).
|
|
- **Managers** orchestrate multi-step workflows across services (fetch from TMDB → save → download media → generate previews). They are the primary business logic layer.
|
|
|
|
### Polymorphic Media
|
|
|
|
The `media` table stores images for entries, collections, seasons, episodes, and people using a `(mediaable, mediaable_id)` discriminator pattern — no physical FK constraints.
|
|
|
|
### Lazy Fetching
|
|
|
|
When a person or entry is accessed for the first time, the corresponding Manager checks whether the data is complete. If not, it transparently fetches missing data from TMDB, downloads media, and returns the enriched object.
|
|
|
|
### Preview Generation
|
|
|
|
All downloaded images get a low-resolution preview variant generated via `sharp`. Previews are served when the client requests `size=preview`, reducing bandwidth for grid/list views.
|
|
|
|
---
|
|
|
|
## API Quick Reference
|
|
|
|
| Verb | Endpoint | Description |
|
|
| -------- | -------------------------------------- | ------------------- |
|
|
| `GET` | `/api/v1/ping` | Health check |
|
|
| `GET` | `/api/v1/entries` | List all entries |
|
|
| `POST` | `/api/v1/entries` | Add entry from TMDB |
|
|
| `GET` | `/api/v1/entries/:id` | Get entry details |
|
|
| `PUT` | `/api/v1/entries/:id/refresh` | Refresh from TMDB |
|
|
| `DELETE` | `/api/v1/entries/:id` | Delete entry |
|
|
| `GET` | `/api/v1/collections` | List collections |
|
|
| `POST` | `/api/v1/collections` | Create collection |
|
|
| `GET` | `/api/v1/people` | List people |
|
|
| `GET` | `/api/v1/seasons/:id/episodes` | Get season episodes |
|
|
| `POST` | `/api/v1/entry-seen` | Log a watch |
|
|
| `GET` | `/api/v1/statistics/watch-time` | Total watch time |
|
|
| `GET` | `/api/v1/media/:cat/:type/:size/:file` | Serve image |
|
|
|
|
See the full **[API Reference](api_reference.md)** for all endpoints.
|
|
|
|
---
|
|
|
|
## Environment Variables
|
|
|
|
| Variable | Set By | Purpose |
|
|
| ---------------- | -------------------- | ------------------------------------------------------------------------------ |
|
|
| `PARENT_PROCESS` | Electron (`main.js`) | JSON object with `electron_path`; tells the backend it was spawned by Electron |
|
|
| `MYSQL_HOST` | Docker Compose | MySQL hostname (defaults to `localhost` in `config/database.js`) |
|
|
| `MYSQL_DB` | Docker Compose | Database name |
|
|
| `MYSQL_USER` | Docker Compose | Database user |
|
|
| `MYSQL_PASSWORD` | Docker Compose | Database password |
|
|
|
|
> **Note:** In non-Docker mode, the database connection is hardcoded in `config/database.js`. Environment variables only apply when running via Docker Compose.
|