Active record mixin for SQLModel
Project description
🚀 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
)
⚖️ 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.
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_repo-0.0.3.tar.gz
.
File metadata
- Download URL: sqlmodel_repo-0.0.3.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bc59c665628563495a9f4d1fc8b453eecba80ac2724f25881aa680a7c2db3d5 |
|
MD5 | 172ab130ca8325bf05315ea8385e5552 |
|
BLAKE2b-256 | b56bca689fc1a4ac3d1c532f81b3ed72f166db1e2dc2c3f1703a4e5a329d0414 |
File details
Details for the file sqlmodel_repo-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: sqlmodel_repo-0.0.3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9f651b9e54addc62192b1d902404ff875aab972909aa14ca825e249afd723e6 |
|
MD5 | 3ce49d2310655b952fa0c97ee628e1a4 |
|
BLAKE2b-256 | 12c4ae4e04a30accbe84712165a838bb5d1c58753d7c6f84f16e3dd3544cc167 |