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, and a resource generator for creating new API modules instantly.


โšก 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.0.tar.gz (16.8 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.0-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_mongo_setup-0.8.0.tar.gz
  • Upload date:
  • Size: 16.8 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.0.tar.gz
Algorithm Hash digest
SHA256 c3a6681dcd44e2cd4a106d9676ed9725167cccc86adae2e15aa8b55df934fa91
MD5 c92e7643c4d86165c86b62c4fd842f4e
BLAKE2b-256 70e656e9e9c8d91da9439823da14db0f09f9dd6285a241e194324349eb102de2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_mongo_setup-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5875db00f52f08fcd6a9f767476d7a8a01b0385a8720e2a74b53f1093ce2fe66
MD5 95dc4a045685d1ae06858d519acf9d69
BLAKE2b-256 b082271b79b516454958e3bd71cf9ef74d76524837aac5a3655d0601b7fb7e4e

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