# am17jx/real-estate-market architecture

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

- Interactive diagram: https://gitdiagram.com/am17jx/real-estate-market
- Repository: https://github.com/am17jx/real-estate-market

## Overview

Step 1: Identify Project Type and Purpose
- This is a standalone backend RESTful API (“Real Estate Market API”) built with Node.js and Express.js.
- It exposes endpoints for user management, property listings, categories, and tags.
- Data persistence is handled by PostgreSQL via Sequelize ORM, with schema managed by migrations and seeders.
- Authentication is JWT-based, with role-based access control (user, admin, owner-company).

Step 2: Analyze File Structure
- Top-level:
- index.js / server.js / app.js: bootstrap the Express app, load middleware, start HTTP server
- config/: DB connection and environment config
- db.js: Sequelize instance
- migrations/ & seeders/: DB schema evolution and initial data
- model/: Sequelize models and associations
- Controller/: request handlers, business logic, validation
- routers/: route definitions mapping HTTP paths to controllers
- utils/: shared utilities (e.g., async error wrapper)
- Key patterns:
- MVC-inspired structure: routers → controllers → models
- Layered architecture: middleware (auth) sits before controllers
- Centralized ORM configuration and data-access in models/index.js
- Data validation via Yup in controller validators

Step 3: Extract Insights from README
- Technology stack: Node.js, Express.js, PostgreSQL, Sequelize, JWT, bcryptjs, Yup, Fuse.js
- Features: CRUD for properties, advanced fuzzy search, pagination, RBAC
- DB schema: five main entities (users, companies, categories, tags, properties) + join table (property_tags)

a. Main Components
1. Client / API Consumer
- Could be a web frontend, mobile app, or API client
2. API Gateway (Express.js Server)
- HTTP request handling, routing (routers)
- Middleware layers: logging, JSON parsing, authentication
3. Controllers & Validation
- Business logic for each resource (user, property, category, tag)
- Input validation modules (Yup-based)
4. Services / Models (Sequelize)
- ORM layer for DB interactions
- Model definitions and associations
5. Database (PostgreSQL)
- Tables: users, companies, categories, tags, properties, property_tags
6. External Services (optional)
- Email/SMS providers (if implemented later)
- Third-party search or caching

b. Relationships and Data Flow
Client → API Gateway (HTTP request)
API Gateway → Authentication Middleware → Routers → Controllers
Controllers → Sequelize Models (CRUD operations)
Models → Database (SQL queries)
Database → Models → Controllers → API Gateway → Client (HTTP response)
- Role-based access filter in auth middleware between Router and Controller.

c. Architectural Patterns & Design Principles
- Layered Architecture
- Separation of Concerns: routers handle path matching, controllers handle logic, models handle data persistence
- Use of ORM (Sequelize) abstracts SQL
- Migration-based DB versioning
- Validation as a cross-cutting concern
- JWT stateless authentication

d. Key Technologies & Frameworks
- Node.js & Express.js (API layer)
- Sequelize ORM (data access)
- PostgreSQL (relational DB)
- JSON Web Tokens (auth)
- Yup (validation)
- Fuse.js (fuzzy search implementation in controller logic)

- Since this is a backend-only service:
- API server layers instead of frontend UI
- Environment config & secrets (e.g., .env → loaded at startup)
- Migration and seeder pipeline feeding into DB
- Deployment units (e.g., Docker container, load balancer) if relevant

Step 6: Diagramming Guidelines
- “API Layer” zone: Middleware, Routers, Controllers
- “Data Layer” zone: ORM, DB
- “Utility Layer” zone: Validation, Error handling, Logging
##### Interactions
- JWT issuance at login
- Role checks before protected endpoints
- Fuzzy search flow with Fuse.js inside propertyController

Step 7: Emphasize Detail
- Modules (e.g., catchAsync utility feeding into controllers)
- Sequelize Model associations (users ↔ properties, properties ↔ categories, many-to-many via property_tags)
- Migration/seed execution path (CLI → migrations → seeders → DB)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client
    Client["API Consumer"]:::external

    %% Configuration
    subgraph "Configuration" 
        Env[".env"]:::config
        ConfigFile["config/config.js"]:::config
    end

    %% Server Bootstrap
    subgraph "Server Bootstrap"
        Index["index.js"]:::gateway
        App["app.js"]:::gateway
        Server["server.js"]:::gateway
    end

    %% API Layer
    subgraph "API Layer"
        Logging["Logging Middleware"]:::gateway
        JSONParser["JSON Body Parser"]:::gateway
        AuthMiddleware["Auth Middleware (JWT & RBAC)"]:::gateway

        subgraph "Routers"
            UserRouter["User Routes"]:::gateway
            PropRouter["Property Routes"]:::gateway
            CatRouter["Category Routes"]:::gateway
            TagRouter["Tag Routes"]:::gateway
        end

        subgraph "Controllers"
            AuthCtrl["Auth Controller"]:::gateway
            UserCtrl["User Controller"]:::gateway
            CatCtrl["Category Controller"]:::gateway
            PropCtrl["Property Controller"]:::gateway
            TagCtrl["Tag Controller"]:::gateway
        end
    end

    %% Utility Layer
    subgraph "Utility Layer"
        Validation["Yup Validators"]:::util
        CatchAsync["catchAsync Utility"]:::util
        FuseSearch["Fuse.js Search Logic"]:::util
    end

    %% Data Layer
    subgraph "Data Layer"
        ORMInit["Sequelize Instance"]:::db
        subgraph "Models & Associations"
            UsersModel["UserModel"]:::db
            CompanyModel["CompanyModel"]:::db
            CategoriesModel["CategoriesModel"]:::db
            TagModel["TagModel"]:::db
            PropertyModel["PropertyModel"]:::db
            PropTagsModel["Property_Tags Model"]:::db
        end
        Migrations["Migrations"]:::db
        Seeders["Seeders"]:::db
        Postgres["PostgreSQL Database"]:::db
    end

    %% Data Flow
    Client -->|HTTP Request| Index
    Index --> App
    App --> Server
    Server --> Env
    Server --> ConfigFile

    Server --> Logging
    Logging --> JSONParser
    JSONParser --> AuthMiddleware
    AuthMiddleware --> UserRouter
    AuthMiddleware --> PropRouter
    AuthMiddleware --> CatRouter
    AuthMiddleware --> TagRouter

    UserRouter --> UserCtrl
    PropRouter --> PropCtrl
    CatRouter --> CatCtrl
    TagRouter --> TagCtrl
    %% auth login
    UserRouter --> AuthCtrl

    AuthCtrl -->|issues JWT| UserCtrl

    %% Utilities
    UserCtrl --> Validation
    CatCtrl --> Validation
    PropCtrl --> Validation
    TagCtrl --> Validation

    UserCtrl --> CatchAsync
    CatCtrl --> CatchAsync
    PropCtrl --> CatchAsync
    TagCtrl --> CatchAsync

    PropCtrl --> FuseSearch

    %% Data Access
    AuthCtrl --> UsersModel
    UserCtrl --> UsersModel
    CatCtrl --> CategoriesModel
    PropCtrl --> PropertyModel
    PropCtrl --> CategoriesModel
    PropCtrl --> TagModel
    TagCtrl --> TagModel

    UsersModel --> ORMInit
    CompanyModel --> ORMInit
    CategoriesModel --> ORMInit
    TagModel --> ORMInit
    PropertyModel --> ORMInit
    PropTagsModel --> ORMInit

    ORMInit --> Postgres
    Migrations --> Postgres
    Seeders --> Postgres

    %% Migrations/Seeders
    Server --> Migrations
    Server --> Seeders

    %% Click Events
    click Index "https://github.com/am17jx/real-estate-market/blob/main/index.js"
    click App "https://github.com/am17jx/real-estate-market/blob/main/app.js"
    click Server "https://github.com/am17jx/real-estate-market/blob/main/server.js"
    click Env "https://github.com/am17jx/real-estate-market/blob/main/.env"
    click ConfigFile "https://github.com/am17jx/real-estate-market/blob/main/config/config.js"
    click ORMInit "https://github.com/am17jx/real-estate-market/blob/main/db.js"
    click UsersModel "https://github.com/am17jx/real-estate-market/blob/main/model/usermodel.js"
    click CompanyModel "https://github.com/am17jx/real-estate-market/blob/main/model/CompanyModel.js"
    click CategoriesModel "https://github.com/am17jx/real-estate-market/blob/main/model/categories.js"
    click TagModel "https://github.com/am17jx/real-estate-market/blob/main/model/tagModel.js"
    click PropertyModel "https://github.com/am17jx/real-estate-market/blob/main/model/propertymodel.js"
    click PropTagsModel "https://github.com/am17jx/real-estate-market/blob/main/model/property_tags.js"
    click Migrations "https://github.com/am17jx/real-estate-market/tree/main/migrations/"
    click Seeders "https://github.com/am17jx/real-estate-market/tree/main/seeders/"
    click UserRouter "https://github.com/am17jx/real-estate-market/blob/main/routers/userroutes.js"
    click PropRouter "https://github.com/am17jx/real-estate-market/blob/main/routers/Propertyroutes.js"
    click CatRouter "https://github.com/am17jx/real-estate-market/blob/main/routers/categoriesroute.js"
    click TagRouter "https://github.com/am17jx/real-estate-market/blob/main/routers/tagrouter.js"
    click AuthCtrl "https://github.com/am17jx/real-estate-market/blob/main/Controller/authController.js"
    click UserCtrl "https://github.com/am17jx/real-estate-market/blob/main/Controller/userController.js"
    click CatCtrl "https://github.com/am17jx/real-estate-market/blob/main/Controller/categoryController.js"
    click PropCtrl "https://github.com/am17jx/real-estate-market/blob/main/Controller/propertyController.js"
    click TagCtrl "https://github.com/am17jx/real-estate-market/blob/main/Controller/TgasController.js"
    click Validation "https://github.com/am17jx/real-estate-market/blob/main/Controller/propertyValidation.js"
    click CatchAsync "https://github.com/am17jx/real-estate-market/blob/main/utils/catchAsync.js"

    %% Styles
    classDef gateway fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef util fill:#FFE5B4,stroke:#FF8C00,color:#FF8C00;
    classDef db fill:#DFF2BF,stroke:#4F8A10,color:#4F8A10;
    classDef config fill:#FFF5BA,stroke:#F5C518,color:#A67C00;
    classDef external fill:#E8E8E8,stroke:#666,color:#333;
```
