Skip to main content

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.

Release files for SQLModel-CRUD-manager 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for SQLModel-CRUD-manager 0.1.12
File Size Uploaded
sqlmodel_crud_manager-0.1.12.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for SQLModel-CRUD-manager 0.1.12
File Interpreter ABI Platform
sqlmodel_crud_manager-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / sqlmodel_crud_manager-0.1.12.tar.gz

Download URL sqlmodel_crud_manager-0.1.12.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
20227762be54da3b95186c74c7576e297571be16142976cb0d14dc928b3d2ea5
BLAKE2b-256 checksum
How to use checksums
b3b049309dead03ca5c563092c23922e03dd6c6fb6778aadf1ce0d52aaa7945a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-15-generic

Release files / sqlmodel_crud_manager-0.1.12-py3-none-any.whl

Download URL sqlmodel_crud_manager-0.1.12-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5e724ff633f1e6b11cceec0c303eaba06445cbdb46006fefca0e7872d9e8690
BLAKE2b-256 checksum
How to use checksums
7258233a4c38b458228573c01c5ea6d857ab5adbbe7a6aa3f8b6cd35d51b90cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-15-generic

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page