Simple library which add django-like objects manager and queryset to sqlalchemy model
Project description
AlchemyManager
AlchemyManager is a lightweight Python ORM built on SQLAlchemy, inspired by Django’s ORM.
It provides Django-like model API with objects manager, QuerySet, Q objects, and supports sync and async CRUD operations with automatic session management.
Features
- Django-style
Model.objects.create(),.get(),.filter(),.update(),.delete() Qobjects for complex queries withAND,OR,NOT, and multiple lookups (eq,lt,gt,contains,in)- Supports sync and async operations
- Automatic session management
- Optional explicit transaction scopes (
sync_session_scope,async_session_scope) - Typed
objectsmanager for IDE autocompletion and static checking - Bulk operations (
bulk_create) - Works with SQLite, PostgreSQL, MySQL (via SQLAlchemy)
Installation
pip install alchemy-manager
Usage
Full Example
from alchemy_manager import Model, init_sync_db
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Integer, String, Boolean
class User(Model):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(100))
age: Mapped[int] = mapped_column(Integer, default=0)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
if __name__ == "__main__":
db_path = "sqlite:///test.db"
init_sync_db(db_path)
instance1 = User.objects.create(name="Alice", age=20)
instance2 = User(name="Anthony", age=17).save()
peoples = User.objects.filter(age_gte=18).all() #[User(name="Alice")]
Sync operations
# Create
user = User.objects.create(name="Alice", age=20)
# Get
user = User.objects.get(id=1)
# Filter
users = User.objects.filter(age__gte=18).all()
# Update
User.objects.filter(id=1).update(name="Bob")
# Delete
user.delete()
# Bulk create
User.objects.bulk_create([
User(name="A"),
User(name="B"),
])
Async operations
import asyncio
async def main():
# Create
user = await User.objects.acreate(name="Bob", age=30)
# Get
user = await User.objects.aget(id=1)
# Filter
users = await User.objects.filter(age__gte=18).aall()
# Update
await User.objects.filter(id=1).aupdate(name="Charlie")
# Delete
await user.adelete()
asyncio.run(main())
Explicit transaction scopes
from alchemy_manager import sync_session_scope, async_session_scope
# Sync
with sync_session_scope():
user = User.objects.create(name="Scoped")
user.save()
# Async
async with async_session_scope():
user = await User.objects.acreate(name="AsyncScoped")
Q object usage
from alchemy_manager.queryset import Q
# Complex query
users = User.objects.filter(
Q(age__gte=18) & ~Q(name__startswith="A")
).all()
Testing
pip install pytest pytest-asyncio
pytest -v
License
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 alchemy_manager-0.0.2.tar.gz.
File metadata
- Download URL: alchemy_manager-0.0.2.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
287d0c769e8cb131e50f1cf82d20d56eb55f91c1e61f81db9610aca4cfdac237
|
|
| MD5 |
60fe3291910094534afcdb621423115b
|
|
| BLAKE2b-256 |
2402eadb9dcd358729ef79d1f937b9ab29456f948291b120cef01d266482be6d
|
Provenance
The following attestation bundles were made for alchemy_manager-0.0.2.tar.gz:
Publisher:
release.yaml on AstralMortem/alchemy-manager
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alchemy_manager-0.0.2.tar.gz -
Subject digest:
287d0c769e8cb131e50f1cf82d20d56eb55f91c1e61f81db9610aca4cfdac237 - Sigstore transparency entry: 778936059
- Sigstore integration time:
-
Permalink:
AstralMortem/alchemy-manager@d5041ccbcd40acfc005d4f42a006df9fa2846558 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/AstralMortem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@d5041ccbcd40acfc005d4f42a006df9fa2846558 -
Trigger Event:
push
-
Statement type:
File details
Details for the file alchemy_manager-0.0.2-py3-none-any.whl.
File metadata
- Download URL: alchemy_manager-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.3 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 |
4470aceee4fd7625145826467315db22614453470df5cfca20e08f229965cee6
|
|
| MD5 |
0bbd43c991a6160de35e261971b0e649
|
|
| BLAKE2b-256 |
aebe2e2971e1f697e9c1764633bbc53eeb3319118ce91706dc7acd2972be6c24
|
Provenance
The following attestation bundles were made for alchemy_manager-0.0.2-py3-none-any.whl:
Publisher:
release.yaml on AstralMortem/alchemy-manager
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alchemy_manager-0.0.2-py3-none-any.whl -
Subject digest:
4470aceee4fd7625145826467315db22614453470df5cfca20e08f229965cee6 - Sigstore transparency entry: 778936062
- Sigstore integration time:
-
Permalink:
AstralMortem/alchemy-manager@d5041ccbcd40acfc005d4f42a006df9fa2846558 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/AstralMortem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@d5041ccbcd40acfc005d4f42a006df9fa2846558 -
Trigger Event:
push
-
Statement type: