JK
Projects
JK
>LOADING // LOADING
INITIALIZING VIEW
Built and Designed byJK
JK
>LOADING // PROJECT DETAILS
PARSING ARCHITECTURE, DIAGRAMS & API ENDPOINTS
Back to Projects
View on GitHub
Microservice Backend
Completednestjspostgrestypescriptjesteslintdockergithubactionspostmanswagger

Microservice Backend

Microservices backend built with NestJS, PostgreSQL, and Docker supporting product and order services via an API Gateway with CI/CD pipelines using GitHub Actions.

Problem & Solution

Demonstrates a production-like microservices architecture with proper separation of concerns between routing (API Gateway), domain logic (Products/Orders services), and data persistence (PostgreSQL). Each service can be built, tested, and deployed independently — suitable for small-to-medium e-commerce backends where product catalog and order management are core domains.

Features

  • Microservices architecture with Product & Order services
  • API Gateway for centralized routing
  • PostgreSQL database with Docker orchestration
  • TurboRepo-based monorepo structure
  • GitHub Actions CI/CD with automated Docker Hub deployments
  • Jest unit testing

Tech Stack

TechnologyVersionPurposeWhy Chosen
TypeScript^5.7.3Primary language for all servicesType safety, NestJS ecosystem standard
NestJS^11.0.1Application frameworkModular architecture, dependency injection, TypeScript-first
TypeORM^0.3.27ORM for PostgreSQL accessActive Record/Repository pattern, NestJS integration
PostgreSQL15Primary databaseJSONB support, ACID compliance, reliability
Docker—Containerization for all servicesConsistent environments, reproducible deployments
Turborepo^2.5.8Monorepo task orchestrationFast parallel builds, caching, npm workspaces integration
GitHub Actions—CI/CD pipeline executionFree for public repos, tight Git integration
Jest^30.0.0Unit testing frameworkNestJS default, snapshot support
Swagger^11.2.0API documentationAuto-generates OpenAPI from decorators

Architecture

Client

API Gateway

:3000

Products Service

:3001

Orders Service

:3002

PostgreSQL

:5432
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.

Core System Components

  • API Gateway for routing requests to internal services
  • Products & Orders service as independent NestJS services
  • Docker Compose orchestration
  • PostgreSQL container

Infrastructure

  • GitHub Actions CI (lint, test, build)
  • Docker Hub CD image deployment
  • Environment variable management

Project Structure

.github
workflows
apps
api-gateway
orders-service
products-service
libs
common
docker-compose.ymlOrchestrates all services and PostgreSQL database

API Endpoints

API Gateway (port 3000)

MethodRoutePurpose
GET/productsList all products
GET/products/:idGet product by ID
POST/productsCreate product
PATCH/products/:idUpdate product
DELETE/products/:idDelete product
GET/ordersList all orders
GET/orders/:idGet order by ID
POST/ordersCreate order

Products Service (port 3001)

MethodRoutePurpose
POST/productsCreate product
GET/productsList all products
GET/products/:idGet product by ID
PATCH/products/:idUpdate product
DELETE/products/:idDelete product

Orders Service (port 3002)

MethodRoutePurpose
POST/ordersCreate order
GET/ordersList all orders
GET/orders/:idGet order by ID

Database Design

product

6 cols
idPK
SERIAL
code
VARCHAR
name
VARCHAR
description
VARCHAR
price
DECIMAL
image
VARCHAR

order

4 cols
idPK
SERIAL
customer
JSONB
products
JSONB
totalAmount
DECIMAL
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.

product

ColumnTypeNotes
idPKSERIALAuto-generated primary key
codeVARCHARProduct SKU/code
nameVARCHARProduct display name
descriptionVARCHARProduct description
priceDECIMALProduct price
imageVARCHARImage URL

No explicit indexes beyond primary key. No unique constraints on code.

order

ColumnTypeNotes
idPKSERIALAuto-generated primary key
customerJSONBStores {name, phone}
productsJSONBArray of {id, name, rate, qty}
totalAmountDECIMALTotal order amount

Products stored as JSONB snapshot — intentional denormalization. No foreign keys to product table.

No foreign key relationships. Order.products is a JSONB snapshot — orders are self-contained records that do not reference the product table. Product price changes do not affect existing orders.

Key Design Decisions

Shared PostgreSQL Database

Decision: Both services connect to the same PostgreSQL instance and database (procura_db)

Why: Simplifies development and deployment — one database to manage

Benefits: Simpler infrastructure, lower resource usage, easier local development

Trade-offs: Violates database-per-service principle, creates hidden coupling, makes independent scaling harder

JSONB for Order Products

Decision: Order entity stores product data as JSONB rather than foreign key references

Why: Decouples order data from product lifecycle — orders are immutable snapshots

Benefits: Orders are self-contained, product changes don't affect historical orders

Trade-offs: No referential integrity, data duplication, no automatic product validation on order creation

API Key Authentication

Decision: Simple static API key in x-api-key header, compared against environment variable

Why: Simplest possible authentication for demonstration purposes

Benefits: Minimal implementation, no token management, no state

Trade-offs: No user identity, no fine-grained permissions, key is static and shared

HTTP-based Inter-service Communication

Decision: API Gateway communicates with downstream services via synchronous HTTP (Axios)

Why: Simplest approach for synchronous request-response pattern

Benefits: Easy to understand, debug, and test; standard HTTP tooling

Trade-offs: Synchronous coupling, no retry/circuit breaker, no async processing

Multi-stage Docker Builds

Decision: Each service uses a 3-stage Dockerfile (deps → builder → runner)

Why: Minimizes final image size and attack surface

Benefits: Production images contain only runtime dependencies and compiled code

Trade-offs: Build time for each stage, no layer caching optimization for src changes

TypeORM synchronize: true

Decision: Both services use synchronize: true in TypeORM config

Why: Auto-creates/updates database schema from entities during development

Benefits: No migration files needed, schema stays in sync with code

Trade-offs: Dangerous in production — can cause data loss on schema changes

Testing

Jest ^30.0.0 with ts-jest ^29.2.5•34 unit tests

What's Tested

  • API Gateway: root health check, all proxy operations (products + orders)
  • Products Service: controller methods (create, findAll, findOne, update, remove)
  • Products Service: service methods (create, findAll, findOne, update, remove)
  • Orders Service: controller methods (create, findAll, findOne, id conversion)
  • Orders Service: service methods (create, findAll, findOne)

Missing Coverage

  • Integration tests with real database
  • API Gateway ↔ Service integration tests
  • Middleware tests for API key validation
  • Validation error scenario tests
  • Error path tests (404, 500 scenarios)

Key Challenges

  • Coordinating multiple services while ensuring fault isolation
  • Setting up CI/CD pipelines for multi-image Docker builds
  • Managing environment variables cleanly inside Docker

Key Learnings

  • Effective use of API Gateway pattern
  • Advanced Docker container orchestration with dependencies
  • Dependency isolation using monorepo tooling (TurboRepo)