sqlalchemy-tx-context


Namesqlalchemy-tx-context JSON
Version 0.4.1 PyPI version JSON
download
home_pagehttps://github.com/QuisEgoSum/sqlalchemy-tx-context
SummaryAn extension for sqlalchemy
upload_time2024-11-13 15:21:53
maintainerNone
docs_urlNone
authorQuisEgoSum
requires_python>=3.7
licenseMIT
keywords sqlalchemy
VCS
bugtrack_url
requirements asyncpg SQLAlchemy contextvars
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# SQLAlchemy Transaction Context

An extension for sqlalchemy to store the session object in context and simplify the retrieval of the query result.


## Create instance 
```python
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy_tx_context import SQLAlchemyTransactionContext


engine = create_async_engine(...)
db = SQLAlchemyTransactionContext(engine)
```

## Execute query

`SQLAlchemyTransactionContext` contains the following methods for creating queries with execution
through a context session:
- `select`
- `insert`
- `update`
- `delete`
- `union`
- `union_all`
- `exists`

Query will use a session from the context.

```python
async def some_repository_method():
    await db.insert(...).values(...).execute()


async def some_function():
    async with db.transaction() as tx:
        await some_repository_method()
        await tx.rollback() # Record will not be inserted
```


If there is no session in the context, a default session will be created to execute a single query.

```python
async def some_repository_method():
    await db.insert(...).values(...).execute()

async def some_function():
      await some_repository_method()
```


Calling the transaction method inside the session will create a nested transaction.

```python
async def some_function():
    async with db.transaction() as tx1:
        isinstance(tx1, AsyncSession) # True
        async with db.transaction() as tx2:
            isinstance(tx2, AsyncSessionTransaction) # True
```

## Execute query with proxy methods

List of proxy methods added to the request object

Proxy methods for the `AsyncSession` properties:
- `execute`
- `scalar`
- `scalars`

Example:

```python
value = await db.select(...).execute()
```

The same as:

```python
async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))
```

Proxy methods for the `Result` properties:
- `first`
- `all`

Example:
```python
value = await db.select(...).first()
```

The same as:

```python
async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))
    value = result.first()
```

Proxy methods for the `MappingResult` properties:

- `mapped_first`
- `mapped_one`
- `mapped_all`

Example:
```python
value = await db.select(...).mapped_first()
```

The same as:

```python
async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))
    value = result.mappings().first()
```

Proxy method for the `CursorResult`:

- `rowcount`


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/QuisEgoSum/sqlalchemy-tx-context",
    "name": "sqlalchemy-tx-context",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "sqlalchemy",
    "author": "QuisEgoSum",
    "author_email": "subbotin.evdokim@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e4/a1/e6247fa9836419a235ccca4e2ca61a9de025ca2f953a3b9be1f0edee8e17/sqlalchemy_tx_context-0.4.1.tar.gz",
    "platform": null,
    "description": "\n# SQLAlchemy Transaction Context\n\nAn extension for sqlalchemy to store the session object in context and simplify the retrieval of the query result.\n\n\n## Create instance \n```python\nfrom sqlalchemy.ext.asyncio import create_async_engine\nfrom sqlalchemy_tx_context import SQLAlchemyTransactionContext\n\n\nengine = create_async_engine(...)\ndb = SQLAlchemyTransactionContext(engine)\n```\n\n## Execute query\n\n`SQLAlchemyTransactionContext` contains the following methods for creating queries with execution\nthrough a context session:\n- `select`\n- `insert`\n- `update`\n- `delete`\n- `union`\n- `union_all`\n- `exists`\n\nQuery will use a session from the context.\n\n```python\nasync def some_repository_method():\n    await db.insert(...).values(...).execute()\n\n\nasync def some_function():\n    async with db.transaction() as tx:\n        await some_repository_method()\n        await tx.rollback() # Record will not be inserted\n```\n\n\nIf there is no session in the context, a default session will be created to execute a single query.\n\n```python\nasync def some_repository_method():\n    await db.insert(...).values(...).execute()\n\nasync def some_function():\n      await some_repository_method()\n```\n\n\nCalling the transaction method inside the session will create a nested transaction.\n\n```python\nasync def some_function():\n    async with db.transaction() as tx1:\n        isinstance(tx1, AsyncSession) # True\n        async with db.transaction() as tx2:\n            isinstance(tx2, AsyncSessionTransaction) # True\n```\n\n## Execute query with proxy methods\n\nList of proxy methods added to the request object\n\nProxy methods for the `AsyncSession` properties:\n- `execute`\n- `scalar`\n- `scalars`\n\nExample:\n\n```python\nvalue = await db.select(...).execute()\n```\n\nThe same as:\n\n```python\nasync with async_sessionmaker(engine).begin() as tx:\n    result = await tx.execute(select(...))\n```\n\nProxy methods for the `Result` properties:\n- `first`\n- `all`\n\nExample:\n```python\nvalue = await db.select(...).first()\n```\n\nThe same as:\n\n```python\nasync with async_sessionmaker(engine).begin() as tx:\n    result = await tx.execute(select(...))\n    value = result.first()\n```\n\nProxy methods for the `MappingResult` properties:\n\n- `mapped_first`\n- `mapped_one`\n- `mapped_all`\n\nExample:\n```python\nvalue = await db.select(...).mapped_first()\n```\n\nThe same as:\n\n```python\nasync with async_sessionmaker(engine).begin() as tx:\n    result = await tx.execute(select(...))\n    value = result.mappings().first()\n```\n\nProxy method for the `CursorResult`:\n\n- `rowcount`\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An extension for sqlalchemy",
    "version": "0.4.1",
    "project_urls": {
        "Homepage": "https://github.com/QuisEgoSum/sqlalchemy-tx-context"
    },
    "split_keywords": [
        "sqlalchemy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2df73a76d6a99760b9c300467d42ed24c515806cf73de367fc84dbabe263c633",
                "md5": "a3c37dd464fc979455cd6831c95791f3",
                "sha256": "69d0e7d1b01ad5d98af28901bd8ba9227ac62a85f67a5a68a26467e37aceb24d"
            },
            "downloads": -1,
            "filename": "sqlalchemy_tx_context-0.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a3c37dd464fc979455cd6831c95791f3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 13584,
            "upload_time": "2024-11-13T15:21:49",
            "upload_time_iso_8601": "2024-11-13T15:21:49.230109Z",
            "url": "https://files.pythonhosted.org/packages/2d/f7/3a76d6a99760b9c300467d42ed24c515806cf73de367fc84dbabe263c633/sqlalchemy_tx_context-0.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e4a1e6247fa9836419a235ccca4e2ca61a9de025ca2f953a3b9be1f0edee8e17",
                "md5": "f94282802c076d6861e799c0d8e0d6d3",
                "sha256": "3adbe43adf0249cc7fcd851813bf7918bbbbebd6a31dafc35934d9b1838c82ed"
            },
            "downloads": -1,
            "filename": "sqlalchemy_tx_context-0.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f94282802c076d6861e799c0d8e0d6d3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 7979,
            "upload_time": "2024-11-13T15:21:53",
            "upload_time_iso_8601": "2024-11-13T15:21:53.194248Z",
            "url": "https://files.pythonhosted.org/packages/e4/a1/e6247fa9836419a235ccca4e2ca61a9de025ca2f953a3b9be1f0edee8e17/sqlalchemy_tx_context-0.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-13 15:21:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "QuisEgoSum",
    "github_project": "sqlalchemy-tx-context",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "asyncpg",
            "specs": [
                [
                    "==",
                    "0.29.0"
                ]
            ]
        },
        {
            "name": "SQLAlchemy",
            "specs": [
                [
                    "==",
                    "2.0.29"
                ]
            ]
        },
        {
            "name": "contextvars",
            "specs": [
                [
                    "==",
                    "2.4"
                ]
            ]
        }
    ],
    "lcname": "sqlalchemy-tx-context"
}
        
Elapsed time: 0.92384s