Files
X4Lo 229ecef902 Update docs vault and README for recent features
- New notes: History, Artwork
- Rewrite TMDB Integration: single detail request, entry mapper, rate
  limiter, refresh sync, stale-entry refresh, recommendations
- Update pages, routing, API reference, models, database migrations,
  services, components, hooks, API clients, settings, media system,
  library, Electron (Tauri in progress) and known issues
- Rewrite README for the current React/Express stack and setup
2026-11-05 19:55:04 +00:00

119 lines
4.5 KiB
Markdown

---
title: Database
tags:
- backend
- database
- mysql
- sequelize
---
# Database
## Overview
MySQL database named **Showzy**, managed by Sequelize 6 ORM.
- Connection: `localhost`, user `root`, password `password`
- Sync mode: `sequelize.sync({ alter: false })` — no auto-alter in production
- Migrations: `config/migrations.js` runs after sync on every start. It adds any column a model defines that the table lacks (additive only, nothing is dropped) and widens a few changed types (`certification`, `last_fetch`, `network_studio`, `networks.name`). New model attributes reach existing databases automatically on the next start.
- Logging: writes SQL to `sequelize.log`
- Genre seeding: 27 TMDB genres auto-seeded on startup
## Entity Relationship Diagram
```mermaid
erDiagram
Entry ||--o| EntryId : has
Entry ||--o| EntryRating : has
Entry ||--o{ EntrySeen : has
Entry ||--o{ Season : has
Entry ||--o{ Credit : has
Entry }o--o{ Genre : tagged
Entry }o--o{ Network : airs-on
Entry }o--o{ Collection : belongs-to
Entry }o--o{ Video : links-to
Season ||--o{ Episode : contains
Episode ||--o{ EpisodeSeen : has
Credit }o--|| People : references
Media ||--|| Entry : polymorphic
Media ||--|| Season : polymorphic
Media ||--|| Episode : polymorphic
Media ||--|| Collection : polymorphic
Media ||--|| People : polymorphic
```
## Relationships
Defined in `backend/config/relations.js`:
| Source | Target | Type | Through | On Delete |
|---|---|---|---|---|
| Entry | EntryId | `hasOne` | — | CASCADE |
| Entry | EntryRating | `hasOne` | — | CASCADE |
| Entry | EntrySeen | `hasMany` | — | CASCADE |
| Entry | Season | `hasMany` | — | CASCADE |
| Entry | Credit | `hasMany` | — | — |
| Entry | Genre | `belongsToMany` | EntryGenre | — |
| Entry | Network | `belongsToMany` | EntryNetwork | — |
| Entry | Collection | `belongsToMany` | UserCollectionEntry | — |
| Season | Episode | `hasMany` | — | CASCADE |
| Episode | EpisodeSeen | `hasMany` | — | — |
| Credit | People | `belongsTo` | — | — |
| Credit | Entry | `belongsTo` | — | — |
| People | Credit | `hasMany` (as 'roles') | — | — |
| Media | * | Polymorphic scopes | `mediaable` + `mediaableId` | — |
| Video | Entry | `belongsToMany` | VideoEntry | CASCADE |
### Polymorphic Media Scopes
Media uses `mediaable` (string: 'entry', 'people', 'collection', 'season', 'episode') and `mediaableId` to associate with any entity.
## Hooks
Defined in `backend/config/sequelizeHooks.js`:
- **UserCollectionEntry.afterCreate** — Updates collection `updatedAt`, auto-assigns `order`
- **UserCollectionEntry.afterUpdate** — Updates collection `updatedAt`, re-orders other entries
- **UserCollectionEntry.afterDestroy** — Updates collection `updatedAt`, cleanup
## All Models
See [[Models]] for detailed field definitions of every model.
### Quick Reference
| Model | Purpose | Timestamps |
|---|---|---|
| [[Models#Entry\|Entry]] | Movies and TV series | Yes |
| [[Models#Season\|Season]] | TV seasons | Yes |
| [[Models#Episode\|Episode]] | TV episodes | Yes |
| [[Models#EntrySeen\|EntrySeen]] | Movie watch records | No |
| [[Models#EpisodeSeen\|EpisodeSeen]] | Episode watch records | Yes |
| [[Models#Media\|Media]] | Polymorphic image records | Yes |
| [[Models#Collection\|Collection]] | TMDB + user collections | Yes |
| [[Models#UserCollectionEntry\|UserCollectionEntry]] | Collection ↔ Entry join | No |
| [[Models#Credit\|Credit]] | Cast/crew credits | Yes |
| [[Models#People\|People]] | Person profiles | Yes |
| [[Models#EntryId\|EntryId]] | External IDs (TMDB, IMDB, TVDB) | No |
| [[Models#EntryRating\|EntryRating]] | Multi-source ratings | No |
| [[Models#EntryGenre\|EntryGenre]] | Entry ↔ Genre join | No |
| [[Models#EntryNetwork\|EntryNetwork]] | Entry ↔ Network join | No |
| [[Models#Genre\|Genre]] | Genres (seeded from TMDB) | No |
| [[Models#Network\|Network]] | TV networks | No |
| [[Models#Franchise\|Franchise]] | Franchise grouping (unused) | No |
| [[Models#Video\|Video]] | Discovery videos | Yes |
| [[Models#VideoEntry\|VideoEntry]] | Video ↔ Entry join | Yes |
## Important Files
- `backend/config/database.js` — Sequelize connection
- `backend/config/migrations.js` — Startup column migrations
- `backend/config/relations.js` — All associations
- `backend/config/dataInitializer.js` — Genre seeding
- `backend/config/sequelizeHooks.js` — Collection order hooks
- `backend/models/` — All model definitions
- `backend/models/` — All Sequelize model definitions