Updated the Obsidian docs-vault

This commit is contained in:
Mohammed Abad committed 2026-04-22 14:48:43 +01:00
1 parent 72fd77ee01
commit 260282a0e2
36 files changed
+3497 -144

No files matched your search

+88 -16
View File
@@ -1,26 +1,98 @@
---
title: Backend
tags:
- backend
- express
- node
---
# Backend
## Overview
Node.js + Express REST API with Sequelize ORM and MySQL database.
Node.js + Express REST API serving the [[Frontend]] via JSON. Uses Sequelize ORM with a MySQL database named **Showzy**.
## Key Endpoints
## Entry Point — `server.js`
- `GET/PUT /settings` — App settings
- `GET/DELETE /maintenance/orphan-files` — Orphan file scan/cleanup
- `GET/DELETE /maintenance/orphan-media-records` — Orphan DB record scan/cleanup
- `GET /statistics/watch-weekday` — Watchday distribution (filtered to EXACT accuracy)
- CRUD endpoints for entries, seasons, episodes, collections, people
1. Creates Express app with `body-parser` (JSON + URL-encoded)
2. CORS: allows all origins (`*`)
3. All API routes mounted under `/api/v1` via `routes/routes.js`
4. Error handler middleware at the end (see [[Error Handling]])
5. Debug crash route at `/killer` (outside `/api/v1`)
6. Sequelize sync: `sequelize.sync({ alter: doUpdate })` where `doUpdate = false`
7. Calls `initializeData()` after sync — seeds 27 TMDB genres if needed
8. Listens on port from `settings.json` (default `4000`)
9. Process-level handlers for `unhandledRejection` and `uncaughtException`
## Important Files
## Project Structure
- `server.js` — Express app setup
- `config/database.js` — Sequelize/MySQL connection
- `config/relations.js` — Model associations
- `controllers/` — Route handlers
- `services/` — Business logic
- `models/` — Sequelize models
```
backend/
├── server.js Entry point
├── package.json Dependencies
├── nodemon.json Dev config (watches .js, .json)
├── settings.json Runtime config (see [[Env]])
├── config/
│ ├── database.js Sequelize instance → MySQL "Showzy"
│ ├── relations.js All model associations → [[Database#Relationships]]
│ ├── dataInitializer.js Genre seeding (27 TMDB genres)
│ └── sequelizeHooks.js UserCollectionEntry hooks (order management)
├── controllers/ Route handlers (14 controllers)
├── services/ Business logic (16 services) → [[Services]]
├── models/ Sequelize models (17 models) → [[Models]]
├── routes/ Express routers (14 route files) → [[API Reference]]
├── errors/ Custom error classes → [[Error Handling]]
├── middleware/
│ └── errorHandler.js Central error catching
├── mappers/
│ └── entryMapper.js Entry response mapping
└── utils/
├── asyncWrapper.js Async route error wrapper
└── moment.js Bundled Moment.js 2.29.0
```
## Known Issues
## Middleware Pipeline
- Maintenance service `deleteOrphanFiles` uses synchronous `fs.existsSync` in a loop
```
Request → body-parser → CORS → /api/v1/routes → Controller → Service → Response
↓ (on error)
errorHandler middleware
```
## Configuration
Backend reads `settings.json` at startup. See [[Env]] for all fields.
Key config files:
- `config/database.js` — Sequelize connection to MySQL `Showzy` on `localhost` (root/password)
- `config/relations.js` — All model associations (see [[Database#Relationships]])
- `config/dataInitializer.js` — Seeds genre table with 27 TMDB genres
- `config/sequelizeHooks.js` — Auto-manages `order` field on [[Collections|UserCollectionEntry]]
## Key Patterns
### Async Wrapper
All route handlers are wrapped with `asyncWrapper()` which catches rejected promises and forwards to `next(err)` → [[Error Handling]].
### Singleton Services
All services use the singleton pattern — `if (!Class.instance) Class.instance = this`.
### Lazy Refresh
[[Services|EntryManager]] and [[Services|PeopleManager]] implement lazy refresh: data is only re-fetched from TMDB if `last_fetch` is null or older than 24 hours.
### Media Pipeline
See [[Media System]] for the complete download → optimize → serve flow.
## See Also
- [[API Reference]] — All endpoints
- [[Database]] — Schema and models
- [[Services]] — Business logic
- [[Error Handling]] — Error classes and middleware
- [[TMDB Integration]] — External API
- [[Media System]] — Image handling