Files
Showy/backend/docs/db_architecture.md
T

463 lines
24 KiB
Markdown

# Database Architecture
> **RDBMS:** MySQL · **ORM:** Sequelize v6 · **Sync strategy:** `sequelize.sync({ alter })` at startup
---
## 1. High-Level Overview
Showy persists a personal media library (movies & TV series) sourced from TMDB.
The schema centres on the **Entry** entity — a movie _or_ TV series — and fans out into ratings, watch-history, seasons/episodes, collections, genres, networks, credits (cast/crew), and polymorphic media assets.
---
## 2. Tables & Column Reference
### 2.1 `entries`
| Column | Type | Constraints | Notes |
| ------------------ | ------------- | ------------------ | -------------------------------------- |
| `id` | INTEGER | PK, AUTO_INCREMENT | |
| `title` | VARCHAR(500) | | Display title |
| `original_title` | VARCHAR(100) | | |
| `release_date` | DATE | | Movie release or series first air date |
| `release_status` | VARCHAR(50) | | e.g. `Released`, `Returning Series` |
| `runtime` | INTEGER | | Minutes (movies only) |
| `tagline` | VARCHAR(200) | | |
| `storyline` | VARCHAR(3000) | | Synopsis / overview |
| `country` | VARCHAR(30) | | Primary production country |
| `certification` | INTEGER | | Age rating |
| `network_studio` | VARCHAR(40) | | Legacy field |
| `collection_id` | INTEGER | | Soft reference to collection |
| `max_cast` | INTEGER | DEFAULT 10 | Max cast members to display |
| `credit_fetched` | BOOLEAN | DEFAULT false | Whether credits have been fetched |
| `watchlist` | BOOLEAN | DEFAULT false | User flag |
| `favorit` | BOOLEAN | DEFAULT false | User flag |
| `hidden` | BOOLEAN | DEFAULT false | User flag |
| `show_in_calendar` | BOOLEAN | DEFAULT true | Calendar visibility |
| `auto_sync` | BOOLEAN | DEFAULT true | Auto-refresh from TMDB |
| `note` | VARCHAR(2000) | | User notes |
| `is_tvseries` | BOOLEAN | | Discriminator: movie vs. TV |
| `status` | VARCHAR(20) | | e.g. `Ended`, `Returning Series` |
| `poster_path` | VARCHAR(200) | | TMDB poster path |
| `banner_path` | VARCHAR(200) | | TMDB backdrop path |
| `date_first_seen` | DATEONLY | | First watch date (computed) |
| `date_last_seen` | DATEONLY | | Last watch date (computed) |
| `last_fetch` | DATEONLY | | Last TMDB sync timestamp |
| `createdAt` | DATETIME | Sequelize-managed | |
| `updatedAt` | DATETIME | Sequelize-managed | |
---
### 2.2 `entryIds`
One-to-one with `entries`. Stores external provider IDs.
| Column | Type | Constraints |
| ---------- | ----------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `entry_id` | INTEGER | FK → `entries.id` |
| `tmdb_id` | VARCHAR(30) | TMDB identifier |
| `imdb_id` | VARCHAR(30) | IMDb identifier |
| `tvdb_id` | VARCHAR(30) | TVDB identifier |
> `timestamps: false`
---
### 2.3 `entryRatings`
One-to-one with `entries`. Aggregated ratings from multiple sources.
| Column | Type | Constraints |
| ------------------------ | ------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `entry_id` | INTEGER | FK → `entries.id` |
| `user_rating` | FLOAT | |
| `tmdb_rating` | FLOAT | |
| `imdb_rating` | FLOAT | |
| `rotten_tomatoes_rating` | FLOAT | |
> `timestamps: false`
---
### 2.4 `entrySeens`
One-to-many from `entries`. Each row = one watch event for a movie/entry.
| Column | Type | Constraints |
| ----------- | ----------- | --------------------- |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `entry_id` | INTEGER | FK → `entries.id` |
| `date_seen` | DATE | When the user watched |
| `seen_with` | VARCHAR(50) | Companion name |
> `timestamps: false`
---
### 2.5 `seasons`
One-to-many from `entries` (TV series only).
| Column | Type | Constraints |
| --------------- | ------------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `tmdb_id` | VARCHAR(30) | |
| `imdb_id` | VARCHAR(30) | |
| `entry_id` | INTEGER | FK → `entries.id` |
| `name` | VARCHAR(500) | |
| `overview` | VARCHAR(3000) | |
| `season_number` | INTEGER | |
| `episode_count` | INTEGER | |
| `air_date` | DATE | |
| `user_rating` | FLOAT | |
| `tmdb_rating` | FLOAT | |
| `imdb_rating` | FLOAT | |
| `poster_path` | VARCHAR(200) | |
| `banner_path` | VARCHAR(200) | |
---
### 2.6 `episodes`
One-to-many from `seasons`.
| Column | Type | Constraints |
| ----------------- | ------------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `tmdb_id` | VARCHAR(30) | |
| `imdb_id` | VARCHAR(30) | |
| `tvdb_id` | VARCHAR(30) | |
| `season_id` | INTEGER | FK → `seasons.id` |
| `name` | VARCHAR(500) | |
| `overview` | VARCHAR(3000) | |
| `episode_number` | INTEGER | |
| `episode_type` | VARCHAR(60) | |
| `runtime` | INTEGER | Minutes |
| `air_date` | DATE | |
| `user_rating` | FLOAT | |
| `tmdb_rating` | FLOAT | |
| `still_path` | VARCHAR(200) | |
| `date_first_seen` | DATEONLY | |
| `date_last_seen` | DATEONLY | |
---
### 2.7 `episodeSeens`
One-to-many from both `episodes` and `entries`.
| Column | Type | Constraints |
| ------------ | ----------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `entry_id` | INTEGER | FK → `entries.id` |
| `episode_id` | INTEGER | FK → `episodes.id` |
| `date_seen` | DATE | |
| `seen_with` | VARCHAR(50) | |
| `createdAt` | DATETIME | Sequelize-managed |
| `updatedAt` | DATETIME | Sequelize-managed |
---
### 2.8 `userCollections`
| Column | Type | Constraints |
| -------------------- | ------------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `tmdb_id` | VARCHAR(30) | |
| `imdb_id` | VARCHAR(30) | |
| `tvdb_id` | VARCHAR(30) | |
| `name` | VARCHAR(150) | |
| `description` | VARCHAR(3000) | |
| `is_user_collection` | BOOLEAN | DEFAULT false |
| `user_rating` | FLOAT | |
| `poster_path` | VARCHAR(200) | |
| `backdrop_path` | VARCHAR(200) | |
| `is_local` | BOOLEAN | DEFAULT false |
---
### 2.9 `userCollectionEntries` (join table)
Many-to-many pivot between `userCollections` and `entries`.
| Column | Type | Constraints |
| -------------------- | ------- | ----------------------------------------- |
| `user_collection_id` | INTEGER | PK (composite), FK → `userCollections.id` |
| `entry_id` | INTEGER | PK (composite), FK → `entries.id` |
| `order` | INTEGER | Display position |
---
### 2.10 `genres`
| Column | Type | Constraints |
| ------ | ----------- | ---------------------------------------- |
| `id` | INTEGER | PK (TMDB genre ID, not auto-incremented) |
| `name` | VARCHAR(30) | |
> `timestamps: false` — Seeded at startup via `dataInitializer.js`.
---
### 2.11 `entryGenres` (join table)
Many-to-many pivot between `entries` and `genres`.
| Column | Type | Constraints |
| ---------- | ------- | --------------------------------- |
| `genre_id` | INTEGER | PK (composite), FK → `genres.id` |
| `entry_id` | INTEGER | PK (composite), FK → `entries.id` |
---
### 2.12 `networks`
| Column | Type | Constraints |
| ---------------- | ------------ | -------------------- |
| `id` | INTEGER | PK (TMDB network ID) |
| `name` | VARCHAR(30) | |
| `origin_country` | VARCHAR(100) | |
| `homepage` | VARCHAR(100) | |
| `logo_path` | VARCHAR(200) | |
| `poster_path` | VARCHAR(200) | |
| `backdrop_path` | VARCHAR(200) | |
---
### 2.13 `entryNetworks` (join table)
Many-to-many pivot between `entries` and `networks`.
| Column | Type | Constraints |
| ------------ | ------- | ---------------------------------- |
| `network_id` | INTEGER | PK (composite), FK → `networks.id` |
| `entry_id` | INTEGER | PK (composite), FK → `entries.id` |
| `order` | INTEGER | Display position |
---
### 2.14 `peoples`
| Column | Type | Constraints |
| ---------------------- | ------------ | -------------------------- |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `tmdb_id` | VARCHAR(100) | |
| `name` | VARCHAR(100) | |
| `birthday` | DATEONLY | |
| `deathday` | DATEONLY | NULLABLE |
| `gender` | INTEGER | TMDB gender enum |
| `biography` | TEXT | |
| `place_of_birth` | VARCHAR(100) | |
| `adult` | BOOLEAN | DEFAULT false |
| `popularity` | FLOAT | |
| `known_for_department` | VARCHAR(50) | e.g. `Acting`, `Directing` |
| `homepage` | VARCHAR(200) | NULLABLE |
| `imdb_id` | VARCHAR(20) | |
| `profile_path` | VARCHAR(200) | |
| `last_fetch` | DATEONLY | |
| `profile_update` | DATEONLY | |
| `fully_fetched` | BOOLEAN | DEFAULT false |
---
### 2.15 `credits`
One-to-many from both `entries` and `peoples`.
| Column | Type | Constraints |
| ---------------- | -------------------- | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `tmdb_credit_id` | VARCHAR(100) | |
| `adult` | BOOLEAN | DEFAULT false |
| `profile_path` | VARCHAR(200) | |
| `character` | VARCHAR(100) | Role name played |
| `order` | INTEGER | Billing order |
| `cast_type` | ENUM(`cast`, `crew`) | DEFAULT `cast` |
| `department` | VARCHAR(100) | Crew department |
| `job` | VARCHAR(100) | Crew job title |
| `entry_id` | INTEGER | FK → `entries.id` |
| `people_id` | INTEGER | FK → `peoples.id` |
---
### 2.16 `franchises`
| Column | Type | Constraints |
| ------------- | ------------ | ------------------ |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `title` | VARCHAR(50) | |
| `overview` | VARCHAR(300) | |
| `user_rating` | FLOAT | |
> `timestamps: false`
---
### 2.17 `media` (polymorphic)
Polymorphic asset table. The target entity is identified by the `(mediaable, mediaable_id)` tuple.
| Column | Type | Constraints |
| -------------- | ------------ | ------------------------------------------------------------------- |
| `id` | INTEGER | PK, AUTO_INCREMENT |
| `mediaable` | VARCHAR(255) | Discriminator: `entry`, `people`, `collection`, `season`, `episode` |
| `mediaable_id` | INTEGER | ID of the owning entity |
| `type` | VARCHAR(50) | `poster`, `backdrop`, `logo`, `still`, `profile` |
| `path` | VARCHAR(200) | Filename on disk |
| `is_local` | BOOLEAN | True if stored locally |
| `is_selected` | BOOLEAN | DEFAULT false — currently active asset |
| `is_custom` | BOOLEAN | DEFAULT false — user-uploaded |
| `is_favorit` | BOOLEAN | DEFAULT false |
---
## 3. Entity Relationship Diagram
```
┌──────────────────────────────────┐
│ franchises │
│──────────────────────────────────│
│ id (PK) │
│ title, overview, user_rating │
└────────────┬─────────────────────┘
│ 1
│
│ N
┌────────────▼─────────────────────────────────────────────────────┐
│ entries │
│──────────────────────────────────────────────────────────────────│
│ id (PK) │ title │ is_tvseries │ franchise_id (FK) │ ... │
└──┬───┬───┬──────┬────────┬────────┬──────────────────────────────┘
│ │ │ │ │ │
│ │ │ │ │ ├─── 1:1 ──► entryIds
│ │ │ │ │ ├─── 1:1 ──► entryRatings
│ │ │ │ │ │
│ │ │ │ │ ├─── 1:N ──► entrySeens
│ │ │ │ │ ├─── 1:N ──► credits ◄── N:1 ── peoples
│ │ │ │ │ ├─── 1:N ──► episodeSeens
│ │ │ │ │ │
│ │ │ │ │ ├─── M:N ──► genres (via entryGenres)
│ │ │ │ │ ├─── M:N ──► networks (via entryNetworks)
│ │ │ │ │ └─── M:N ──► userCollections (via userCollectionEntries)
│ │ │ │ │
│ │ │ │ │ 1:N
│ │ │ │ ┌─────▼──────────────┐
│ │ │ │ │ seasons │
│ │ │ │ │────────────────────│
│ │ │ │ │ id (PK) │
│ │ │ │ │ entry_id (FK) │
│ │ │ │ └─────┬──────────────┘
│ │ │ │ │ 1:N
│ │ │ │ ┌─────▼──────────────┐
│ │ │ │ │ episodes │
│ │ │ │ │────────────────────│
│ │ │ │ │ id (PK) │
│ │ │ │ │ season_id (FK) │
│ │ │ │ └─────┬──────────────┘
│ │ │ │ │ 1:N
│ │ │ │ ┌─────▼──────────────┐
│ │ │ │ │ episodeSeens │
│ │ │ │ │────────────────────│
│ │ │ │ │ episode_id (FK) │
│ │ │ │ │ entry_id (FK) │
│ │ │ │ └───────────────────-┘
│ │ │ │
│ │ │ │ ┌───────────────────┐
│ │ │ └──────────┤ media (polymorphic)│
│ │ │ │───────────────────│
│ │ │ │ mediaable │
│ │ │ │ mediaable_id │
│ │ │ └───────────────────┘
│ │ │ ▲ ▲ ▲ ▲ ▲
│ │ │ entry ─────┘ │ │ │ └───── episode
│ │ │ people ───────┘ │ └──────── season
│ │ │ collection ──────┘
```
---
## 4. Relationship Summary
| Relationship | Type | FK / Pivot | ON DELETE |
| ----------------------- | ----- | ---------------------------------------- | -------------------- |
| Franchise → Entry | 1 : N | `entries.franchise_id` | — |
| Entry → EntryId | 1 : 1 | `entryIds.entry_id` | CASCADE |
| Entry → EntryRating | 1 : 1 | `entryRatings.entry_id` | CASCADE |
| Entry → EntrySeen | 1 : N | `entrySeens.entry_id` | CASCADE |
| Entry → Season | 1 : N | `seasons.entry_id` | CASCADE |
| Season → Episode | 1 : N | `episodes.season_id` | CASCADE |
| Episode → EpisodeSeen | 1 : N | `episodeSeens.episode_id` | CASCADE |
| Entry → EpisodeSeen | 1 : N | `episodeSeens.entry_id` | CASCADE |
| Entry ↔ Genre | M : N | `entryGenres` | CASCADE |
| Entry ↔ Network | M : N | `entryNetworks` | CASCADE |
| Entry ↔ UserCollection | M : N | `userCollectionEntries` | CASCADE |
| Entry → Credit | 1 : N | `credits.entry_id` | CASCADE |
| People → Credit | 1 : N | `credits.people_id` | CASCADE |
| Media → * (polymorphic) | N : 1 | `media.mediaable` + `media.mediaable_id` | — (no FK constraint) |
---
## 5. Polymorphic Media Pattern
The `media` table uses a **polymorphic association** (no physical foreign key constraints).
Scoping is done via Sequelize's `scope` option:
```js
Entry.hasMany(Media, {
foreignKey: 'mediaableId',
constraints: false,
as: 'medias',
scope: { mediaable: 'entry' }
});
```
Supported owners: `entry`, `people`, `collection`, `season`, `episode`.
---
## 6. Sequelize Hooks
Defined in `config/sequelizeHooks.js`, the following lifecycle hooks enforce business rules:
| Hook | Model | Purpose |
| -------------- | --------------------- | ---------------------------------------------------------------- |
| `afterCreate` | `UserCollectionEntry` | Auto-assign `order` value; touch parent collection's `updatedAt` |
| `afterUpdate` | `UserCollectionEntry` | Re-order sibling entries; touch parent collection's `updatedAt` |
| `afterDestroy` | `UserCollectionEntry` | Touch parent collection's `updatedAt`; recalculate poster |
---
## 7. Data Seeding
On startup, `config/dataInitializer.js` inserts **27 TMDB genre records** into the `genres` table if they are missing. Genre IDs mirror TMDB's canonical IDs (e.g., `28 = Action`, `18 = Drama`).
---
## 8. Schema Sync Strategy
The server boots with:
```js
sequelize.sync({ alter: doUpdate })
```
- `alter: true` — Sequelize issues `ALTER TABLE` statements to reconcile model definitions with the live schema.
- `force: true` (commented out) — Would drop and recreate all tables. Reserved for development resets.
No explicit migration framework is in use; schema evolution is managed via Sequelize's auto-alter mechanism.
---
## 9. Indexing & Constraints
| Constraint Type | Table(s) | Details |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------- |
| Primary Key (auto-increment) | Most tables | Standard `id` column |
| Composite Primary Key | `entryGenres`, `entryNetworks`, `userCollectionEntries` | Prevents duplicate associations |
| Foreign Key | `seasons.entry_id`, `episodes.season_id`, `entrySeens.entry_id`, `episodeSeens.episode_id`, `episodeSeens.entry_id`, `credits.entry_id`, `credits.people_id` | Referential integrity |
| No FK constraint | `media.mediaable_id` | Polymorphic — `constraints: false` in Sequelize |
| ENUM | `credits.cast_type` | Restricted to `cast` / `crew` |