How raulzamoraperez/backend_bv works
Project Type and Purpose
• This is a Node.js/TypeScript–based backend REST API for a “Biblioteca Virtual UDEA” (virtual library) system.
• It follows an MVC‐style, layered architecture, using Express for HTTP handling and Sequelize (MySQL) for persistence.
High-Level Architectural Layers
a. Client Layer
• External consumer (e.g., front-end SPA, mobile app, Postman) issues HTTP requests to the API.
b. Presentation Layer (Express Server)
• src/server.ts / src/app.ts bootstrap the Express application.
• Middleware stack (CORS, body parsing, validation, file upload with Multer, JWT authentication, error handling) processes each incoming request.
c. Routing Layer
• src/routes defines routers for each domain (Administrador, Alumno, Book, Area, Materia, Matricula, Semestre, AuthAlumno).
• Requests are dispatched to the appropriate Controller based on HTTP method + URL.
d. Controller Layer
• src/controllers/*Controller.ts implement endpoint handlers, performing request→DTO mapping, basic input checks, and orchestrating calls to services or ORM models.
e. Service / Business Logic Layer (optional in some controllers)
• For “Administrador”, there’s a dedicated Service class (Administrador.Service.ts). Other controllers can be refactored similarly.
f. Data Access Layer
• src/data/mysql/udea.database.ts sets up a Sequelize connection to the MySQL database.
• src/data/mysql/models/*.ts define Sequelize models (Administrador, Alumno, Area, Libro, Materia, Matricula, Semestre).
• Migrations (migrations/*.js) manage schema evolution via sequelize-cli.
g. Utilities & Helpers
• src/config holds environment loading (envs.ts), CORS setup, JWT adapter, Bcrypt adapter.
• src/helpers handles email notifications and token generation.
• src/errors and src/middleware provide structured error and exception handling.
Database & Persistence
• MySQL instance named “biblioteca_virtual_udea.”
• Sequelize ORM interfaces with tables corresponding to domain models.
• Migration scripts (migrations folder) create/alter tables on deploy or schema change.
External Dependencies & Tools
• JWT for authentication tokens.
• Bcrypt for password hashing.
• Multer for file (book/upload, cover) uploads.
• dependency-check-bin folder contains OWASP dependency-check CLI (for local security scans), not part of request flow but part of CI.
Diagramming the System
• Clients (Browser/Mobile/API Consumer)
• Express HTTP Server
• Middleware Layer (CORS, Validation, Auth, Upload, Error Handler)
• Controllers
• Services (if any)
• Sequelize ORM / Models
• MySQL Database
• Helpers (Email Service, Token Generator)
• Configuration Module (env loader, JWT adapter, Bcrypt adapter)
• Migration Tool (sequelize-cli) & dependency-check (side tool)
Interactions & Data Flow
1) Client → HTTP Server: Incoming request
2) HTTP Server → Middleware: CORS → Body Parser → Validation → File Upload → JWT Auth → Error Handler
3) Middleware → Router: Route lookup (e.g., /books, /auth/admin)
4) Router → Controller: Controller.method(req, res)
5) Controller → Service or directly to Model:
• Service calls Helpers (e.g., token generation, email)
• Model (via Sequelize) executes SQL queries
6) Model → Database: SQL SELECT/INSERT/UPDATE/DELETE
7) Database → Model → Controller → HTTP Server → Client: Response payload
Architectural Patterns & Principles
• Layered architecture (presentation, routing, controllers, services, data).
• Separation of concerns: each folder (config, controllers, data, helpers, middleware) has a clear responsibility.
• Use of DTOs (src/controllers/…/dtos) to validate and type-safely map incoming payloads.
• Configuration and adapters abstract environment variables, JWT, Bcrypt.
• Migrations ensure schema versioning and reproducibility.
Final Advice
• End-to-end path of a request through middleware, routing, business logic, and persistence.
• Don’t forget auxiliary flows: file upload via Multer, email sending via helper, token generation via JWT adapter.
• Important interactions (e.g., “Generates JWT token,” “Validates input DTO,” “Executes SQL query”).