Skip to main content

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

Tests Ruff pypi versions license

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 sent
  • list(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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlmodel_crud_manager-0.1.12.tar.gz (7.8 kB view hashes)

Uploaded Source

Built Distribution

sqlmodel_crud_manager-0.1.12-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page