Skip to main content

A CLI tool to auto-generate MongoDB connection boilerplates for FastAPI projects.

Project description

fastapi-mongo-setup ๐Ÿš€

PyPI version Python versions License: MIT

Stop writing MongoDB boilerplate for FastAPI! fastapi-mongo-setup is a powerful CLI tool that scaffolds production-ready FastAPI + MongoDB projects โ€” with optional JWT auth, Docker, tests, a resource generator, and auto-start support.


โšก Installation

pip install fastapi-mongo-setup

๐Ÿ› ๏ธ Usage

Interactive Mode (Recommended)

mongo-setup
Welcome to fastapi-mongo-setup!
Include JWT Authentication? (y/N): y
Include Docker setup? (y/N): y
Include Test scaffolding? (y/N): y

Flag Mode

mongo-setup                # Base setup (DB + CRUD)
mongo-setup --auth         # + JWT Authentication
mongo-setup --docker       # + Docker containerization
mongo-setup --test         # + Pytest tests & Ruff linter
mongo-setup --all          # Everything included

๐Ÿงฉ Resource Generator (NEW!)

Generate new API modules on the fly โ€” no more copy-pasting boilerplate:

mongo-setup resource products
mongo-setup resource blog_posts
mongo-setup resource orders

Each generates a complete src/<name>/ module with:

  • router.py โ€” Full CRUD endpoints (POST, GET, GET by ID, PUT, DELETE)
  • schemas.py โ€” Pydantic Create, Update, and Response models
  • service.py โ€” MongoDB CRUD operations with serialization

๐Ÿ“ Generated Project Structure

your-project/
โ”œโ”€โ”€ .env                    # Environment variables (auto-populated)
โ”œโ”€โ”€ requirements.txt        # All dependencies
โ”œโ”€โ”€ main.py                 # FastAPI entry point with MongoDB lifespan
โ”œโ”€โ”€ Dockerfile              # ๐Ÿณ (with --docker)
โ”œโ”€โ”€ docker-compose.yml      # ๐Ÿณ (with --docker)
โ”œโ”€โ”€ .dockerignore           # ๐Ÿณ (with --docker)
โ”œโ”€โ”€ pytest.ini              # ๐Ÿงช (with --test)
โ”œโ”€โ”€ .ruff.toml              # ๐Ÿงน (with --test)
โ”œโ”€โ”€ tests/                  # ๐Ÿงช (with --test)
โ”‚   โ”œโ”€โ”€ conftest.py         # Async test client setup
โ”‚   โ”œโ”€โ”€ test_tasks.py       # Task endpoint tests
โ”‚   โ””โ”€โ”€ test_auth.py        # Auth endpoint tests (with --auth)
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ config.py           # Pydantic Settings configuration
    โ”œโ”€โ”€ utils/
    โ”‚   โ”œโ”€โ”€ db.py           # Async Motor connection manager
    โ”‚   โ””โ”€โ”€ helpers.py      # ObjectId serialization helpers
    โ”œโ”€โ”€ tasks/              # Default CRUD module
    โ”‚   โ”œโ”€โ”€ router.py
    โ”‚   โ”œโ”€โ”€ schemas.py
    โ”‚   โ””โ”€โ”€ service.py
    โ”œโ”€โ”€ auth/               # ๐Ÿ” (with --auth)
    โ”‚   โ”œโ”€โ”€ router.py
    โ”‚   โ”œโ”€โ”€ schemas.py
    โ”‚   โ”œโ”€โ”€ service.py
    โ”‚   โ”œโ”€โ”€ dependencies.py
    โ”‚   โ””โ”€โ”€ utils.py
    โ””โ”€โ”€ <your_resource>/    # ๐Ÿงฉ (with resource command)
        โ”œโ”€โ”€ router.py       # Full CRUD (POST, GET, GET/:id, PUT, DELETE)
        โ”œโ”€โ”€ schemas.py      # Create, Update, Response models
        โ””โ”€โ”€ service.py      # MongoDB operations

๐Ÿš€ Quick Start

With Docker ๐Ÿณ

mongo-setup --all
docker-compose up --build

Without Docker

mongo-setup --all
pip install -r requirements.txt
python main.py

Add a New Resource

mongo-setup resource products

Then add to your main.py:

from src.products.router import router as products_router
app.include_router(products_router)

Open http://localhost:8000/docs to see your Swagger UI!


๐Ÿ” Auth Endpoints (with --auth)

Method Endpoint Description Auth Required
POST /auth/register Create a new user account โŒ
POST /auth/login Get a JWT access token โŒ
GET /auth/me Get current user's profile โœ… Bearer

๐Ÿงฉ Resource Generator Endpoints

Each mongo-setup resource <name> generates:

Method Endpoint Description
POST /<name>/ Create item
GET /<name>/ List all items
GET /<name>/{id} Get by ID
PUT /<name>/{id} Update item
DELETE /<name>/{id} Delete item

๐Ÿงช Testing (with --test)

pytest                 # Run all tests
ruff check .           # Lint code
ruff format .          # Auto-format code

๐Ÿณ Docker (with --docker)

File Purpose
Dockerfile Python 3.11-slim image with your FastAPI app
docker-compose.yml Orchestrates FastAPI + MongoDB 7 containers
.dockerignore Keeps the Docker image clean and small

๐Ÿค Contributing

Found a bug or want to request a feature? Feel free to open an issue or submit a pull request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_mongo_setup-0.8.2.tar.gz (17.2 kB view details)

Uploaded Source

File details

Details for the file fastapi_mongo_setup-0.8.2.tar.gz.

File metadata

  • Download URL: fastapi_mongo_setup-0.8.2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for fastapi_mongo_setup-0.8.2.tar.gz
Algorithm Hash digest
SHA256 e29dde8d8d3a5bab014f33a7731a02a5e3a3493851a5c0dd7dd2f7e5a5717fa6
MD5 77112fcadc28779eeeb73a2d50d3f8ea
BLAKE2b-256 4ecaa0213004c6a05557b6ef963436e9e0043047c0944b968b26f2e880f31f3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page