{"id":50877639,"url":"https://github.com/amrahhh/sqla_async_orm_queries","last_synced_at":"2026-06-15T11:31:52.067Z","repository":{"id":200743223,"uuid":"706094168","full_name":"amrahhh/sqla_async_orm_queries","owner":"amrahhh","description":"Base model for SqlAlchemy async orm queries","archived":false,"fork":false,"pushed_at":"2025-11-20T07:09:20.000Z","size":94,"stargazers_count":16,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T21:57:09.045Z","etag":null,"topics":["asyncio","fastapi","sqlalchemy","sqlalchemy-async","sqlalchemy-orm"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amrahhh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-17T09:42:44.000Z","updated_at":"2025-11-20T07:08:48.000Z","dependencies_parsed_at":"2024-01-19T14:46:26.204Z","dependency_job_id":"97d2c308-24b1-4a34-a658-3485d73e681b","html_url":"https://github.com/amrahhh/sqla_async_orm_queries","commit_stats":null,"previous_names":["amrahhh/sqla_async_orm_queries"],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/amrahhh/sqla_async_orm_queries","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amrahhh%2Fsqla_async_orm_queries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amrahhh%2Fsqla_async_orm_queries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amrahhh%2Fsqla_async_orm_queries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amrahhh%2Fsqla_async_orm_queries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amrahhh","download_url":"https://codeload.github.com/amrahhh/sqla_async_orm_queries/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amrahhh%2Fsqla_async_orm_queries/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34358715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","fastapi","sqlalchemy","sqlalchemy-async","sqlalchemy-orm"],"created_at":"2026-06-15T11:31:51.282Z","updated_at":"2026-06-15T11:31:52.055Z","avatar_url":"https://github.com/amrahhh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLAlchemy ORM with Async Support\n\nThis 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.\n\n## Key Features\n\n- **Async Session Management**: Provides session management using async SQLAlchemy.\n- **CRUD Operations**: Includes create, read, update, and delete operations.\n- **Soft Delete**: Ability to soft-delete records by marking them as inactive.\n- **Audit Logging**: Automatically logs changes to models (insert, update, delete).\n- **Pydantic Model Integration**: Supports data validation using Pydantic models.\n- **Event Listeners**: Event listeners automatically trigger audit logging on inserts, updates, and deletes.\n- **Transaction Management**: Supports transactional operations with rollback on failure.\n- **Pagination Support**: Allows for paginated queries and returns results with metadata.\n- **Bulk Operations**: Provides bulk create, update, and delete functionality.\n\n### Installation\n```sh\npip install sqla-async-orm-queries\n```\n\nAlternatively, if you prefer to use poetry for package dependencies:\n```sh\npoetry shell\npoetry add sqla-async-orm-queries\n```\n\nUsage Examples\nBefore using this code, ensure you have the following dependency installed:\n\nPython 3.8 or above\n\n\n## How to Use\n\n### 1. Define a Model\n\nTo 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.\n\n```python\nfrom sqlalchemy import Column, Integer, String\nfrom sqla_async_orm_queries.models import  Model, PydanticModelMixin, AuditLog\n\nclass User(Model):\n    __tablename__ = 'users'\n    id = Column(Integer, primary_key=True)\n    name = Column(String, nullable=False)\n    email = Column(String, unique=True, nullable=False)\n\n    class PydanticModel(PydanticModelMixin):\n        id: Optional[int]\n        name: str\n        email: str\n```\n\n### 2. Initialize the Session\n\nYou need to initialize the session factory to enable database operations. Make sure to provide an `async_sessionmaker` for managing async sessions.\n\n```python\nfrom sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker\nfrom sqla_async_orm_queries.models import  Model, PydanticModelMixin, AuditLog\n\nDATABASE_URL = \"sqlite+aiosqlite:///:memory:\"\n\nengine = create_async_engine(DATABASE_URL)\nSessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False)\n\nModel.init_session(SessionLocal)\n```\n\n### 3. Perform CRUD Operations\n\nCRUD operations are supported out of the box. You can create, read, update, delete, and soft delete records using the provided methods.\n\n```python\n# Create a new user\nuser_data = {'name': 'John Doe', 'email': 'john@example.com'}\nuser = await User.create(user_data)\n\n# Read a user\nuser = await User.select_one(User.id == 1)\n\n# Update a user\nawait User.update({'name': 'Jane Doe'}, User.id == 1)\n\n# Soft delete a user\nawait User.soft_delete(User.id == 1)\n```\n\n### 4. Audit Logging\n\nThe `AuditLog` model automatically logs any insert, update, or delete operation. The logs are stored in the `audit_logs` table.\n\n```python\n# View audit logs\naudit_logs = await AuditLog.select_all()\n```\n\n### 5. Transaction Management\n\nThe `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.\n\n```python\nasync def my_operations(session):\n    await User.create({'name': 'New User', 'email': 'new_user@example.com'}, session=session)\n\nawait User.transactional(my_operations)\n```\n\n### 6. Bulk Operations\n\nYou can create, update, and delete multiple records in a single transaction using the `bulk_create`, `bulk_update`, and `bulk_delete` methods.\n\n```python\n# Bulk create users\nusers_data = [{'name': 'User 1', 'email': 'user1@example.com'}, {'name': 'User 2', 'email': 'user2@example.com'}]\nawait User.bulk_create(users_data)\n\n# Bulk delete users\nawait User.bulk_delete([User.id == 1, User.id == 2])\n```\n\n### 7. Pagination\n\nPaginate through results using the `select_with_pagination` method:\n\n```python\npagination = await User.select_with_pagination(page=2, per_page=10)\nprint(pagination.items)  # The users on the second page\n```\n\n## Installation\n\n1. Install the necessary dependencies:\n\n```bash\npip install sqlalchemy aiosqlite pydantic\n```\n\n2. Copy or clone this repository and define your models by inheriting from `Model`.\n\n## Running Tests\n\nYou can run tests using `pytest` and `pytest-asyncio` for testing asynchronous operations. \n\n1. Install the test dependencies:\n\n```bash\npip install pytest pytest-asyncio\n```\n\n2. Run the tests:\n\n```bash\npytest\n```\n\n## License\n\nThis project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famrahhh%2Fsqla_async_orm_queries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famrahhh%2Fsqla_async_orm_queries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famrahhh%2Fsqla_async_orm_queries/lists"}