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.1.tar.gz (17.2 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.8.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_mongo_setup-0.8.1.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.1.tar.gz
Algorithm Hash digest
SHA256 2ce2b21123024e84de655e9a8afabb7f993893bfba3b857ee632839947007528
MD5 7853156fa11eb7dd935c3f09c1dce528
BLAKE2b-256 5d86c28eca35cdf2660c167ff95579d3982f2fc2d37bcd48c29fa25ee9d8937d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_mongo_setup-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 776c26590c44af47cebf3b57a4d87f045c390eb94e0c5570c16289c72a493eb0
MD5 7d98f99156d9a640e377f2abe461f4fa
BLAKE2b-256 4a648e0e4d1194eb647e5d7b8d58e184158098d3ee3a24c56fa8684977360179

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