Skip to main content

A dynamic FastAPI router that automatically creates CRUD routes for your mongodb models

Project description

Instant CRUD APIs from Pydantic Models
Build a fully working FastAPI CRUD API in 10 seconds.

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

---

🚀 Features

  • Auto-generate CRUD endpoints from Pydantic models
  • Works with FastAPI
  • MongoDB support
  • Fully extendable
  • Zero boilerplate

⚡ Example

from fastapi import FastAPI
import motor.motor_asyncio
from fastapi_crudrouter_mongodb import (
    ObjectIdType,
    MongoModel,
    CRUDRouter,
)

# 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: ObjectIdType | None = None
    name: str
    email: str

# Instantiating the CRUDRouter
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)

✨ What you get instantly

HTTP Verb Path Description
GET /users List all users
POST /users Create a new user
GET /users/{id} Get a user by id
PUT /users/{id} Update a user by id
PATCH /users/{id} Partially update a user by id
DELETE /users/{id} Delete a user by id

!!!tip "No routing. No boilerplate. No repetition." The CRUDRouter automatically generates all the necessary routes for your models.


Install

Requires Python >=3.10.

 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 fastapi import FastAPI
from fastapi_crudrouter_mongodb import (
    ObjectIdType,
    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: ObjectIdType | 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)

🧠 Why

FastAPI is amazing, but CRUD is repetitive.

This library removes boilerplate so you can focus on your product

📚 Documentation

👉 Full docs: Right here


Automatic OpenAPI Documentation

By default, the CRUDRouter automatically documents all generated routes in accordance with the OpenAPI specification.

CRUDRouter OpenAPI schema

The CRUDRouter can dynamically generate comprehensive documentation based on the provided models.

CRUDRouter OpenAPI schema details


Credits :

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_crudrouter_mongodb-1.0.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_crudrouter_mongodb-1.0.0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_crudrouter_mongodb-1.0.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_crudrouter_mongodb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f23c34b3dc01ff3121c2e0544e62e16cec902efdc0b3db106d325425d6a6c00c
MD5 8169151e068bf789089f1a4e23ff4d4b
BLAKE2b-256 296a7553e234495c7bcb623c585caeadf1bdbdea1336ae9c2cd52bbc034c23e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_crudrouter_mongodb-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c13c0c43d97441b502c79c98a8cdd61c7e7a4df5610ff41ef455067fc3bf5d1d
MD5 aa98db4f0f61579288670c65bbcd747d
BLAKE2b-256 f8ced81a3d07935603cef4599c601382d9c8583a7389c85ecb204d4be8b09af2

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