A NestJS REST API backend service with user management, social features, and background job processing.
- NestJS 11 / TypeScript 5.7
- MySQL with TypeORM
- RabbitMQ for background jobs
- JWT authentication with Passport.js
- Docker for containerization
- Node.js 22+
- Docker & Docker Compose
# Start MySQL and RabbitMQ
docker compose up -d mysql rabbitmq
# Install dependencies
npm install
# Copy environment file
cp .env.example .env
# Run migrations
npm run migration:run
# Start development server
npm run start:dev
# Start worker (separate terminal)
npm run start:worker:devAPI available at http://localhost:3000/api/v1
Swagger docs at http://localhost:3000/docs
docker compose upsrc/
├── main.ts # API entry point
├── worker.ts # Worker entry point
├── app.module.ts # Root module
├── common/ # Shared utilities, filters, interceptors
├── database/
│ ├── entities/ # TypeORM entities
│ └── migrations/ # Database migrations
└── modules/ # Feature modules
├── auth/
├── users/
├── posts/
├── comments/
├── likes/
├── follows/
├── feed/
├── notifications/
└── ...
# Development
npm run start:dev # API with hot reload
npm run start:worker:dev # Worker with hot reload
# Production
npm run build
npm run start:prod # API
npm run start:worker # Worker
# Testing
npm run test # Unit tests
npm run test:e2e # E2E tests
npm run test:cov # Coverage report
# Code Quality
npm run lint # ESLint
npm run format # Prettier
# Database
npm run migration:generate src/database/migrations/MigrationName
npm run migration:run
npm run migration:revertSee .env.example for all available options. Required:
DATABASE_URL=mysql://user:pass@localhost:3306/db
RABBITMQ_URL=amqp://guest:guest@localhost:5672
JWT_SECRET=your-secret-key
CORS_ORIGIN=http://localhost:3000
NODE_ENV=development- URI-based versioning (
/api/v1/...) - Rate limiting (100 req/min per IP)
- Standard response format:
{ success, data, timestamp } - Pagination via
?page=1&limit=10 - JWT Bearer authentication
Firebase Cloud Messaging is used for sending push notifications to mobile devices.
- Go to Firebase Console
- Create a project or select existing one
- Go to Project Settings > Service Accounts
- Click "Generate new private key"
- Add credentials to
.env:
FIREBASE_PROJECT_ID=your-project-id
FIREBASE_CLIENT_EMAIL=[email protected]
FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"- Auto-initializes on startup (gracefully skips if not configured)
- Sends to all registered devices for a user
- Automatically removes invalid/expired tokens
- Supports custom data payloads
// Inject FCMService
constructor(private readonly fcmService: FCMService) {}
// Send notification to user
await fcmService.sendToUser(userId, {
title: 'New Comment',
body: 'Someone commented on your post',
data: { postId: '123', type: 'comment' }
})
// Send to specific device token
await fcmService.sendToToken(deviceToken, {
title: 'Hello',
body: 'Test notification'
})Users register their device tokens via the Devices module (/api/v1/devices). Tokens are stored per-user and automatically cleaned up when they become invalid.
# Build production images
docker build -f Dockerfile.prod -t app:latest .
docker build -f Dockerfile.worker -t worker:latest .
# Run with production compose
docker compose -f docker-compose.prod.yml up -d- GitHub Actions:
.github/workflows/ - GitLab CI:
.gitlab-ci.yml
Pipelines include:
- Lint and test on PRs
- Build Docker images
- Deploy to staging (main branch)
- Deploy to production (version tags)
MIT