# jesusdev1294/api_business architecture

> Architecture diagram of the GitHub repository jesusdev1294/api_business, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jesusdev1294/api_business
- Repository: https://github.com/jesusdev1294/api_business

## Overview

##### Project Type and Purpose
- This is a Node.js-based backend API (“indicators-backend”) that runs as serverless functions (via Serverless Framework + AWS Lambda) or inside a Docker container on EC2.
- Its responsibilities are: expose HTTP endpoints (via Express wrapped in serverless-http), implement business logic in controllers (lof.controller, search.controller), and persist/query data via a relational database through a data service layer (src/db).

##### File Structure Analysis
- Top-level:
- serverless.yml – defines AWS Lambda + API Gateway resources.
- Docker-related files (Dockerfile inferred, Docker commands in README).
- src/app.js – Express application entry.
- src/controllers – HTTP request handlers.
- src/db – data-access layer (connection, init, service, models).
- src/json – sample/reference data (employees.json).
- Patterns:
- Separation of concerns: HTTP layer → Controllers → Data Service → Database.
- Use of ORM models under src/db/models.
- Configurable execution environments: serverless offline, Docker container, PM2 on EC2.

##### README Insights
- Commands to spin up locally with Serverless Offline or Docker.
- Indicates deployment targets: AWS Lambda (via Serverless Framework) and EC2 container (Docker + PM2).
- Dependencies: express, serverless-http, serverless-offline, pm2, plus a relational DB driver.

a. Main Components
- Client(s): UI, CLI tools, third-party services.
- API Gateway (AWS) – routes HTTP(s) to Lambda.
- Compute layer:
– AWS Lambda functions running Express (wrapped by serverless-http)
– Docker container (app-indicator) running the same Express app under PM2 on EC2
- Controllers (lof.controller.js, search.controller.js)
- Data Service (src/db/dataService.js) & Connection Manager (src/db/connection.js)
- Relational Database (e.g., AWS RDS / another SQL engine) with schema defined by ORM models
- Sample Data / JSON store (for local/testing)
b. Relationships / Interactions
1. Client issues HTTP request → API Gateway (or EC2 load balancer)
2. Gateway invokes Lambda / routes to EC2 container
3. Express app (app.js) receives request → dispatches to Controller
4. Controller calls Data Service → uses Connection → reads/writes via ORM models to the Database
5. Database returns data → flows back up through Service → Controller → HTTP response
c. Architectural Patterns
- Layered architecture: Presentation (HTTP/API) → Business Logic (controllers) → Data Access (service + models) → Persistence (DB)
- Serverless microservice for on-demand scaling, with a parallel Docker/EC2 deployment option for long-running processes or compatibility
- Configuration via serverless.yml and environment templates for dev/prod
d. Key Technologies
- Node.js + Express + serverless-http
- Serverless Framework + serverless-offline plugin
- Docker + PM2 (on EC2)
- ORM (e.g., Sequelize) under src/db/models
- JSON as mock/test data source

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Interfaces
    Client["Client"]:::external
    APIGW["API Gateway\n(AWS)"]:::external
    LB["Load Balancer\n(EC2)"]:::external

    %% Compute Layer
    Lambda["AWS Lambda\nExpress\n(serverless-http)"]:::compute
    EC2["EC2 Container\nDocker + PM2"]:::compute

    %% Configuration Files
    ServerlessYml["serverless.yml"]:::logic
    DotEnv[".env.template"]:::logic
    README["README.md"]:::logic
    Package["package.json"]:::logic

    %% Application Logic Layer
    App["Express App\n(src/app.js)"]:::logic
    LOF["LOF Controller\n(src/controllers/lof.controller.js)"]:::logic
    Search["Search Controller\n(src/controllers/search.controller.js)"]:::logic
    DataService["Data Service\n(src/db/dataService.js)"]:::logic
    Connection["Connection Manager\n(src/db/conecction.js)"]:::logic
    Init["Database Init\n(src/db/init.js)"]:::logic
    Index["Database Index\n(src/db/index.js)"]:::logic

    %% Persistence Layer
    association["Association Model\n(src/db/models/association.js)"]:::persistence
    baseModel["Base Model\n(src/db/models/base.js)"]:::persistence
    capitalPropio["CapitalPropio Model\n(src/db/models/capitalPropio.js)"]:::persistence
    clientes["Clientes Model\n(src/db/models/clientes.js)"]:::persistence
    tramoRenta["TramoRenta Model\n(src/db/models/tramoRenta.js)"]:::persistence
    DB["Relational Database\n(AWS RDS)"]:::persistence
    JSON["Local JSON Data\n(src/json/employess.json)"]:::persistence

    %% Client to Gateway/Load Balancer
    Client -->|"HTTP Request"| APIGW
    Client -->|"HTTP Request"| LB

    %% Gateway/Load Balancer to Compute
    APIGW -->|"Invoke"| Lambda
    LB -->|"Route"| EC2

    %% Configuration to Compute
    ServerlessYml -->|"Defines resources"| Lambda
    DotEnv -->|"Env variables"| Lambda
    DotEnv -->|"Env variables"| EC2
    README -->|"Docker/PM2 scripts"| EC2
    Package -->|"PM2 scripts"| EC2

    %% Compute to App
    Lambda -->|"runs"| App
    EC2 -->|"runs"| App

    %% App to Controllers
    App -->|"GET /lof"| LOF
    App -->|"GET /search"| Search

    %% Controllers to Data Service
    LOF -->|"calls"| DataService
    Search -->|"calls"| DataService

    %% Data Service to Connection and DB Init/Index
    DataService -->|"uses"| Connection
    Connection -->|"initializes"| Init
    Connection -->|"references"| Index

    %% Data Service to Models and Persistence
    DataService -->|"queries"| association
    DataService -->|"queries"| baseModel
    DataService -->|"queries"| capitalPropio
    DataService -->|"queries"| clientes
    DataService -->|"queries"| tramoRenta
    association -->|"SQL"| DB
    baseModel -->|"SQL"| DB
    capitalPropio -->|"SQL"| DB
    clientes -->|"SQL"| DB
    tramoRenta -->|"SQL"| DB

    %% Data Service to JSON for local/testing
    DataService -.->|"Mock Data"| JSON

    %% Click Events
    click ServerlessYml "https://github.com/jesusdev1294/api_business/blob/main/serverless.yml"
    click DotEnv "https://github.com/jesusdev1294/api_business/blob/main/src/.env.template"
    click README "https://github.com/jesusdev1294/api_business/blob/main/README.md"
    click Package "https://github.com/jesusdev1294/api_business/blob/main/package.json"
    click App "https://github.com/jesusdev1294/api_business/blob/main/src/app.js"
    click LOF "https://github.com/jesusdev1294/api_business/blob/main/src/controllers/lof.controller.js"
    click Search "https://github.com/jesusdev1294/api_business/blob/main/src/controllers/search.controller.js"
    click DataService "https://github.com/jesusdev1294/api_business/blob/main/src/db/dataService.js"
    click Connection "https://github.com/jesusdev1294/api_business/blob/main/src/db/conecction.js"
    click Init "https://github.com/jesusdev1294/api_business/blob/main/src/db/init.js"
    click Index "https://github.com/jesusdev1294/api_business/blob/main/src/db/index.js"
    click association "https://github.com/jesusdev1294/api_business/blob/main/src/db/models/association.js"
    click baseModel "https://github.com/jesusdev1294/api_business/blob/main/src/db/models/base.js"
    click capitalPropio "https://github.com/jesusdev1294/api_business/blob/main/src/db/models/capitalPropio.js"
    click clientes "https://github.com/jesusdev1294/api_business/blob/main/src/db/models/clientes.js"
    click tramoRenta "https://github.com/jesusdev1294/api_business/blob/main/src/db/models/tramoRenta.js"
    click JSON "https://github.com/jesusdev1294/api_business/blob/main/src/json/employess.json"

    %% Styles
    classDef external fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef compute fill:#90EE90,stroke:#000,stroke-width:1px
    classDef logic fill:#FFA500,stroke:#000,stroke-width:1px
    classDef persistence fill:#D3D3D3,stroke:#000,stroke-width:1px
```
