The SQLModel CRUD Manager is a Python library that facilitates common Create, Read, Update, and Delete (CRUD) operations on SQLModel entities within a FastAPI application. This library simplifies database interactions and provides an easy-to-use interface for managing SQLModel entities.
You can install the SQLModel CRUD Manager using pip:
pip install SQLModel-CRUD-manager
Here's a simple example demonstrating how to use the SQLModel CRUD Manager within a FastAPI application:
from fastapi import APIRouter, status
from sqlmodel_crud_manager import CRUDManager
from core.sql.database import engine as db_engine
from core.sql.models import YourModel, YourModelCreate
router = APIRouter()
# Initializing CRUD Manager with YourModel model and database engine
crud = CRUDManager(YourModel, db_engine)
@router.get("/{pk}", status_code=status.HTTP_200_OK, response_model=YourModel)
def get_your_model(pk: int):
return crud.get(pk)
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=YourModel)
def create_your_model(YourModel: YourModelCreate):
return crud.create(YourModel)
@router.get("/", status_code=status.HTTP_200_OK, response_model=list[YourModel])
def list_your_model():
return crud.list()
@router.put("/", status_code=status.HTTP_200_OK, response_model=YourModel)
def update_your_model(YourModel: YourModel):
return crud.update(YourModel)
@router.delete("/{pk}")
def delete_your_model(pk: int):
return crud.delete(pk)
The CRUDManager class provides the following methods:
get(pk: int) -> ModelType
: Retrieve an object based on its primary key.get_by_ids(ids:list[int]) -> list[ModelType]
: Get a list of records matching the keys sentlist(query: QueryLike = None) -> list[ModelType]
: Get a list of records matching the query.create(object: ModelCreateType) -> ModelType
: Create a new object in the database.update(input_object: ModelType) -> ModelType
: Update an object in the database.delete(pk: int) -> ModelType
: Delete an object based on its primary key.
To use the CRUDManager class, initialize it with a model and a database engine:
from sqlmodel_crud_manager import CRUDManager
from sqlalchemy import create_engine
from sqlmodel import SQLModel
engine = create_engine("sqlite:///example.db")
# Replace `YourModel` and `YourModelCreate` with your actual model classes
class YourModelCreate(SQLModel):
name: str
phone: PhoneNumber
class YourModel(YourModelCreate, table=True):
id: int | None = Field(default=None, primary_key=True)
crud = CRUDManager(YourModel, engine)
- sqlalchemy
- sqlmodel
- fastapi
This library is licensed under the MIT License.
Contributions are welcome! Feel free to open issues or submit pull requests.
Feel free to expand on this README by adding details about specific methods, advanced usage, or any additional configurations. It's important to provide clear examples and instructions to help users quickly understand and utilize your library.