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 : mclate github guide
Tired of rewriting generic CRUD routes? Need to rapidly prototype a feature for a presentation or a hackathon? Thankfully, fastapi-crudrouter-mongodb has your back.
As an extension to the APIRouter included with FastAPI, the FastAPI CRUDRouter will automatically generate and document your CRUD routes for you, all you have to do is pass your model and maybe your database connection.
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.
from datetime import datetime
from typing import List, Optional, Union
from fastapi import FastAPI
from pydantic import Field
from fastapi_crudrouter_mongodb import CRUDRouter, MongoModel, MongoObjectId, CRUDLookup
import motor.motor_asyncio
# Database connection using motor
client = motor.motor_asyncio.AsyncIOMotorClient(
"mongodb://localhost:27017/local")
db = client.local
# Models
class MessageModel(MongoModel):
id: Optional[MongoObjectId] = Field()
message: str
user_id: MongoObjectId
created_at: Optional[str] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
updated_at: Optional[str] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
class AddressModel(MongoModel):
id: Optional[MongoObjectId] = Field()
street: str
city: str
state: str
zip: str
class UserModel(MongoModel):
id: Optional[MongoObjectId] = Field()
name: str
email: str
addresses: Optional[List[AddressModel]]
messages: Optional[Union[List[MessageModel], MessageModel]] = None
# 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
messages_lookup = CRUDLookup(
model=MessageModel,
collection_name="messages",
prefix="messages",
local_field="_id",
foreign_field="user_id"
)
users_controller = CRUDRouter(
model=UserModel,
db=db,
collection_name="users",
lookups=[messages_lookup],
prefix="/users",
tags=["users"],
)
# Instantiating the FastAPI app
app = FastAPI()
app.include_router(users_controller)
OpenAPI Support
By default, all routes generated by the CRUDRouter will be documented according to OpenAPI spec.
Below are the default routes created by the CRUDRouter shown in the generated OpenAPI documentation.
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.0.4.tar.gz
.
File metadata
- Download URL: fastapi-crudrouter-mongodb-0.0.4.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7944abeca9d9e0c5e37a996f8900d7a06cca6769ceb356e739f13006f008baeb |
|
MD5 | 4d083576f5a7abb1c1ceb5ae60604b46 |
|
BLAKE2b-256 | 42b9cd640552b9ee7dcb1246539fca8de2c768850285b78f75fd68a97e5cd5c2 |
File details
Details for the file fastapi_crudrouter_mongodb-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: fastapi_crudrouter_mongodb-0.0.4-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7711c11a17a34ebab6ec52d0f1adc689d059a0f024a6fbc1e8348e8d34c9ea5d |
|
MD5 | b98c286cee2a33ae4fcd89102c5d2345 |
|
BLAKE2b-256 | cb4e4e301a0cd51d92abd25bc305fdec58edbf577755bf1efd2521358c5fab1c |