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.7.0.tar.gz (15.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.7.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_mongo_setup-0.7.0.tar.gz
  • Upload date:
  • Size: 15.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.7.0.tar.gz
Algorithm Hash digest
SHA256 f153216663b54d9029cf5f228ae3ce20ab8ed51f769b58783ae08d050c6725ec
MD5 75b8455e86c6b4ad43cfbab4e48472bb
BLAKE2b-256 887d77d7757a22179bdfe63fb3b67ad106dec57193dca3e419ba5eccc95d386a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_mongo_setup-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 141eb53ab4ed1afaf924cbce5c59466f191618fecc071ce6c2430e7f8fece233
MD5 a631b8ba66a4bfbf7c9907ca6cd59534
BLAKE2b-256 cc7645d4262d8adbb00a64115e6699318aa9dbf748826cb6ec02accdff421447

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