How bashleigh/nestjs-blog works
1. System shape
- This repo is a single NestJS backend application for a basic blog API.
- It is organized as a modular monolith with three main feature areas: auth, user, and blog.
2. Runtime and deployment model
- The application runs on Node.js/NestJS.
- In development it uses nodemon / HMR (main.hmr.ts, nodemon.json, webpack.config.js) for reloads.
- In production it is intended to run from the compiled dist output.
- The backing datastore is MySQL, started locally via docker-compose.yml.
3. Composition root
- src/app.module.ts is the top-level wiring point.
- It aggregates the feature modules and config/database setup, so the architecture is centered around Nest’s dependency injection and module boundaries.
4. Auth subsystem
- src/auth contains the authentication boundary:
- auth.controller.ts exposes auth endpoints.
- auth.service.ts contains the login/token logic.
- jwt.strategy.ts integrates JWT validation with Nest/Passport.
- interfaces/jwt-payload.interface.ts defines token payload shape.
- This module is the security gate for protected routes.
5. User subsystem
- src/user is the user-domain module with controller and service layers.
- It likely supports user creation/retrieval and is used both directly and indirectly by auth.
- The user service is a distinct domain service, not just an auth helper, which keeps identity data separate from token handling.
6. Blog subsystem
- src/blog is the core content module.
- blog.controller.ts handles blog endpoints.
- blog.service.ts contains blog business logic.
- slug.provider.ts is a dedicated helper for generating URL-friendly slugs, showing that slug creation is treated as domain logic rather than a controller concern.
- src/config/slugify.ts and the slug provider together suggest titles are normalized into stable blog identifiers.
7. Persistence layer
- src/entities contains the TypeORM persistence model:
- base.entity.ts for shared entity fields
- user.entity.ts
- blog.entity.ts
- These entities are the bridge between Nest services and MySQL through TypeORM.
- The data flow is therefore: controller → service → TypeORM entity/repository → MySQL.
8. API/data contract layer
- src/models holds API-facing shapes for auth, blog, and user payloads.
- This separation indicates the repo distinguishes between database entities and response/request models, which helps keep external API contracts decoupled from persistence details.
9. Shared utilities
- src/paginate provides reusable pagination interfaces and helpers.
- This is a cross-cutting concern used by listing endpoints, most likely blog and user queries.
- It acts as a small shared infrastructure layer inside the app.
10. Configuration boundaries
- src/config/database.ts configures TypeORM/MySQL connectivity.
- src/config/jwt.ts holds JWT settings.
- src/config/slugify.ts centralizes slug formatting behavior.
- .env.dist implies runtime behavior is environment-driven, with config values injected rather than hardcoded.
11. Testing structure
- Unit tests live beside services/providers as *.spec.ts.
- End-to-end coverage lives under test/ with app.e2e-spec.ts.
- This shows the repo validates both isolated domain logic and full HTTP/module wiring.
12. Architecture summary for a diagram
- The clean diagram is: HTTP clients → Nest controllers → domain services → TypeORM entities/repositories → MySQL.
- Cross-cutting concerns sit alongside that path: JWT auth/strategy protects endpoints, slug generation supports blog creation, and pagination supports list responses.
- The app is a conventional NestJS layered backend with clear separation between API, domain logic, and persistence.
Main entry points: app.module.ts App module · index.ts Entities · auth.module.ts Auth module · blog.module.ts Blog module
Components 25
Entrypoints
Application bootstrap and development reload entrypoints.
- App bootstrap · Production NestJS entrypoint that starts the HTTP server.
src/main.ts - HMR bootstrap · Development entrypoint for hot reload and nodemon-based iteration.
src/main.hmr.ts - App module · Top-level Nest module that wires feature modules and infrastructure.
src/app.module.ts
Feature modules
Main NestJS domain boundaries.
- Auth module · Authentication boundary for login and JWT validation.
src/auth/auth.module.ts - Auth API · Exposes authentication endpoints.
src/auth/auth.controller.ts - Auth service · Implements login and token logic.
src/auth/auth.service.ts - JWT strategy · Validates JWTs and integrates protected-route auth.
src/auth/jwt.strategy.ts - User module · User-domain boundary for account data and retrieval.
src/user/user.module.ts - User API · Exposes user-related endpoints.
src/user/user.controller.ts - User service · Handles user-domain business logic.
src/user/user.service.ts - Blog module · Core content boundary for posts and slugs.
src/blog/blog.module.ts - Blog API · Exposes blog endpoints.
src/blog/blog.controller.ts - Blog service · Implements blog creation and content logic.
src/blog/blog.service.ts - Slug helper · Generates stable URL-friendly slugs for blog content.
src/blog/slug.provider.ts
Shared layer
Cross-cutting configuration, models, and helpers.
- Database config · Configures MySQL/TypeORM connectivity.
src/config/database.ts - JWT config · Holds JWT settings used by auth.
src/config/jwt.ts - Slug config · Centralizes slug formatting behavior.
src/config/slugify.ts - API models · Request/response shapes for auth, blog, and user APIs.
src/models/index.ts - Pagination · Reusable pagination interfaces and utilities.
src/paginate/pagination.ts
Persistence
TypeORM entities and database connectivity.
- Entities · Persistence models bridging services and MySQL.
src/entities/index.ts - User entity · Database model for users.
src/entities/user.entity.ts - Blog entity · Database model for blog posts.
src/entities/blog.entity.ts - Base entity · Shared entity fields and persistence defaults.
src/entities/base.entity.ts
Runtime infra
Local database and deployment support.
- MySQL · Backing datastore used by the application.
docker-compose.yml - Webpack HMR · Development bundling used for hot reload.
webpack.config.js
Connections 30
- App bootstrap → App module: boots · Production startup loads the Nest composition root.
- HMR bootstrap → Webpack HMR: uses · Development startup depends on the HMR webpack build.
- HMR bootstrap → App module: boots · Hot-reload entry still initializes the same application module.
- App module → Auth module: imports · Wires the authentication boundary into the application.
- App module → User module: imports · Wires the user boundary into the application.
- App module → Blog module: imports · Wires the content boundary into the application.
- App module → Database config: configures · Provides database connectivity to feature modules.
- App module → JWT config: configures · Supplies JWT settings to authentication.
- Auth module → Auth API: contains · Exposes auth HTTP endpoints.
- Auth module → Auth service: provides · Encapsulates authentication logic.
- Auth module → JWT strategy: uses · Attaches JWT validation for protected routes.
- Auth service → User service: reads users · Authentication depends on user-domain data.
- Auth service → API models: returns · Uses API-facing auth shapes.
- JWT strategy → JWT config: reads · Uses JWT configuration for validation.
- User module → User API: contains · Exposes user HTTP endpoints.
- User module → User service: provides · Encapsulates user business logic.
- User service → Entities: persists via · User operations flow through TypeORM models.
- Blog module → Blog API: contains · Exposes blog HTTP endpoints.
- Blog module → Blog service: provides · Encapsulates blog business logic.
- Blog module → Slug helper: uses · Blog behavior depends on slug generation.
- Blog service → Entities: persists via · Blog operations flow through TypeORM models.
- Blog service → Pagination: lists with · Blog listing endpoints use shared pagination.
- Slug helper → Slug config: reads · Slug formatting is centralized in config.
- Entities → Base entity: extends · Domain entities share common base fields.
- Entities → User entity: includes · Persistence model for users.
- Entities → Blog entity: includes · Persistence model for blog posts.
- Entities → MySQL: stored in · TypeORM maps entities to MySQL tables.
- API models → Auth module: shapes · Auth request/response contracts.
- API models → User module: shapes · User API contracts.
- API models → Blog module: shapes · Blog API contracts.