Skip to main content

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()
  • Q objects for complex queries with AND, 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 objects manager 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alchemy_manager-0.0.4.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

alchemy_manager-0.0.4-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file alchemy_manager-0.0.4.tar.gz.

File metadata

  • Download URL: alchemy_manager-0.0.4.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for alchemy_manager-0.0.4.tar.gz
Algorithm Hash digest
SHA256 88c7ad96c30af997ad4b6881d4947ba1a78da18caa62d97d18139f7e8d09da48
MD5 fb1fe331c9765eeb67432d343dd0eb73
BLAKE2b-256 7ddfe08c12215b66bbf2c3019e67c299e47ad018b18c17bc708979baba9f2b7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for alchemy_manager-0.0.4.tar.gz:

Publisher: release.yaml on AstralMortem/alchemy-manager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file alchemy_manager-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for alchemy_manager-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3a830cb41362ea1397ef9b84f95267709781474e2278b2e5e7159923a5ccee47
MD5 f45f092126aedcc278ff0c3c5a78b97a
BLAKE2b-256 37bc4e191223e393f97f113a047e7c7c52ee7577f6dfbf59731708df79f22cd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for alchemy_manager-0.0.4-py3-none-any.whl:

Publisher: release.yaml on AstralMortem/alchemy-manager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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