Helper class for upserting records to database
Project description
Sadel
Sadel is a helper class for upserting records with SQLModel.
Installation
pip install sadel
Example upsert
from sadel import Sadel
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel import Field, create_engine, select, or_
from sqlmodel.ext.asyncio.session import AsyncSession
class Hero(Sadel, table=True):
__tablename__ = "hero"
_upsert_index_elements = {"id"}
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None
# Create
sqlite_url_async = "sqlite+aiosqlite:///database.db"
async_engine = create_async_engine(sqlite_url_async, echo=True, future=True)
hero = Hero(name="Deadpond", secret_name="Dive Wilson")
async with AsyncSession(async_engine) as session:
# Upsert the record
await Hero.upsert(hero, session)
# Fetch the upserted record
result = (
(await session.exec(select(Hero).where(Hero.name == "Deadpond")))
.all()
)
print(result)
Output:
[Hero(id=1, name='Deadpond', secret_name='Dive Wilson', age=None, created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None)]
Example batch upsert
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
async with AsyncSession(async_engine) as session:
await Hero.batch_upsert([hero_1, hero_2, hero_3], session)
result = (
(
await session.exec(
select(Hero).where(or_(Hero.name == "Deadpond", Hero.name == "Spider-Boy", Hero.name == "Rusty-Man"))
)
)
.all()
)
print(result)
Output:
[Hero(id=1, name='Deadpond', secret_name='Dive Wilson',age=None, created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None),
Hero(id=2, name='Spider-Boy", secret_name='Pedro Parqueador',age=None, created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None),
Hero(id=3, name='Rusty-Man', secret_name='Tommy Sharp', age=48, created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None)]
Example update record
async with AsyncSession(async_engine) as session:
# Upsert the record
hero = Hero(name="Deadpond", secret_name="Dive Wilson", age=25)
await Hero.upsert(hero, session)
# Update the record
hero.age = 30
# Upsert the updated record
await Hero.upsert(hero, session)
# Fetch the updated record
result = (
(await session.exec(select(Hero).where(Hero.name == "Deadpond")))
.scalars()
.all()
)
print(result)
Output:
[Hero(id=1, name='Deadpond', secret_name='Dive Wilson', age=30, created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=datetime.datetime(2024, 8, 1, 19, 39, 8))]
Features
- Upsert and batch_upsert functions.
- For auditing, automatically adds and manages
created_onandmodified_oncolumns to your table (timezones are supported). - Validates your data before upserting using Pydantic validate_model method (not supported in SQLModel)
- Asyncio
- Compatible with Alembic
- Specify the (PK) columns to use for upserting using
_upsert_index_elementsattribute - Ignore specific columns from updating using
_upsert_exclude_fieldsattribute
Contributing
- Fork the repository
- Create a new branch
- Make your changes
- Raise a PR
License
This project is licensed under the terms of the MIT License
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sadel-0.1.1.tar.gz.
File metadata
- Download URL: sadel-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
299c0a9746d91ce10e519213c4e672be8104ce736bed0739212dfbbaead77834
|
|
| MD5 |
17a96bfa9fadcf0b35d9a5254485c47c
|
|
| BLAKE2b-256 |
c739836811146b6f39150588af783a5a57d7aa9d05eed283b7fc7bffc0c77d77
|
Provenance
The following attestation bundles were made for sadel-0.1.1.tar.gz:
Publisher:
build-and-publish.yml on dan1elt0m/sadel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sadel-0.1.1.tar.gz -
Subject digest:
299c0a9746d91ce10e519213c4e672be8104ce736bed0739212dfbbaead77834 - Sigstore transparency entry: 600988384
- Sigstore integration time:
-
Permalink:
dan1elt0m/sadel@d63e24fc8bd1b5fd8fe71bd8d17a760f3b0660f6 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/dan1elt0m
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@d63e24fc8bd1b5fd8fe71bd8d17a760f3b0660f6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sadel-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sadel-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0b10cc3c1e1c6a56a2e438a76c371a5a0dbe08d97e585faf3424c4799f657a8
|
|
| MD5 |
44da9fffb637bade084a78f8b60e30e9
|
|
| BLAKE2b-256 |
205a0ab31ff12fccec7bd601e3005233e2f679a1330f2b54ca4ef0377d0ad89a
|
Provenance
The following attestation bundles were made for sadel-0.1.1-py3-none-any.whl:
Publisher:
build-and-publish.yml on dan1elt0m/sadel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sadel-0.1.1-py3-none-any.whl -
Subject digest:
b0b10cc3c1e1c6a56a2e438a76c371a5a0dbe08d97e585faf3424c4799f657a8 - Sigstore transparency entry: 600988385
- Sigstore integration time:
-
Permalink:
dan1elt0m/sadel@d63e24fc8bd1b5fd8fe71bd8d17a760f3b0660f6 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/dan1elt0m
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@d63e24fc8bd1b5fd8fe71bd8d17a760f3b0660f6 -
Trigger Event:
release
-
Statement type: