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 :

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.1.tar.gz (20.2 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.1-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_crudrouter_mongodb-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a9064cb51896ec8b1aab8dcfc09b599a98369f97d91e49d1c54c3bbef002e4df
MD5 9f4b1a97462d9b6eefc2cabd7b41ef89
BLAKE2b-256 2cd1fc95c24981fe32c002a852ee921ead9956bfba860af6ce98f5c74a545443

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_crudrouter_mongodb-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f51e770bb190349b0bbd6e19be759dca11de9dfe5a86d8a5f730d3bd0651598
MD5 ef585217e548cbd327f4066c26bb12d8
BLAKE2b-256 b6bfe5563c9fb525bfa2623b7297b604fdba48f29efc7f336654db6f5f64ef37

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