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!
- 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
- 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
- Docker Desktop installed and running
- Python 3.11+ (for local development)
- Git
- VS Code with DocumentDB extension (recommended)
git clone https://github.com/documentdb/fast-api-sample.git
cd fast-api-sample
# 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
andcustomers
and confirm.
- For each collection, click the
Import
button on the top-right corner. - Import the
/scripts/sample_customers.json
file in thecustomers
collection and/scripts/sample_products.json
file in theproducts
collection.
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
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
- Final Product: http://localhost
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
GET /api/v1/products
- List all products (with pagination)GET /api/v1/products/{id}
- Get a specific productPOST /api/v1/products
- Create a new productPUT /api/v1/products/{id}
- Update a productDELETE /api/v1/products/{id}
- Delete a product
GET /api/v1/orders
- List all ordersGET /api/v1/orders/{id}
- Get a specific orderPOST /api/v1/orders
- Create a new orderPUT /api/v1/orders/{id}/status
- Update order status
GET /api/v1/customers
- List all customersGET /api/v1/customers/{id}
- Get a specific customerPOST /api/v1/customers
- Create a new customer
# 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
The DocumentDB for VS Code extension provides a powerful, integrated way to explore and manage your database directly within VS Code.
-
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
)
- Open VS Code Extensions (
-
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
-
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.
Connect to DocumentDB using mongosh:
mongosh localhost:27017 -u docdb_user -p docdb_password \
--authenticationMechanism SCRAM-SHA-256 \
--tls --tlsAllowInvalidCertificates
- Install dependencies:
cd backend
pip install -r requirements.txt
- 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
- Run the FastAPI app:
cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# All services
docker-compose logs -f
# Specific service
docker-compose logs -f backend
docker-compose logs -f documentdb
docker-compose down
docker-compose up --build
.
βββ 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
- β BSON document storage
- β MongoDB wire protocol compatibility
- β Complex aggregation pipelines
- β Indexing strategies
- β Full-text search (optional)
- β Vector search for AI workloads (optional)
- β Async/await patterns
- β Pydantic validation
- β Automatic OpenAPI documentation
- β Dependency injection
- β Error handling
- β CORS configuration
- β Multi-stage builds
- β Environment variable management
- β Volume mounting for development
- β Health checks
- β Network isolation
This project is licensed under the MIT License.
- Extension Marketplace: Install DocumentDB for VS Code
- GitHub Repository: microsoft/vscode-documentdb
- Extension Documentation: Official Docs
- Quickstart Guide: VS Code Extension Quick Start
- Blog Post: Meet the DocumentDB Extension
- Local Guide: docs/DOCUMENTDB_EXTENSION_GUIDE.md