Skip to main content

🚀 SQLModelRepo - A simple CRUD util for SQLModel

SQLModelRepo is a simple and powerful repository pattern implementation for managing database interactions using the SQLAlchemy-backed SQLModel ORM. It abstracts common database tasks (CRUD - Create, Read, Update, Delete) 🛠️, making data manipulation and access easier, with support for filtering, pagination, and session management 🎯.

🎯 Features

  • 🏷️ Generic Repository: Perform Create, Read (single or multiple), Update, and Delete (CRUD) operations effortlessly.
  • 🔄 Session Management: Automate session reuse or creation, ensuring efficient transaction handling.
  • 🔍 Filtering & Pagination: Easily filter and paginate results.
  • ♻️ Partial Updates: Update records partially using SQL.

📦 Installation

pip install sqlmodel_repo

Ensure you have sqlmodel and other dependencies installed.

🚀 Usage

Define your SQLModel Repository 🏗️

Create an SQLModel class representing your database table.

...
from sqlmodel_repo import SQLModelRepo

class User(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    username: str
    email: str
    extra_metadata: dict = Field(sa_column=Column(JSON))
    
users_repo = SQLModelRepo(model=User, db_engine=engine)

Basic Operations 🛠️

Create a Record ✍️

You can easily create a new record using create.

new_user = users_repo.create(username="john_doe", email="john@example.com")

Retrieve by ID 🆔

Fetch a record by its ID using get_by_id.

user = users_repo.get_by_id(new_user.id)

Update a Record 🔄

Modify a record and call save to persist your changes.

user.email = "john_new@example.com"
users_repo.save(user)

Or, perform partial updates directly with update(id, **kwargs):

users_repo.update(user.id, email="updated_email@example.com")

Delete a Record 🗑️

Easily delete a record by passing the instance to the delete method.

users_repo.delete(user)

Reuse session 🔄

with Session(engine) as session:
    assert users_repo(session).all()

Advanced Querying 🔥

Filtering Records 🔎

Use the filter method to retrieve records meeting specific criteria.

# Filter by username 'john_doe'
users = users_repo.filter(username="john_doe").all()

# Find usernames starting with 'jo'
users = users_repo.filter(User.username.startswith('jo')).all()

Querying Inside JSON Fields 🗂️

from sqlalchemy import cast, String

users = users_repo.filter(cast(User.extra_metadata['some_num'], String) == '99').all()

Paginated Results 📄

Fetch paginated results using paginate or paginate_with_total to retrieve ordered subsets of data.

# Paginate results, sorted by username in descending order
users_paginated, total_count = users_repo.filter().paginate_with_total(
    offset=0, limit=4, order_by="username", desc=True
)

Async 🚅

sqlmodel_repo also has an async version:

pip install sqlmodel_repo[async]
from sqlmodel_repo.async_repo import AsyncSQLModelRepo

⚖️ License

This project is licensed under the MIT License.


Enjoy coding and happy querying with SQLModelRepo! 🎉

Author is a lazy person, so this README.md was generated by AI.

Release files for sqlmodel-repo 0.0.4

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-repo 0.0.4
File Size Uploaded
sqlmodel_repo-0.0.4.tar.gz 11.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlmodel-repo 0.0.4
File Interpreter ABI Platform
sqlmodel_repo-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / sqlmodel_repo-0.0.4.tar.gz

Download URL sqlmodel_repo-0.0.4.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f8a5bdf0b20e7b93ac453f10b520602d8c6b38a5c9d647b71dd2d60ce73c4913
BLAKE2b-256 checksum
How to use checksums
553f7f0ea6db45ddc2f8525a9492e2cb04dc89c03850174ee0176e2f41a45ede
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.0

Release files / sqlmodel_repo-0.0.4-py3-none-any.whl

Download URL sqlmodel_repo-0.0.4-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a5f15cfa4294c0556371da13ee5ceac7c5781d7704dd7dc60b1b6d8caaa0df3
BLAKE2b-256 checksum
How to use checksums
e6d17950a32a909eff9ad503a62353dd6c4492d1b71e48c6ff2ac3207613bd2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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