How niraj-khatiwada/ultimate-nestjs-boilerplate works
1. This repository is a NestJS backend boilerplate built around a Fastify HTTP server, with a separate worker process for background jobs. The codebase is organized as a modular monolith: the main API app, shared infrastructure services, auth, and worker queues all live in the same repo but are split by module boundaries.
2. The primary external-facing surface is in src/api, which contains REST controllers, a GraphQL resolver, and WebSocket support. The user module is especially notable because it exposes both controller and resolver entry points, while health and file are REST-oriented. This means the architecture is multi-protocol rather than REST-only.
3. Authentication is handled by a dedicated src/auth subsystem built around Better Auth. It includes auth services, a guard, and persistence entities for users, sessions, accounts, passkeys, two-factor data, and verification records. The boundary here is important: business modules consume auth through guards/decorators rather than implementing login/session logic themselves.
4. Persistence is PostgreSQL via TypeORM. The database layer includes a data-source, migrations, seeds, and shared base model classes. The presence of entity and migration files indicates a schema-first backend where data shape is managed centrally and evolved through migrations rather than ad hoc runtime creation.
5. Configuration is heavily centralized under src/config. There are dedicated config modules for app runtime, database, auth, Redis, BullMQ, mail, AWS, Sentry, throttling, and Grafana. This suggests most infrastructure dependencies are injected through configuration factories and validated at startup rather than read directly in feature code.
6. Shared infrastructure lives under src/shared and src/services. Key services include Redis-backed caching, mail delivery, Socket.io gateway support, and AWS S3 file storage. The aws service is the explicit storage boundary for file uploads, while cache and socket show Redis being reused for both application caching and websocket scaling.
7. Background jobs are handled by BullMQ in a separate worker area under src/worker. The email queue has its own module, processor, service, event types, and queue constants, which implies the main API enqueues email tasks and the worker process consumes them asynchronously. That separation matters for architecture diagrams because job execution is not done in the request thread.
8. Email delivery is split between template generation and sending. React Email templates live in src/shared/mail/templates, while build-time scripts convert them into static artifacts consumed by the backend mail service. In development, Mailpit provides the SMTP inbox, so the flow is template source -> built email artifact -> mail service -> SMTP server.
9. Cross-cutting concerns are implemented as decorators, interceptors, middleware, and guards rather than inside feature modules. Examples include Sentry interception, file upload interception, basic auth middleware, public/optional auth decorators, current-user decorators, and custom validators. This indicates a framework-heavy design where request shaping and policy enforcement happen at the edge.
10. Rate limiting and observability are first-class infrastructure pieces. Throttling uses Redis-backed guards/factories, Sentry captures runtime errors, Pino is used for logging, and Prometheus/Grafana dashboards are provisioned for server and database metrics. Swagger setup is also included, which matters because it supports API documentation and frontend code generation.
11. Internationalization is built in with generated translation artifacts and locale files for English and Spanish. The src/generated/i18n.generated.ts file and translation JSONs show that message keys are managed centrally and compiled into app-ready resources.
12. The repo is designed for containerized deployment and operational tooling. Docker Compose files cover dev and prod, there is a separate Docker image for Mailpit, PM2 config is present for process management, and GitHub Actions plus a deploy script support CI/CD. Monitoring dashboards and email tooling are also bundled, reinforcing that this is meant as a production-ready starter rather than just an app skeleton.
13. In diagram terms, the main boundaries are: API entrypoints, auth/session boundary, database persistence layer, Redis-backed shared infrastructure, async worker boundary, and external services such as S3, SMTP, Sentry, and monitoring systems. Most feature flows pass through config/infrastructure modules before reaching the domain modules, which is the key structural pattern in this repo.
Main entry points: api.module.ts API module · app.module.ts App module · config.type.ts Config layer · user.module.ts User API
Components 23
API Surface
HTTP, GraphQL, and WebSocket entrypoints
- API module · Root module for public API features
src/api/api.module.ts - User API · User endpoints exposed via controller and resolver
src/api/user/user.module.ts - Health API · Service health endpoints
src/api/health/health.module.ts - File API · File upload and file-serving endpoints
src/api/file/file.module.ts
Auth Boundary
Better Auth services and persistence
- Auth module · Authentication composition around Better Auth
src/auth/auth.module.ts - Auth guard · Protects routes and mediates auth context
src/auth/auth.guard.ts - Better Auth · Login, session, and account orchestration
src/auth/better-auth.service.ts - Auth entities · Users, sessions, accounts, passkeys, and verification records
src/auth/entities/user.entity.ts
Persistence
TypeORM database layer and schema evolution
- DB source · PostgreSQL connection and TypeORM setup
src/database/data-source.ts - Base models · Shared model inheritance for persistence entities
src/database/models/base.model.ts - Migrations · Versioned database changes and seed data
src/database/migrations/1746266963361-init.ts
Shared Infra
Config, shared services, and cross-cutting framework pieces
- App module · Top-level Nest application composition
src/app.module.ts - Main entry · Starts the HTTP server and app lifecycle
src/main.ts - GraphQL setup · GraphQL runtime wiring and schema integration
src/graphql/graphql.factory.ts - Socket module · WebSocket gateway and Redis adapter
src/shared/socket/socket.module.ts - Config layer · Centralized runtime and infrastructure configuration
src/config/config.type.ts - Cache · Redis-backed application cache
src/shared/cache/cache.module.ts - Mail service · Template rendering and outbound email delivery
src/shared/mail/mail.module.ts - S3 storage · AWS S3 boundary for uploads and object storage
src/services/aws/aws.module.ts - Throttling · Redis-backed request limiting
src/config/throttler/throttler.guard.ts - Translations · Generated and locale-specific message catalogs
src/generated/i18n.generated.ts
Async Worker
Background job processing and queue execution
- Email queue · Async email job processor and queue service
src/worker/queues/email/email.module.ts
Operations
Deployment, observability, and container tooling
- Ops tooling · Docker, CI/CD, logging, metrics, and dashboards
src/tools/grafana/dashboards/server.dashboard.json
Connections 31
- Main entry → App module: bootstraps · Runtime starts the Nest application composition
- App module → API module: loads · Application composition includes the public API surface
- App module → Auth module: loads · Application composition includes authentication
- App module → Config layer: configures · Central config is injected across the app
- App module → DB source: connects · App lifecycle initializes database access
- API module → User API: includes · User feature is part of the API surface
- API module → Health API: includes · Health endpoints are part of the API surface
- API module → File API: includes · File endpoints are part of the API surface
- API module → GraphQL setup: enables · GraphQL is wired into the API runtime
- API module → Socket module: enables · Realtime support is attached to the API runtime
- User API → Auth guard: protects · User operations rely on authentication guardrails
- User API → Better Auth: uses · User flows consume auth/session services
- User API → Auth entities: reads/writes · User flows persist auth-related records
- Auth module → Better Auth: provides · Auth module exposes Better Auth orchestration
- Auth module → Auth guard: provides · Auth module exports route protection
- Auth module → Auth entities: persists · Auth state is stored in dedicated entities
- Better Auth → DB source: queries · Auth service uses the TypeORM/PostgreSQL layer
- Auth entities → Base models: extends · Auth entities inherit shared persistence base models
- DB source → Migrations: evolves via · Schema is managed through migrations and seeds
- Config layer → Cache: feeds · Redis configuration supports shared cache and rate limiting
- Config layer → Mail service: feeds · Mail configuration drives outbound email delivery
- Config layer → S3 storage: feeds · AWS settings configure object storage access
- Config layer → Throttling: feeds · Throttling behavior is configured centrally
- Mail service → Email queue: enqueues/consumes · API mail flow delegates delivery jobs to the worker queue
- Email queue → Mail service: delivers · Worker consumes queued messages and sends mail
- Mail service → S3 storage: attaches files · Mail and file workflows can rely on shared storage services
- Cache → Socket module: scales · Redis backs websocket adapter coordination
- Cache → Throttling: backs · Redis is used for distributed rate limiting
- Translations → API module: localizes · API responses and messages use generated translations
- Ops tooling → App module: observes · Operational tooling monitors the running application
- Ops tooling → DB source: observes · Dashboards track database health and metrics