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.
Project description
SQLModel CRUD Manager
Introduction
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.
Installation
You can install the SQLModel CRUD Manager using pip:
pip install SQLModel-CRUD-manager
Usage
Example
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)
CRUDManager Class
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.
Initialization
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)
Requirements
- sqlalchemy
- sqlmodel
- fastapi
License
This library is licensed under the MIT License.
Contribution
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.
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
File details
Details for the file sqlmodel_crud_manager-0.1.12.tar.gz
.
File metadata
- Download URL: sqlmodel_crud_manager-0.1.12.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-15-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20227762be54da3b95186c74c7576e297571be16142976cb0d14dc928b3d2ea5 |
|
MD5 | ce23a964866dcefe194d045a29183336 |
|
BLAKE2b-256 | b3b049309dead03ca5c563092c23922e03dd6c6fb6778aadf1ce0d52aaa7945a |
File details
Details for the file sqlmodel_crud_manager-0.1.12-py3-none-any.whl
.
File metadata
- Download URL: sqlmodel_crud_manager-0.1.12-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-15-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5e724ff633f1e6b11cceec0c303eaba06445cbdb46006fefca0e7872d9e8690 |
|
MD5 | d309a5ff6e3dee17da033b0aaae3585d |
|
BLAKE2b-256 | 7258233a4c38b458228573c01c5ea6d857ab5adbbe7a6aa3f8b6cd35d51b90cb |