A dynamic FastAPI router that automatically creates CRUD routes for your mongodb models
Project description
⚡ Create CRUD routes with lighting speed ⚡
A dynamic FastAPI router that automatically creates CRUD routes for your Mongodb models
Documentation: https://pierrod.github.io/fastapi-crudrouter-mongodb-doc/
Source Code: https://github.com/pierrod/fastapi-crudrouter-mongodb
Credits :
-
Base projet and idea : awtkns
-
Convert _id to id (for previous versions of Pydantic) : mclate github guide
-
For Pydantic v2 : Stackoverflow
Are you exhausted from constantly rewriting basic CRUD routes? Do you find yourself needing to swiftly prototype features for presentations or hackathons? Well, rejoice! Introducing fastapi-crudrouter-mongodb, your ultimate solution.
As a complement to FastAPI's APIRouter, the FastAPI CRUDRouter for MongoDB 🌱 takes care of the heavy lifting for you. It automatically generates and documents your CRUD routes with minimal effort. Simply provide your model and your database connection, and you're good to go!
Installation
pip install fastapi-crudrouter-mongodb
Basic Usage
I will provide more examples in the future, but for now, here is a basic example of how to use the FastAPI CRUDRouter for Mongodb :seedling:.
from typing import Annotated
from fastapi import FastAPI
from fastapi_crudrouter_mongodb import (
ObjectId,
MongoObjectId,
MongoModel,
CRUDRouter,
)
import motor.motor_asyncio
# Database connection using motor
client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://localhost:27017/local")
# store the database in a global variable
db = client.local
# Database Model
class UserModel(MongoModel):
id: Annotated[ObjectId, MongoObjectId] | None = None
name: str
email: str
password: str
# Instantiating the CRUDRouter, and a lookup for the messages
# a User is a model that contains a list of embedded addresses and related to multiple messages
users_router = CRUDRouter(
model=UserModel,
db=db,
collection_name="users",
prefix="/users",
tags=["users"],
)
# Instantiating the FastAPI app
app = FastAPI()
app.include_router(users_router)
Advanced Usage
fastapi-crudrouter-mongodb offers several functionalities designed to maximize the benefits of your auto-generated CRUD routes. Here are some key highlights:
- Automatic Lookups
- Automatic Embeds
- Ability to provide Custom out schema
- Ability to Disable specific routes
- Ability to Add custom dependencies to specific routes
OpenAPI Support
"Automatic OpenAPI Documentation"
By default, the CRUDRouter automatically documents all generated routes in accordance with the OpenAPI specification.
The default routes generated by the CRUDRouter are displayed in the OpenAPI documentation generated by the system.
The CRUDRouter can dynamically generate comprehensive documentation based on the provided models.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi-crudrouter-mongodb-0.1.2.tar.gz
.
File metadata
- Download URL: fastapi-crudrouter-mongodb-0.1.2.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43c69efee69f8da68a663862228d7b7f6944a2c67c83909548890fca674f9a46 |
|
MD5 | 78a7b040ad7595e43ab059ba9e912b91 |
|
BLAKE2b-256 | 45f41b0b8d26ace4d08e97e2f6d8d42bf6ea95f82fd1500e41d5026b91759213 |
File details
Details for the file fastapi_crudrouter_mongodb-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: fastapi_crudrouter_mongodb-0.1.2-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a71a89fd464bab825dc44d6ab00ec32c7cae982f0a1a8d8b3a96e9c6faf338f |
|
MD5 | 66743a48c8e9e1f5776a56a389e78e73 |
|
BLAKE2b-256 | ea72bfa865600db01f5e32825be5341e5a2bcd974ffda423c8b78f9940094a44 |