Skip to main content

SQLAlchemy ORM with Async Support

This project provides an abstract base class Model with advanced CRUD operations, audit logging, and support for SQLAlchemy's async ORM functionality. It includes several useful features such as soft delete functionality, Pydantic model integration for validation, and audit logging with event listeners.

Key Features

  • Async Session Management: Provides session management using async SQLAlchemy.
  • CRUD Operations: Includes create, read, update, and delete operations.
  • Soft Delete: Ability to soft-delete records by marking them as inactive.
  • Audit Logging: Automatically logs changes to models (insert, update, delete).
  • Pydantic Model Integration: Supports data validation using Pydantic models.
  • Event Listeners: Event listeners automatically trigger audit logging on inserts, updates, and deletes.
  • Transaction Management: Supports transactional operations with rollback on failure.
  • Pagination Support: Allows for paginated queries and returns results with metadata.
  • Bulk Operations: Provides bulk create, update, and delete functionality.

Installation

pip install sqla-async-orm-queries

Alternatively, if you prefer to use poetry for package dependencies:

poetry shell
poetry add sqla-async-orm-queries

Usage Examples Before using this code, ensure you have the following dependency installed:

Python 3.8 or above

How to Use

1. Define a Model

To define your own model, inherit from the Model class and define your fields using SQLAlchemy's Column types. Each model can also define its own Pydantic schema for validation purposes.

from sqlalchemy import Column, Integer, String
from sqla_async_orm_queries.models import  Model, PydanticModelMixin, AuditLog

class User(Model):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    email = Column(String, unique=True, nullable=False)

    class PydanticModel(PydanticModelMixin):
        id: Optional[int]
        name: str
        email: str

2. Initialize the Session

You need to initialize the session factory to enable database operations. Make sure to provide an async_sessionmaker for managing async sessions.

from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqla_async_orm_queries.models import  Model, PydanticModelMixin, AuditLog

DATABASE_URL = "sqlite+aiosqlite:///:memory:"

engine = create_async_engine(DATABASE_URL)
SessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False)

Model.init_session(SessionLocal)

3. Perform CRUD Operations

CRUD operations are supported out of the box. You can create, read, update, delete, and soft delete records using the provided methods.

# Create a new user
user_data = {'name': 'John Doe', 'email': 'john@example.com'}
user = await User.create(user_data)

# Read a user
user = await User.select_one(User.id == 1)

# Update a user
await User.update({'name': 'Jane Doe'}, User.id == 1)

# Soft delete a user
await User.soft_delete(User.id == 1)

4. Audit Logging

The AuditLog model automatically logs any insert, update, or delete operation. The logs are stored in the audit_logs table.

# View audit logs
audit_logs = await AuditLog.select_all()

5. Transaction Management

The transactional method provides an easy way to run a set of operations inside a transaction. If any error occurs, the transaction will be rolled back.

async def my_operations(session):
    await User.create({'name': 'New User', 'email': 'new_user@example.com'}, session=session)

await User.transactional(my_operations)

6. Bulk Operations

You can create, update, and delete multiple records in a single transaction using the bulk_create, bulk_update, and bulk_delete methods.

# Bulk create users
users_data = [{'name': 'User 1', 'email': 'user1@example.com'}, {'name': 'User 2', 'email': 'user2@example.com'}]
await User.bulk_create(users_data)

# Bulk delete users
await User.bulk_delete([User.id == 1, User.id == 2])

7. Pagination

Paginate through results using the select_with_pagination method:

pagination = await User.select_with_pagination(page=2, per_page=10)
print(pagination.items)  # The users on the second page

Installation

  1. Install the necessary dependencies:
pip install sqlalchemy aiosqlite pydantic
  1. Copy or clone this repository and define your models by inheriting from Model.

Running Tests

You can run tests using pytest and pytest-asyncio for testing asynchronous operations.

  1. Install the test dependencies:
pip install pytest pytest-asyncio
  1. Run the tests:
pytest

License

This project is licensed under the MIT License.

Metadata

Release files for sqla-async-orm-queries 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqla-async-orm-queries 2.1.1
File Size Uploaded
sqla_async_orm_queries-2.1.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqla-async-orm-queries 2.1.1
File Interpreter ABI Platform
sqla_async_orm_queries-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / sqla_async_orm_queries-2.1.1.tar.gz

Download URL sqla_async_orm_queries-2.1.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8fd814a6e1365f75760eb127b748273aa867e8ccee50a62e2402d709735179dd
BLAKE2b-256 checksum
How to use checksums
2f07c1afc25b8970c2283b80d443252d50a85221a7aab57bbc1c5290cec2f2e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

Release files / sqla_async_orm_queries-2.1.1-py3-none-any.whl

Download URL sqla_async_orm_queries-2.1.1-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3cc1c36ebeb8b06e43a5dd7b588c437c46a4038bbd097a06f29811534f53bfb2
BLAKE2b-256 checksum
How to use checksums
3dbf33e0b3587d51bb1fd3dc46eecbd7fee82ac90c8f7b7941f9c3eeb00dd74a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

1.2.0

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page