JK
Έργα
JK
>ΦΟΡΤΩΣΗ // ΦΟΡΤΩΣΗ
ΑΡΧΙΚΟΠΟΙΗΣΗ ΠΡΟΒΟΛΗΣ
Σχεδιάστηκε και δημιουργήθηκε απόJK
JK
>ΦΟΡΤΩΣΗ // PROJECT DETAILS
PARSING ARCHITECTURE, DIAGRAMS & API ENDPOINTS
Επιστροφή στα έργα
Προβολή στο GitHub
Backend Μικροϋπηρεσιών
Ολοκληρώθηκεnestjspostgrestypescriptjesteslintdockergithubactionspostmanswagger

Backend Μικροϋπηρεσιών

Ολοκληρωμένη αρχιτεκτονική μικροϋπηρεσιών με NestJS, PostgreSQL και Docker μέσω κεντρικού API Gateway.

Πρόβλημα & Λύση

Διαχωρισμός δρομολόγησης (API Gateway), επιχειρησιακής λογικής (ανεξάρτητες υπηρεσίες) και βάσης δεδομένων για αυτόνομη ανάπτυξη και κλιμάκωση.

Χαρακτηριστικά

  • Ανεξάρτητες μικροϋπηρεσίες για Προϊόντα και Παραγγελίες
  • API Gateway για κεντρική δρομολόγηση και ασφάλεια
  • Βάση δεδομένων PostgreSQL με ορχήστρωση Docker Compose
  • Διαχείριση monorepo με TurboRepo
  • CI/CD μέσω GitHub Actions και αυτοματοποιημένη δημοσίευση στο Docker Hub
  • Πλήρεις δοκιμές μονάδας με Jest

Τεχνολογικό stack

ΤεχνολογίαΈκδοσηΣκοπόςΓιατί επιλέχθηκε
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

Αρχιτεκτονική

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

Δομή έργου

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

API endpoints

API Gateway (θύρα 3000)

ΜέθοδοςΔιαδρομήΣκοπός
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 (θύρα 3001)

ΜέθοδοςΔιαδρομήΣκοπός
POST/productsCreate product
GET/productsList all products
GET/products/:idGet product by ID
PATCH/products/:idUpdate product
DELETE/products/:idDelete product

Orders Service (θύρα 3002)

ΜέθοδοςΔιαδρομήΣκοπός
POST/ordersCreate order
GET/ordersList all orders
GET/orders/:idGet order by ID

Σχεδιασμός βάσης δεδομένων

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

ΣτήληΤύποςΣημειώσεις
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

ΣτήληΤύποςΣημειώσεις
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.

Βασικές σχεδιαστικές αποφάσεις

Shared PostgreSQL Database

Απόφαση: Both services connect to the same PostgreSQL instance and database (procura_db)

Λόγος: Simplifies development and deployment — one database to manage

Οφέλη: Simpler infrastructure, lower resource usage, easier local development

Ανταλλάγματα: Violates database-per-service principle, creates hidden coupling, makes independent scaling harder

JSONB for Order Products

Απόφαση: Order entity stores product data as JSONB rather than foreign key references

Λόγος: Decouples order data from product lifecycle — orders are immutable snapshots

Οφέλη: Orders are self-contained, product changes don't affect historical orders

Ανταλλάγματα: No referential integrity, data duplication, no automatic product validation on order creation

API Key Authentication

Απόφαση: Simple static API key in x-api-key header, compared against environment variable

Λόγος: Simplest possible authentication for demonstration purposes

Οφέλη: Minimal implementation, no token management, no state

Ανταλλάγματα: No user identity, no fine-grained permissions, key is static and shared

HTTP-based Inter-service Communication

Απόφαση: API Gateway communicates with downstream services via synchronous HTTP (Axios)

Λόγος: Simplest approach for synchronous request-response pattern

Οφέλη: Easy to understand, debug, and test; standard HTTP tooling

Ανταλλάγματα: Synchronous coupling, no retry/circuit breaker, no async processing

Multi-stage Docker Builds

Απόφαση: Each service uses a 3-stage Dockerfile (deps → builder → runner)

Λόγος: Minimizes final image size and attack surface

Οφέλη: Production images contain only runtime dependencies and compiled code

Ανταλλάγματα: Build time for each stage, no layer caching optimization for src changes

TypeORM synchronize: true

Απόφαση: Both services use synchronize: true in TypeORM config

Λόγος: Auto-creates/updates database schema from entities during development

Οφέλη: No migration files needed, schema stays in sync with code

Ανταλλάγματα: Dangerous in production — can cause data loss on schema changes

Δοκιμές

Jest ^30.0.0 with ts-jest ^29.2.5•34 μονάδες δοκιμών

Τι δοκιμάζεται

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

Κενά κάλυψης

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

Βασικές προκλήσεις

  • Συντονισμός υπηρεσιών διατηρώντας απομόνωση σφαλμάτων
  • Ρύθμιση αποδοτικών αγωγών CI/CD για πολλαπλά Docker images

Βασικά διδάγματα

  • Μοτίβα API Gateway για ενοποιημένη πρόσβαση και προώθηση αιτημάτων
  • Δικτύωση κοντέινερ και ορχήστρωση με Docker Compose