A robust production-ready backend API built with NestJS, designed for enterprise-grade Software-as-a-Service applications.
Quick guide to set up and run the platform.
graph TD
A[Client Request] --> B[Global Guards: JWT & RBAC]
B --> C[Global Interceptors: Logging/Transform]
C --> D[Feature Module: Controller]
D --> E[Service Layer: Business Logic]
E --> F[Repository Layer: Prisma ORM]
F --> G[(PostgreSQL: Row Level Isolation)]
subgraph Async Operations
E --> H[BullMQ / Redis]
H --> I[Worker: Email/Billing]
end
| Component | Implementation | Industry Value |
|---|---|---|
| Data Isolation | Row-Level Multi-tenancy | Essential for GDPR/CCPA compliance in SaaS |
| Security Architecture | JWT + RBAC + Passport.js | Protects enterprise data against unauthorized access |
| Scalability | Redis Queue + BullMQ Workers | Handles high-volume traffic without system degradation |
| Maintainability | Clean Architecture & NestJS DI | Reduces long-term technical debt and operational costs |
- Clean Architecture: Clear separation between presentation, business, and data layers
- Dependency Injection: NestJS DI system for testable, maintainable code
- Module Organization: Feature-based modules for scalability
- Repository Pattern: Abstracted data access through Prisma
- DTO Validation: Comprehensive input validation and transformation
- Error Handling: Centralized error handling with proper HTTP status codes
- Language: TypeScript 5.0+
- Runtime: Node.js 18+
- Framework: NestJS 10+ (Enterprise-grade backend framework)
- Database: PostgreSQL 15+ (ACID compliance, row-level security)
- ORM: Prisma 5+ (Type-safe database access)
- Authentication: JWT with Passport.js (Stateless authentication)
- Authorization: Role-based access control (RBAC)
- Queue System: Redis + BullMQ (Background job processing)
- Validation: class-validator + class-transformer
- Containerization: Docker 24+ with multi-stage builds
- Testing: Jest 29+ with Supertest for E2E testing
- Logging: Winston structured logging
- User registration and login
- JWT-based authentication
- Role-based access control (Admin/User)
- Password hashing with bcrypt
- Organization-based data isolation
- Row-level multi-tenancy
- Users belong to organizations
- Users: Manage organization users
- Organizations: Admin-only management
- Projects: Organization-specific projects
- Tasks: Project-specific tasks with assignment
All modules include:
- Pagination
- Filtering
- Sorting
- Input validation
- Error handling
- Asynchronous email sending simulation (logged to console)
- Job retries and failure handling (simplified)
- Logging
- Mocked Stripe payment processing
- Webhook endpoint for payment events
- Unit tests for services
- Basic e2e test for authentication
Create a .env file in the root directory:
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/portfolio_saas?schema=public"
# JWT
JWT_SECRET="your-secret-key"
JWT_EXPIRES_IN="1h"
# Redis
REDIS_URL="redis://localhost:6379"
# Email (simulation)
EMAIL_HOST="smtp.gmail.com"
EMAIL_PORT=587
EMAIL_USER="your-email@gmail.com"
EMAIL_PASS="your-password"
# Payment (Stripe sandbox)
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."- Node.js (v18+)
- PostgreSQL
- Redis
- Docker (optional)
-
Clone the repository
git clone <repository-url> cd portfolio-saas-backend
-
Install dependencies
npm install
-
Set up database
# Start PostgreSQL and Redis locally or via Docker docker run --name postgres -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password -e POSTGRES_DB=portfolio_saas -p 5432:5432 -d postgres:15 docker run --name redis -p 6379:6379 -d redis:7-alpine -
Configure environment
cp .env.example .env # Edit .env with your values -
Run database migrations
npm run prisma:migrate npm run prisma:generate
-
Start the application
npm run start:dev
The API will be available at http://localhost:3000.
- Build and run with Docker Compose
docker-compose up --build
This will start the app, PostgreSQL, and Redis.
POST /auth/register
Content-Type: application/json
{
"email": "user@example.com",
"password": "password123",
"firstName": "John",
"lastName": "Doe",
"organizationId": "org-id"
}POST /auth/login
Content-Type: application/json
{
"username": "user@example.com",
"password": "password123"
}Response:
{
"access_token": "jwt-token-here"
}POST /organizations
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "My Company"
}GET /organizations?page=1&limit=10&name=search
Authorization: Bearer <token>POST /projects
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "Website Redesign",
"description": "Redesign company website"
}GET /projects?page=1&limit=10&name=search
Authorization: Bearer <token>POST /tasks
Authorization: Bearer <token>
Content-Type: application/json
{
"title": "Design homepage",
"description": "Create new homepage design",
"projectId": "project-id"
}GET /tasks?page=1&limit=10&status=pending
Authorization: Bearer <token>POST /payments/create-intent
Content-Type: application/json
{
"amount": 1000,
"currency": "usd"
}POST /payments/webhook
Content-Type: application/json
Stripe-Signature: <signature>
{
"type": "payment_intent.succeeded",
"data": { ... }
}- Row-based multi-tenancy: Each table includes
organizationIdfor data isolation - Pros: Simple, scalable, no schema duplication
- Cons: Requires careful query filtering
- JWT with Passport: Industry standard, stateless
- Role-based: Simple ADMIN/USER roles
- Password hashing: bcrypt for security
- BullMQ with Redis: Reliable queue system
- Email simulation: Logs instead of actual sending for demo
- Mocked Stripe: Simulates real payment processing
- Webhook handling: Basic event processing
- PostgreSQL: Robust, ACID compliant
- Prisma: Type-safe ORM, migrations, schema management
- NestJS modules: Feature-based organization
- Dependency injection: Testable, maintainable code
- Validation: class-validator for input validation
The platform implements a Shared Database, Shared Schema strategy. Data isolation is enforced at the service level through mandatory organizationId filters in every query. This approach balances cost-efficiency with the scalability required for high-growth startups.
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Coverage
npm run test:covThis application is containerized with Docker and can be deployed to any cloud platform supporting Docker containers (AWS ECS, Google Cloud Run, Azure Container Instances, etc.).
For production:
- Use environment-specific configs
- Set up proper database migrations
- Configure monitoring and logging
- Implement rate limiting
- Add API versioning
- Set up CI/CD pipeline
- Follow the existing code style
- Write tests for new features
- Update documentation
- Use meaningful commit messages
Patrick - Computer Engineer To view other projects and portfolio details, visit: https://pklavc.github.io/projects.html
This project demonstrates advanced backend development capabilities for enterprise SaaS applications.
14bbba43 (Clean up project structure and update documentation) 87cf431c
======= <<<<<<< HEAD
=======
14bbba43 (Clean up project structure and update documentation) 87cf431c