Skip to content

About

FastAPI integration for PyMongo: Pydantic-native BSON types, client lifespan/DI, settings helpers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

fastapi-pymongo

FastAPI integration for PyMongo:

  • PyObjectId: Pydantic-native ObjectId. Validates from ObjectId or hex string, serializes to a string in JSON mode (Python-mode dumps keep the ObjectId, so documents round-trip through insert_one), correct OpenAPI/JSON schema.
  • MongoSettings: pydantic-settings base class for Mongo URI and database name. Subclass to add app settings.
  • mongo_lifespan / get_client / get_database: client lifecycle wired to FastAPI's lifespan, exposed via Depends().
  • beanie_lifespan (optional, fastapi-pymongo[beanie]): runs Beanie's init_beanie alongside the rest of fastapi_pymongo, for an ODM on top.

Install

pip install fastapi-pymongo

Optional Beanie integration:

pip install "fastapi-pymongo[beanie]"

Optional cursor pagination via fastapi-pagination:

pip install "fastapi-pymongo[pagination]"

Examples

Development

pip install -e ".[dev]"
pytest            # unit and integration tests (perf is deselected by default)
pytest -m perf    # PyObjectId overhead benchmarks
ruff check src tests examples
mypy src

Integration tests need a running MongoDB; set MONGODB_URI (default mongodb://localhost:27017). Run the full suite with a local server, for example via mongodb-runner start --topology standalone.

Documentation

Sphinx docs (furo theme) under docs/source:

pip install -e ".[docs]"
sphinx-build -b html docs/source docs/build
open docs/build/index.html

About

FastAPI integration for PyMongo: Pydantic-native BSON types, client lifespan/DI, settings helpers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages