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.
🚀 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.
The CRUDRouter can dynamically generate comprehensive documentation based on the provided models.
Credits :
-
Base projet and idea : awtkns
-
Convert _id to id (for previous versions of Pydantic) : mclate github guide
-
For Pydantic v2 : Stackoverflow
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_crudrouter_mongodb-1.0.1.tar.gz.
File metadata
- Download URL: fastapi_crudrouter_mongodb-1.0.1.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9064cb51896ec8b1aab8dcfc09b599a98369f97d91e49d1c54c3bbef002e4df
|
|
| MD5 |
9f4b1a97462d9b6eefc2cabd7b41ef89
|
|
| BLAKE2b-256 |
2cd1fc95c24981fe32c002a852ee921ead9956bfba860af6ce98f5c74a545443
|
File details
Details for the file fastapi_crudrouter_mongodb-1.0.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_crudrouter_mongodb-1.0.1-py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f51e770bb190349b0bbd6e19be759dca11de9dfe5a86d8a5f730d3bd0651598
|
|
| MD5 |
ef585217e548cbd327f4066c26bb12d8
|
|
| BLAKE2b-256 |
b6bfe5563c9fb525bfa2623b7297b604fdba48f29efc7f336654db6f5f64ef37
|