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.2.tar.gz (6.9 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.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

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

Hashes for alchemy_manager-0.0.2.tar.gz
Algorithm Hash digest
SHA256 287d0c769e8cb131e50f1cf82d20d56eb55f91c1e61f81db9610aca4cfdac237
MD5 60fe3291910094534afcdb621423115b
BLAKE2b-256 2402eadb9dcd358729ef79d1f937b9ab29456f948291b120cef01d266482be6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for alchemy_manager-0.0.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for alchemy_manager-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4470aceee4fd7625145826467315db22614453470df5cfca20e08f229965cee6
MD5 0bbd43c991a6160de35e261971b0e649
BLAKE2b-256 aebe2e2971e1f697e9c1764633bbc53eeb3319118ce91706dc7acd2972be6c24

See more details on using hashes here.

Provenance

The following attestation bundles were made for alchemy_manager-0.0.2-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