# eslamtaha-dev/mern-ecommerce-backend architecture

> Architecture diagram of the GitHub repository eslamtaha-dev/mern-ecommerce-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eslamtaha-dev/mern-ecommerce-backend
- Repository: https://github.com/eslamtaha-dev/mern-ecommerce-backend

## Overview

#### Purpose and entry points
This is a TypeScript/Express REST backend for an e-commerce domain: authentication, catalog/category management, user roles, product images, and orders. `server.ts` is the long-running Node entry point; it validates environment, connects MongoDB, and listens. `app.ts` composes the Express application and is also the serverless-compatible handler.

#### HTTP composition and request boundary
`app.ts` applies Helmet, CORS, rate limiting, lazy MongoDB connection, route mounting under `/api/v1`, and terminal error handling. Resource routers define public versus protected endpoints and arrange validation/auth/controller stages:
- `routes/auth.route.ts`
- `routes/product.route.ts`
- `routes/order.route.ts`

Requests are validated before controller/database access through Zod middleware; async failures converge on a centralized HTTP error mapper:
- `utils/validate.ts`
- `utils/errorHandler.ts`
- `utils/catchAsync.ts`

#### Authentication and authorization
Registration/login create JWTs; user passwords are bcrypt-hashed and hidden by default at the model layer. Protected requests verify the token and reload the user from MongoDB, then role guards enforce admin-only mutations. User role changes preserve the invariant that at least one admin remains.
- `controllers/auth.controller.ts`
- `middlewares/auth.middleware.ts`
- `models/user.model.ts`
- `controllers/user.controller.ts`

#### Domain persistence and identifiers
Mongoose models persist to MongoDB/Atlas. Categories and users use ObjectIds. Products and orders additionally expose atomic sequential numeric IDs, generated through a counter collection, which defines their URL lookup contract.
- `models/product.model.ts`
- `models/order.model.ts`
- `models/category.model.ts`
- `models/counter.model.ts`
- `utils/getNextSequence.ts`

#### Catalog and media lifecycle
Product and category controllers implement catalog reads and admin CRUD. Product listing supports pagination, sorting, search, and category filtering. Image-bearing product writes cross a media boundary: Multer accepts image data in memory, Cloudinary receives a stream, and returned URL/public ID are stored with the product. Replaced/deleted assets are cleaned up without making cleanup failure fail the main request.
- `controllers/product.controller.ts`
- `controllers/category.controller.ts`
- `middlewares/upload.middleware.ts`
- `utils/cloudinary.ts`

#### Order integrity flow
Authenticated customers submit product references and quantities, not trusted prices. The order controller loads products from MongoDB, derives item pricing and total server-side, then persists the order. Owners can access their orders; admins can list all orders and transition status.
- `controllers/order.controller.ts`
- `models/order.model.ts`
- `validators/order.validator.ts`

#### Runtime and external infrastructure
Node.js 18+, Express 5, TypeScript, Mongoose, and MongoDB provide the service runtime and data store. Required external services are MongoDB (typically Atlas) and Cloudinary; JWT signing depends on `JWT_SECRET`. Environment validation fails boot early in the process path, while `app.ts`’s guarded connection approach supports serverless invocations. Demo data is initialized by `scripts/seed.ts`.

Main entry points: [`routes/auth.route.ts`](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/routes/auth.route.ts) (API routers), [`app.ts`](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/app.ts) (Express app), [`models/product.model.ts`](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/product.model.ts) (Catalog models), [`models/user.model.ts`](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/user.model.ts) (User model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_server{{"Node server<br/>entrypoint<br/>[server.ts]"}}
  node_seed["Demo data seed<br/>script<br/>[seed.ts]"]
end

subgraph group_http["HTTP API"]
  node_app["Express app<br/>[app.ts]"]
  node_routers["API routers<br/>route layer<br/>[auth.route.ts]"]
  node_validation["Zod validation<br/>middleware<br/>[validate.ts]"]
  node_errorflow["Async error mapping<br/>error boundary<br/>[errorHandler.ts]"]
end

subgraph group_security["Identity &amp; Access"]
  node_auth["JWT auth guard<br/>middleware<br/>[auth.middleware.ts]"]
  node_authcontroller["Auth controller<br/>[auth.controller.ts]"]
  node_usercontroller["User administration<br/>controller<br/>[user.controller.ts]"]
end

subgraph group_domain["Domain Services"]
  node_catalog["Catalog controllers"]
  node_upload["Image upload<br/>middleware"]
  node_orders["Order controller"]
end

subgraph group_data["Persistence"]
  node_users[("User model<br/>Mongoose model<br/>[user.model.ts]")]
  node_catalogmodels[("Catalog models<br/>Mongoose models<br/>[product.model.ts]")]
  node_ordermodel[("Order model<br/>Mongoose model<br/>[order.model.ts]")]
  node_counter[("Sequence counter<br/>Mongoose model<br/>[counter.model.ts]")]
end

subgraph group_external["External Services"]
  node_mongodb[("MongoDB / Atlas<br/>database")]
  node_cloudinary["Cloudinary<br/>media service"]
end

node_server -->|"starts"| node_app
node_server -->|"connects at boot"| node_mongodb
node_app -.->|"lazy connection"| node_mongodb
node_app -->|"mounts /api/v1"| node_routers
node_app -->|"terminal handler"| node_errorflow
node_routers -->|"validates"| node_validation
node_routers -->|"protects routes"| node_auth
node_routers -->|"auth endpoints"| node_authcontroller
node_routers -->|"catalog endpoints"| node_catalog
node_routers -->|"order endpoints"| node_orders
node_auth -->|"reloads user"| node_users
node_authcontroller -->|"creates/authenticates"| node_users
node_usercontroller -->|"updates roles"| node_users
node_catalog -->|"image-bearing writes"| node_upload
node_upload -->|"streams images"| node_cloudinary
node_catalog -->|"CRUD and listing"| node_catalogmodels
node_catalogmodels -->|"allocates product ID"| node_counter
node_orders -->|"loads trusted products"| node_catalogmodels
node_orders -->|"persists derived order"| node_ordermodel
node_ordermodel -->|"allocates order ID"| node_counter
node_users -->|"persists"| node_mongodb
node_catalogmodels -->|"persists"| node_mongodb
node_ordermodel -->|"persists"| node_mongodb
node_counter -->|"persists"| node_mongodb
node_seed -->|"initializes"| node_mongodb

click node_server "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/server.ts"
click node_app "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/app.ts"
click node_routers "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/routes/auth.route.ts"
click node_validation "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/utils/validate.ts"
click node_errorflow "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/utils/errorHandler.ts"
click node_auth "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/middlewares/auth.middleware.ts"
click node_authcontroller "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/auth.controller.ts"
click node_usercontroller "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/user.controller.ts"
click node_catalog "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/product.controller.ts"
click node_upload "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/middlewares/upload.middleware.ts"
click node_orders "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/order.controller.ts"
click node_users "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/user.model.ts"
click node_catalogmodels "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/product.model.ts"
click node_ordermodel "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/order.model.ts"
click node_counter "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/counter.model.ts"
click node_seed "https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/scripts/seed.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server,node_seed toneBlue
class node_app,node_routers,node_validation,node_errorflow toneAmber
class node_auth,node_authcontroller,node_usercontroller toneMint
class node_catalog,node_upload,node_orders toneRose
class node_users,node_catalogmodels,node_ordermodel,node_counter toneIndigo
class node_mongodb,node_cloudinary toneTeal
```

## Components

### Runtime

- [Node server](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/server.ts): entrypoint · Validates environment, connects MongoDB, and listens. · `server.ts`
- [Demo data seed](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/scripts/seed.ts): script · Initializes demo data. · `scripts/seed.ts`

### HTTP API

- [Express app](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/app.ts): application · Serverless-compatible app composition with global middleware and error handling. · `app.ts`
- [API routers](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/routes/auth.route.ts): route layer · Public and protected auth, product, and order endpoints under /api/v1. · `routes/auth.route.ts`
- [Zod validation](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/utils/validate.ts): middleware · Rejects invalid requests before controllers access data. · `utils/validate.ts`
- [Async error mapping](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/utils/errorHandler.ts): error boundary · Async failures converge on centralized HTTP error responses. · `utils/errorHandler.ts`

### Identity & Access

- [JWT auth guard](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/middlewares/auth.middleware.ts): middleware · Verifies tokens, reloads users, and enforces roles. · `middlewares/auth.middleware.ts`
- [Auth controller](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/auth.controller.ts): controller · Registers and logs in users, issuing JWTs. · `controllers/auth.controller.ts`
- [User administration](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/user.controller.ts): controller · Manages roles while preserving at least one admin. · `controllers/user.controller.ts`

### Domain Services

- [Catalog controllers](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/product.controller.ts): controller · Product/category reads and admin CRUD, including filtered product listing. · `controllers/product.controller.ts`
- [Image upload](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/middlewares/upload.middleware.ts): middleware · Accepts product images in memory for media writes. · `middlewares/upload.middleware.ts`
- [Order controller](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/controllers/order.controller.ts): controller · Derives prices and totals from persisted products before saving orders. · `controllers/order.controller.ts`

### Persistence

- [User model](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/user.model.ts): Mongoose model · Hashes passwords and hides them by default. · `models/user.model.ts`
- [Catalog models](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/product.model.ts): Mongoose models · Products use sequential numeric IDs; categories use ObjectIds. · `models/product.model.ts`
- [Order model](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/order.model.ts): Mongoose model · Orders expose sequential numeric IDs. · `models/order.model.ts`
- [Sequence counter](https://github.com/eslamtaha-dev/mern-ecommerce-backend/blob/main/models/counter.model.ts): Mongoose model · Atomically allocates product and order numeric IDs. · `models/counter.model.ts`

### External Services

- **MongoDB / Atlas**: database
- **Cloudinary**: media service · Stores product image streams and supports asset cleanup.

## Connections

- Node server → Express app: starts
- Node server → MongoDB / Atlas: connects at boot
- Express app → MongoDB / Atlas: lazy connection
- Express app → API routers: mounts /api/v1
- Express app → Async error mapping: terminal handler
- API routers → Zod validation: validates
- API routers → JWT auth guard: protects routes
- API routers → Auth controller: auth endpoints
- API routers → Catalog controllers: catalog endpoints
- API routers → Order controller: order endpoints
- JWT auth guard → User model: reloads user
- Auth controller → User model: creates/authenticates
- User administration → User model: updates roles
- Catalog controllers → Image upload: image-bearing writes
- Image upload → Cloudinary: streams images
- Catalog controllers → Catalog models: CRUD and listing
- Catalog models → Sequence counter: allocates product ID
- Order controller → Catalog models: loads trusted products
- Order controller → Order model: persists derived order
- Order model → Sequence counter: allocates order ID
- User model → MongoDB / Atlas: persists
- Catalog models → MongoDB / Atlas: persists
- Order model → MongoDB / Atlas: persists
- Sequence counter → MongoDB / Atlas: persists
- Demo data seed → MongoDB / Atlas: initializes
