How jjoenasr/fastapi-ecommerce works
Project Type and Purpose
• This is a backend‐only, full‑stack API service for an e‑commerce platform, implemented twice: one version uses a relational database (SQLite via SQLModel), the other uses MongoDB (Beanie ODM).
• Core capabilities: user auth, product CRUD, order processing, stock management, search & pagination, image uploads, and an AI‑powered RAG chatbot.
File Structure
• Two sibling directories:
• ecommerce‑rdb/ – SQLModel + SQLite version
• ecommerce‑mongodb/ – Beanie + MongoDB version
• Common files in each implementation:
• main.py – FastAPI app bootstrap
• database.py – DB connection & session/engine setup
• models.py – ORM/ODM models (User, Product, Order, etc.)
• routes/ (auth.py, store.py, chat.py) – API endpoint definitions
• utils.py – common helpers (JWT creation, hashing, pagination)
• seed.py – optional data seeder using models & datasets/faq.csv
• requirements.txt – dependency list
• datasets/faq.csv – seed data for the RAG chatbot.
Extract Insights from README
• Two interchangeable backends sharing identical route contracts.
• Security: bcrypt password hashing, JWT tokens, OAuth2 dependencies, CORS middleware.
• External dependency: Google Gemini API (via GOOGLE_API_KEY) to power the RAG chatbot.
Main Components
a. External Clients
• Web/Mobile clients or API tools hitting the HTTP interface (port 8000).
b. API Gateway / FastAPI Layer
• Entry point: main.py → includes routers: auth, store, chat.
c. Route Controllers
• AuthController, StoreController, ChatController – HTTP-to-service translation.
d. Service / Business Logic Layer (optionally show as part of controllers or separate)
• JWT management, order processing workflows, pagination & filtering logic.
e. Data Access Layer
• Relational version: SQLModel sessions, SQLite engine.
• Document version: Beanie ODM, MongoClient connection.
f. Databases
• SQLite (ecommerce‑rdb)
• MongoDB (ecommerce‑mongodb)
g. External Services
• Google Gemini API for AI‑RAG chatbot.
h. Supporting Modules
• Utils (hashing, token utils, common exceptions)
• Seed Script & FAQ dataset (initial data import)
i. Infrastructure / DevOps (optional)
• Environment variables: SECRET_KEY, ACCESS_TOKEN_EXPIRE_MINUTES, MONGO_URI
• Uvicorn server, Docker (if used), CI/CD pipelines.
Relationships & Data Flows
• Client → FastAPI (HTTP request)
• FastAPI router → service logic → data access layer → Database
• Chat route → RAG service → External Gemini API → returns answer to client
• Seed script → reads CSV → models → inserts into DB
• Utils module injected via dependencies (e.g., Depends(get_db)).
Architectural Patterns & Design Principles
• Layered Architecture: Presentation (routes) → Business Logic → Data Access
• Dependency Injection: FastAPI Depends for DB sessions and auth
• Single Responsibility: each route file handles one domain (auth, store, chat)
• Asynchronous IO in MongoDB version (Beanie + async routes) versus sync in SQLite version
• Environment‑driven configuration for flexibility.
Technologies & Frameworks
• FastAPI, Uvicorn
• SQLModel (rdb) / Beanie (mongodb)
• SQLite / MongoDB
• Python 3.8+
• bcrypt, PyJWT, OAuth2 (security)
• Pandas or CSV reader in seed.py
• Google Gemini API client