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 Python CLI tool that instantly scaffolds a professional, industry-standard FastAPI + MongoDB project โ€” including optional JWT authentication, Docker containerization, and test scaffolding.


โšก Installation

pip install fastapi-mongo-setup

๐Ÿ› ๏ธ Usage

Interactive Mode (Recommended)

Just run the command and answer the prompts:

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

Use command-line flags directly:

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

๐Ÿ“ 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 loader
    โ”œโ”€โ”€ utils/
    โ”‚   โ”œโ”€โ”€ db.py           # Async Motor connection manager
    โ”‚   โ””โ”€โ”€ helpers.py      # ObjectId serialization helpers
    โ”œโ”€โ”€ tasks/
    โ”‚   โ”œโ”€โ”€ router.py       # GET / POST / DELETE endpoints
    โ”‚   โ”œโ”€โ”€ schemas.py      # Pydantic validation models
    โ”‚   โ””โ”€โ”€ service.py      # Database CRUD logic
    โ””โ”€โ”€ auth/               # ๐Ÿ” (with --auth)
        โ”œโ”€โ”€ router.py       # /auth/register, /auth/login, /auth/me
        โ”œโ”€โ”€ schemas.py      # UserCreate, UserLogin, Token models
        โ”œโ”€โ”€ service.py      # Create user, find user by email
        โ”œโ”€โ”€ dependencies.py # JWT creation & verification
        โ””โ”€โ”€ utils.py        # Password hashing with bcrypt

๐Ÿš€ Running Your Generated Project

With Docker (Recommended) ๐Ÿณ

docker-compose up --build

Without Docker

pip install -r requirements.txt
python main.py

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

๐Ÿงช Testing (with --test)

Run the pre-built test suite:

pytest

The generated tests cover:

  • โœ… Root endpoint health check
  • โœ… Task CRUD operations (create, list, delete)
  • โœ… Auth registration & login flow (with --auth)
  • โœ… Unauthorized access protection (with --auth)

Linting

ruff check .       # Check for issues
ruff format .      # Auto-format code

๐Ÿณ Docker Setup (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

๐Ÿ— Why this exists?

Setting up Motor (async MongoDB driver) with FastAPI typically requires repetitive boilerplate: connection managers, lifespans, ObjectId serialization, JWT auth plumbing, Docker configs, and test infrastructure. This tool does all of that for you in one command, providing a clean, modular architecture designed for scaling.

๐Ÿค 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.6.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_mongo_setup-0.6.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_mongo_setup-0.6.0.tar.gz
  • Upload date:
  • Size: 14.5 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.6.0.tar.gz
Algorithm Hash digest
SHA256 2f5fa3ef2c82b8885444c6291ba77dabcd9a8d40907aeb06aaed3adfab35d677
MD5 2e4d59bc83b0b77ce9314bf9f2564348
BLAKE2b-256 297c9451b674100fb618e4e00b96be403cfa45e550209eb15530ac9c8788b44c

See more details on using hashes here.

File details

Details for the file fastapi_mongo_setup-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_mongo_setup-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21f5cfd91352406d8acf12372b9896be107b3f9d83b842084b0ef4dc696ae07e
MD5 89a1fdfba664ba1a358578985c38b007
BLAKE2b-256 010b52f41b8bf99bed392993ca176a9992e4b2375b828c3f80684bc7bcaae692

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