Skip to main content

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

Monthly Downloads Shield Badge Weekly Downloads Shield Badge Python Version Python Version

---

Documentation: https://pierrod.github.io/fastapi-crudrouter-mongodb-doc/

Source Code: https://github.com/pierrod/fastapi-crudrouter-mongodb

Credits :


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.

CRUDRouter full OpenAPI image

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-crudrouter-mongodb-0.0.6.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

fastapi_crudrouter_mongodb-0.0.6-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-crudrouter-mongodb-0.0.6.tar.gz.

File metadata

File hashes

Hashes for fastapi-crudrouter-mongodb-0.0.6.tar.gz
Algorithm Hash digest
SHA256 84ae9e5ee838b33e83fae9a799bd516b05d379fe5b892e3c3ba17214d0748652
MD5 8f3d4ab6f5f4ddd7efac1d6633bee727
BLAKE2b-256 9e8ddf55807ba10da4bb81206a480bdf1834c93c3310192b426d3cc93d8d64e2

See more details on using hashes here.

File details

Details for the file fastapi_crudrouter_mongodb-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_crudrouter_mongodb-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e0c0193d2a06bd54e57421cf8d3134a54f7b8ab92d7e020eae8d1fffc2da5c8a
MD5 881c02190aa494cf0a7e91d82507c663
BLAKE2b-256 6be4ba5a14b57dce17da6d099434628d0a6239949f2b8cdb8c50a82f69edac85

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page