Skip to main content

A set of CRUD utilities to expedite operations with SQLModel

Project description

SQLMODEL_CRUD_UTILS

A set of CRUD (Create, Read, Update, Delete) utilities designed to streamline and expedite common database operations when using SQLModel, offering both synchronous and asynchronous support.

Built with the tools and technologies:

Python SQLModel SQLAlchemy pytest uv


Table of Contents


Overview

sqlmodel-crud-utils provides a convenient layer on top of SQLModel and SQLAlchemy to simplify common database interactions. It offers both synchronous and asynchronous functions for creating, reading, updating, and deleting data, along with helpers for bulk operations, filtering, pagination, and relationship loading. The goal is to reduce boilerplate code in projects using SQLModel.


Features

  • Sync & Async Support: Provides parallel functions in sqlmodel_crud_utils.sync and sqlmodel_crud_utils.a_sync.
  • Simplified CRUD: Offers high-level functions:
    • get_one_or_create: Retrieves an existing record or creates a new one.
    • get_row: Fetches a single row by primary key.
    • get_rows: Fetches multiple rows with flexible filtering, sorting, and pagination.
    • get_rows_within_id_list: Fetches rows matching a list of primary keys.
    • update_row: Updates fields of an existing row.
    • delete_row: Deletes a row by primary key.
    • write_row: Inserts a single new row.
    • insert_data_rows: Inserts multiple new rows with fallback for individual insertion on bulk failure.
    • bulk_upsert_mappings: Performs bulk insert-or-update operations (dialect-aware).
  • Relationship Loading: Supports eager loading (selectinload) and lazy loading (lazyload) via parameters in get_row and get_rows.
  • Flexible Filtering: get_rows supports filtering by exact matches (filter_by) and common comparisons (__like, __gte, __lte, __gt, __lt, __in) using keyword arguments.
  • Pagination: Built-in pagination for get_rows.
  • Dialect-Specific Upsert: Automatically uses the correct upsert syntax (e.g., ON CONFLICT DO UPDATE for PostgreSQL/SQLite) based on the SQL_DIALECT environment variable.
  • Error Handling: Includes basic error logging via loguru and session rollback on exceptions.

Project Structure

└── sqlmodel_crud_utils/
    ├── __init__.py
    ├── __pycache__
       ├── __init__.cpython-313.pyc
       ├── a_sync.cpython-313.pyc
       ├── sync.cpython-313.pyc
       └── utils.cpython-313.pyc
    ├── a_sync.py
    ├── sync.py
    └── utils.py

Project Index

sqlmodel_crud_utils/
__root__
a_sync.py Contains asynchronous versions of the CRUD utility functions, designed for use with `asyncio` and async database drivers (e.g., `aiosqlite`, `asyncpg`).
sync.py Contains synchronous versions of the CRUD utility functions for standard execution environments.
utils.py Provides shared helper functions used by both `sync.py` and `a_sync.py`, such as environment variable retrieval and dynamic dialect-specific import logic for upsert statements.


Getting Started

Prerequisites

  • Python: Version 3.8+ recommended.
  • Database: A SQLAlchemy-compatible database (e.g., PostgreSQL, SQLite, MySQL).
  • SQLModel: Your project should be using SQLModel for ORM definitions.

Configuration

This package requires the SQL_DIALECT environment variable to be set for the upsert functionality to work correctly across different database backends.

Set it in your environment:

export SQL_DIALECT=postgresql # or sqlite, mysql, etc

Or add it to a .env file in your project root (will be loaded automatically via python-dotenv):

SQL_DIALECT=postgresql

Refer to SQLAlchemy Dialects for a list of supported dialect names.

Installation

Install from PyPI (Recommended):

pip install sqlmodel-crud-utils
# Or using uv:
uv pip install sqlmodel-crud-utils

Build from source:

  1. Clone the sqlmodel_crud_utils repository:
git clone https://github.com/fsecada01/SQLModel-CRUD-Utilities.git
  1. Navigate to the project directory:
cd sqlmodel_crud_utils
  1. Install the project dependencies:
uv pip install -r core_requirements.txt
# For testing/development
uv pip install -r dev_requirements.txt

(Alternatively, use pip install -r requirements.txt && pip install .)

Usage

Import the desired functions from either the sync or a_sync module and use them with your SQLModel session and models.

Example (Synchronous):

from sqlmodel import Session, SQLModel, create_engine, Field
from sqlmodel_crud_utils.sync import get_one_or_create, get_rows

# Assume MyModel is defined and engine is created

class MyModel(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    value: int | None = None

DATABASE_URL = "sqlite:///./mydatabase.db"
engine = create_engine(DATABASE_URL)

SQLModel.metadata.create_all(engine)

with Session(engine) as session:
    # Get or create an instance
    instance, created = get_one_or_create(
        session_inst=session, model=MyModel,
        name="Test Item", create_method_kwargs={"value": 123}
    )
    print(f"Instance ID: {instance.id}, Was created: {not created}")

    # Get rows matching criteria
    success, rows = get_rows(
        session_inst=session,
        model=MyModel,
        value__gte=100,
        sort_field="name"
    )
    if success:
        print(f"Found {len(rows)} rows with value >= 100:")
        for row in rows:
            print(f"- {row.name} (ID: {row.id})")

(See sync.py and a_sync.py docstrings or the full README examples from previous interactions for more detailed usage)

Testing

Ensure development dependencies are installed (uv pip install -r dev_requirements.txt or pip install -r dev_requirements.txt).

Run the test suite using pytest:

python -m pytest

This will execute all tests in the tests/ directory and provide coverage information based on the pytest.ini or pyproject.toml configuration.


Project Roadmap

  • Alpha Release: Initial working version with core CRUD functions.
  • Testing: Achieve 100% test coverage via Pytest.
  • CI/CD: Implement GitHub Actions for automated testing, build, and release.
  • Beta Release: Refine features based on initial testing and usage.
  • Community Feedback: Solicit feedback from users.
  • 360 Development Review: Comprehensive internal review of code, docs, and tests.
  • Official 1.0 Release: Stable release suitable for production use.

Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

Contributing Guidelines
  1. Fork the Repository: Start by forking the project repository to your GitHub account.
  2. Clone Locally: Clone the forked repository to your local machine.
    git clone https://github.com/fsecada01/SQLModel-CRUD-Utilities.git
    
  3. Create a New Branch: Always work on a new branch for your changes.
       git checkout -b feature/your-new-feature
    
  4. Make Your Changes: Implement your feature or bug fix. Add tests!
  5. Test Your Changes: Run pytest to ensure all tests pass.
  6. Format and Lint: Ensure code follows project standards (e.g., using black, ruff, pre-commit).
  7. Commit Your Changes: Commit with a clear and concise message.
    git commit -m "feat: Implement the new feature."
    
  8. Push to GitHub: Push the changes to your forked repository.
    git push origin feature/your-new-feature
    
  9. Submit a Pull Request: Create a PR against the main branch of the original repository. Clearly describe your changes.
  10. Review: Wait for code review and address any feedback.
Contributor Graph


License

This project is protected under the MIT License. For more details, refer to the LICENSE file.


Acknowledgments

  • inspiration drawn from the need to streamline CRUD operations across multiple projects utilizing SQLModel.
  • Built upon the excellent foundations provided by SQLModel and SQLAlchemy.
  • Utilizes Loguru for logging and Factory Boy for test data generation.

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_utilities-0.1.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlmodel_crud_utilities-0.1.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file sqlmodel_crud_utilities-0.1.0.tar.gz.

File metadata

  • Download URL: sqlmodel_crud_utilities-0.1.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sqlmodel_crud_utilities-0.1.0.tar.gz
Algorithm Hash digest
SHA256 18f3efa4aa4d2778dda2ddb1dd9d8a6121b836c5faa1c223d5ee2da9e1426f9a
MD5 b0efee33df00ce704a04f248ad40ee9f
BLAKE2b-256 07234de8c4b041a95a1b9549e0715ac6500de119b02a565e3160fff959b68a75

See more details on using hashes here.

File details

Details for the file sqlmodel_crud_utilities-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlmodel_crud_utilities-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0be445ea8a53d2803c358a51ad90a38a62f7f6af85a267ab69352d186f40bd78
MD5 5db562fd7aae51a973c6be1b17a6e57a
BLAKE2b-256 5afcbe90af5b24ab7bdce6e71080dce41679822805a8d12cb2a19131a4da0099

See more details on using hashes here.

Supported by

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