saas-backend-platform

Multi-Tenant SaaS Backend Platform

NestJS Node.js Version PostgreSQL License: MIT Build Status

Project Overview

A robust production-ready backend API built with NestJS, designed for enterprise-grade Software-as-a-Service applications.

Getting Started

Quick guide to set up and run the platform.

Architecture Overview

Visual Request Lifecycle

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

Engineering Impact & National Interest

| 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 |

Core Architecture Patterns

Tech Stack

Features

Authentication & Authorization

Multi-Tenancy

CRUD Modules

All modules include:

Background Processing

External Integration

Testing

Environment Variables

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_..."

How to Run Locally

Prerequisites

Local Setup

  1. Clone the repository
    git clone <repository-url>
    cd portfolio-saas-backend
    
  2. Install dependencies
    npm install
    
  3. 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
    
  4. Configure environment
    cp .env.example .env
    # Edit .env with your values
    
  5. Run database migrations
    npm run prisma:migrate
    npm run prisma:generate
    
  6. Start the application
    npm run start:dev
    

The API will be available at http://localhost:3000.

Docker Setup

  1. Build and run with Docker Compose
    docker-compose up --build
    

This will start the app, PostgreSQL, and Redis.

API Documentation

Authentication

Register

POST /auth/register
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "password123",
  "firstName": "John",
  "lastName": "Doe",
  "organizationId": "org-id"
}

Login

POST /auth/login
Content-Type: application/json

{
  "username": "user@example.com",
  "password": "password123"
}

Response:

{
  "access_token": "jwt-token-here"
}

Organizations (Admin only)

Create Organization

POST /organizations
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "My Company"
}

List Organizations

GET /organizations?page=1&limit=10&name=search
Authorization: Bearer <token>

Projects

Create Project

POST /projects
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "Website Redesign",
  "description": "Redesign company website"
}

List Projects

GET /projects?page=1&limit=10&name=search
Authorization: Bearer <token>

Tasks

Create Task

POST /tasks
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "Design homepage",
  "description": "Create new homepage design",
  "projectId": "project-id"
}

List Tasks

GET /tasks?page=1&limit=10&status=pending
Authorization: Bearer <token>

Payments

Create Payment Intent

POST /payments/create-intent
Content-Type: application/json

{
  "amount": 1000,
  "currency": "usd"
}

Webhook

POST /payments/webhook
Content-Type: application/json
Stripe-Signature: <signature>

{
  "type": "payment_intent.succeeded",
  "data": { ... }
}

Design Decisions

Multi-Tenancy

Authentication

Background Jobs

Payment Integration

Database

Architecture

Technical Note on Data Isolation

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.

Testing

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Coverage
npm run test:cov

Deployment

This 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:

  1. Use environment-specific configs
  2. Set up proper database migrations
  3. Configure monitoring and logging
  4. Implement rate limiting
  5. Add API versioning
  6. Set up CI/CD pipeline

Contributing

  1. Follow the existing code style
  2. Write tests for new features
  3. Update documentation
  4. Use meaningful commit messages

Author

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.

«««< HEAD GitHub Sponsors GitHub Sponsors

14bbba43 (Clean up project structure and update documentation) 87cf431c GitHub Sponsors ======= «««< HEAD GitHub Sponsors ======= GitHub Sponsors 14bbba43 (Clean up project structure and update documentation) 87cf431c