Skip to content

documentdb/fast-api-sample

Repository files navigation

FastAPI + DocumentDB E-Commerce Demo

A modern, full-stack e-commerce application built with FastAPI and open-source DocumentDB (MongoDB-compatible), orchestrated with Docker containers.

πŸ§ͺ New to this project? Start here for step-by-step instructions!

🎯 What You'll Learn

  • Setting up a FastAPI backend and connecting it to DocumentDB using Docker Compose
  • How open-source DocumentDB enables rapid iteration and seamless migration to cloud environments
  • Using the DocumentDB for VS Code extension to explore, query, and manage your database visually

πŸ—οΈ Architecture

  • Backend: FastAPI (Python 3.11+)
  • Database: DocumentDB (MongoDB-compatible, PostgreSQL-based)
  • ODM: Beanie (async MongoDB ODM built on Pydantic)
  • Containerization: Docker & Docker Compose
  • Developer Tools: DocumentDB for VS Code extension, MongoDB Playground

πŸ“‹ Prerequisites

  • Docker Desktop installed and running
  • Python 3.11+ (for local development)
  • Git
  • VS Code with DocumentDB extension (recommended)

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/documentdb/fast-api-sample.git
cd fast-api-sample

2. Set up your DocumentDB instance

# Pull the latest DocumentDB Docker image
docker pull ghcr.io/documentdb/documentdb/documentdb-local:latest

# Tag the image for convenience
docker tag ghcr.io/documentdb/documentdb/documentdb-local:latest documentdb

# Run the container with your chosen username and password
docker run -dt -p 10260:10260 --name documentdb-container documentdb --username <YOUR_USERNAME> --password <YOUR_PASSWORD>
docker image rm -f ghcr.io/documentdb/documentdb/documentdb-local:latest
  • Click the DocumentDB icon in the VS Code sidebar
  • Click "Add New Connection"
  • On the navigation bar, click on "Connection String"
  • Paste your connection string
mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256
  • Click on the drop-down next to your local connection and select "Create Database..."
  • Enter database name and confirm (suggested: ecommerce)
  • Click on the drop-down next to your created database and select "Create Collection..."
  • Create your collections, products and customers and confirm.

3. Import data to DocumentDB

  • For each collection, click the Import button on the top-right corner.
  • Import the /scripts/sample_customers.json file in the customers collection and /scripts/sample_products.json file in the products collection.

4. Set Up Environment Variables

cp .env.example .env

In the .env file, add your connection string, database name, and DocumentDB credentials.

Afterwards, install the requirements needed for the data to migrate to the frontend.

cd backend
pip install -r requirements.txt

5. Start the Application

docker-compose up -d

This will:

  • Build the FastAPI backend container
  • Pull and start the DocumentDB container
  • Set up networking between services
  • Initialize the database connection

6. Access the Application

πŸ“š API Endpoints

Products

  • GET /api/v1/products - List all products (with pagination)
  • GET /api/v1/products/{id} - Get a specific product
  • POST /api/v1/products - Create a new product
  • PUT /api/v1/products/{id} - Update a product
  • DELETE /api/v1/products/{id} - Delete a product

Orders

  • GET /api/v1/orders - List all orders
  • GET /api/v1/orders/{id} - Get a specific order
  • POST /api/v1/orders - Create a new order
  • PUT /api/v1/orders/{id}/status - Update order status

Customers

  • GET /api/v1/customers - List all customers
  • GET /api/v1/customers/{id} - Get a specific customer
  • POST /api/v1/customers - Create a new customer

πŸ§ͺ Running Tests

# Run all tests
docker-compose exec backend pytest

# Run with coverage
docker-compose exec backend pytest --cov=app --cov-report=html

# Run specific test file
docker-compose exec backend pytest tests/test_products.py

πŸ› οΈ Development Workflow

Using the DocumentDB for VS Code Extension (Recommended)

The DocumentDB for VS Code extension provides a powerful, integrated way to explore and manage your database directly within VS Code.

Quick Setup

  1. Install the Extension:

    • Open VS Code Extensions (Ctrl+Shift+X / Cmd+Shift+X)
    • Search for "DocumentDB for VS Code"
    • Install the extension by Microsoft (ms-azuretools.vscode-documentdb)
  2. Connect to Your Local Database:

    • Click the DocumentDB icon in the Activity Bar
    • Click "Add New Connection" β†’ "Connection String"
    • Paste: mongodb://docdb_user:docdb_password@localhost:27017/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256
    • Name it "E-commerce Local Dev" and connect
  3. Explore Your Data:

    • Expand your connection to see databases and collections
    • View documents in Table, Tree, or JSON view
    • Run queries with IntelliSense support
    • Import/Export data as JSON

πŸ“– Detailed Guide: See DocumentDB Extension Guide for comprehensive documentation.

Alternative: Command Line Access

Connect to DocumentDB using mongosh:

mongosh localhost:27017 -u docdb_user -p docdb_password \
  --authenticationMechanism SCRAM-SHA-256 \
  --tls --tlsAllowInvalidCertificates

Local Development (without Docker)

  1. Install dependencies:
cd backend
pip install -r requirements.txt
  1. Start DocumentDB separately:
docker run -dt -p 27017:27017 \
  --name documentdb-local \
  ghcr.io/microsoft/documentdb/documentdb-local:latest \
  --username docdb_user --password docdb_password
  1. Run the FastAPI app:
cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

View Logs

# All services
docker-compose logs -f

# Specific service
docker-compose logs -f backend
docker-compose logs -f documentdb

Rebuild After Code Changes

docker-compose down
docker-compose up --build

πŸ“ Project Structure

.
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ main.py              # FastAPI application entry point
β”‚   β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”‚   β”œβ”€β”€ config.py        # Configuration settings
β”‚   β”‚   β”‚   └── database.py      # Database connection setup
β”‚   β”‚   β”œβ”€β”€ models/              # Beanie document models
β”‚   β”‚   β”‚   β”œβ”€β”€ product.py
β”‚   β”‚   β”‚   β”œβ”€β”€ order.py
β”‚   β”‚   β”‚   └── customer.py
β”‚   β”‚   β”œβ”€β”€ routers/             # API route handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ products.py
β”‚   β”‚   β”‚   β”œβ”€β”€ orders.py
β”‚   β”‚   β”‚   └── customers.py
β”‚   β”‚   └── schemas/             # Pydantic request/response schemas
β”‚   β”‚       β”œβ”€β”€ product.py
β”‚   β”‚       β”œβ”€β”€ order.py
β”‚   β”‚       └── customer.py
β”‚   β”œβ”€β”€ tests/                   # Test files
β”‚   β”‚   β”œβ”€β”€ conftest.py
β”‚   β”‚   β”œβ”€β”€ test_products.py
β”‚   β”‚   β”œβ”€β”€ test_orders.py
β”‚   β”‚   └── test_customers.py
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ .env.example
└── README.md

🌟 Key Features Demonstrated

DocumentDB Capabilities

  • βœ… BSON document storage
  • βœ… MongoDB wire protocol compatibility
  • βœ… Complex aggregation pipelines
  • βœ… Indexing strategies
  • βœ… Full-text search (optional)
  • βœ… Vector search for AI workloads (optional)

FastAPI Best Practices

  • βœ… Async/await patterns
  • βœ… Pydantic validation
  • βœ… Automatic OpenAPI documentation
  • βœ… Dependency injection
  • βœ… Error handling
  • βœ… CORS configuration

Docker Best Practices

  • βœ… Multi-stage builds
  • βœ… Environment variable management
  • βœ… Volume mounting for development
  • βœ… Health checks
  • βœ… Network isolation

πŸ“„ License

This project is licensed under the MIT License.

πŸ”— Resources

Official Documentation

DocumentDB for VS Code Extension

Additional Resources

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published