{"id":18218862,"url":"https://github.com/klen/aio-databases","last_synced_at":"2025-04-02T22:31:06.014Z","repository":{"id":57408799,"uuid":"395628751","full_name":"klen/aio-databases","owner":"klen","description":"Async Support for various databases","archived":false,"fork":false,"pushed_at":"2024-11-05T09:39:08.000Z","size":279,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2025-03-13T07:17:45.363Z","etag":null,"topics":["asyncio","database","sql","trio"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klen.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-13T11:27:31.000Z","updated_at":"2024-12-24T21:07:01.000Z","dependencies_parsed_at":"2022-09-26T17:10:22.710Z","dependency_job_id":null,"html_url":"https://github.com/klen/aio-databases","commit_stats":null,"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klen%2Faio-databases","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klen%2Faio-databases/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klen%2Faio-databases/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klen%2Faio-databases/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klen","download_url":"https://codeload.github.com/klen/aio-databases/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246905079,"owners_count":20852806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","database","sql","trio"],"created_at":"2024-11-03T18:04:53.963Z","updated_at":"2025-04-02T22:31:05.704Z","avatar_url":"https://github.com/klen.png","language":"Python","readme":"# AIO-Databases\n\nThe package gives you async support for a range of databases (SQLite,\nPostgreSQL, MySQL).\n\n[![Tests Status](https://github.com/klen/aio-databases/workflows/tests/badge.svg)](https://github.com/klen/aio-databases/actions)\n[![PYPI Version](https://img.shields.io/pypi/v/aio-databases)](https://pypi.org/project/aio-databases/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/aio-databases)](https://pypi.org/project/aio-databases/)\n\n## Features\n\n* Has no dependencies (except databases drivers)\n* Supports [asyncio](https://docs.python.org/3/library/asyncio.html) and [trio](https://github.com/python-trio/trio)\n* Supports [aiosqlite](https://github.com/omnilib/aiosqlite),\n  [aiomysql](https://github.com/aio-libs/aiomysql),\n  [aiopg](https://github.com/aio-libs/aiopg),\n  [asyncpg](https://github.com/MagicStack/asyncpg),\n  [triopg](https://github.com/python-trio/triopg),\n  [trio_mysql](https://github.com/python-trio/trio-mysql)\n* Manage pools of connections\n* Manage transactions\n\n## Requirements\n\n* python \u003e= 3.9\n\n## Installation\n\n**aio-databases** should be installed using pip:\n\n```shell\n$ pip install aio-databases\n```\n\nYou have to choose and install the required database drivers with:\n\n```shell\n# To support SQLite\n$ pip install aio-databases[aiosqlite]  # asyncio\n\n# To support MySQL\n$ pip install aio-databases[aiomysql]   # asyncio\n$ pip install aio-databases[trio_mysql] # trio\n\n# To support PostgreSQL (choose one)\n$ pip install aio-databases[aiopg]      # asyncio\n$ pip install aio-databases[asyncpg]    # asyncio\n$ pip install aio-databases[triopg]     # trio\n\n# To support ODBC (alpha state)\n$ pip install aio-databases[aioodbc]    # asyncio\n```\n\n\n## Usage\n\n### Init a database\n\n```python\n    from aio_databases import Database\n\n    # Initialize a database\n    db = Database('sqlite:///:memory:')  # with default driver\n\n    # Flesh out the driver\n    db = Database('asyncpg+pool://test:test@localhost:5432/tests', maxsize=10)\n```\n\n### Supported schemas\n\n- `aiomyql`\n- `aiomyql+pool`\n- `aiopg`\n- `aiopg+pool`\n- `asyncpg`\n- `asyncpg+pool`\n- `aioodbc`\n- `aioodbc+pool`\n- `aiosqlite`\n- `trio-mysql`\n- `triopg`\n\n### Setup a pool of connections (optional)\n\nSetup a pool of connections\n\n```python\n    # Initialize a database's pool\n    async def my_app_starts():\n        await db.connect()\n\n    # Close the pool\n    async def my_app_ends():\n        await db.disconnect()\n\n    # As an alternative users are able to use the database\n    # as an async context manager\n\n    async with db:\n        await my_main_coroutine()\n```\n\n### Get a connection\n\n```python\n    # Acquire and release (on exit) a connection\n    async with db.connection():\n        await my_code()\n\n    # Acquire a connection only if it not exist\n    async with db.connection(False):\n        await my_code()\n```\n\nIf a pool is setup it will be used\n\n### Run SQL queries\n\n```python\n    await db.execute('select $1', '1')\n    await db.executemany('select $1', '1', '2', '3')\n\n    records = await db.fetchall('select (2 * $1) res', 2)\n    assert records == [(4,)]\n\n    record = await db.fetchone('select (2 * $1) res', 2)\n    assert record == (4,)\n    assert record['res'] == 4\n\n    result = await db.fetchval('select 2 * $1', 2)\n    assert result == 4\n```\n\n* Iterate through rows one by one\n\n```python\n\n    async for rec in db.iterate('select name from users'):\n        print(rec)\n\n```\n\n### Manage connections\n\nBy default the database opens and closes a connection for a query.\n\n```python\n    # Connection will be acquired and released for the query\n    await db.fetchone('select %s', 42)\n\n    # Connection will be acquired and released again\n    await db.fetchone('select %s', 77)\n```\n\nManually open and close a connection\n\n```python\n\n    # Acquire a new connection object\n    async with db.connection():\n        # Only one connection will be used\n        await db.fetchone('select %s', 42)\n        await db.fetchone('select %s', 77)\n        # ...\n\n    # Acquire a new connection or use an existing\n    async with db.connection(False):\n        # ...\n```\n\nIf there any connection already `db.method` would be using the current one\n```python\n    async with db.connection(): # connection would be acquired here\n        await db.fetchone('select %s', 42)  # the connection is used\n        await db.fetchone('select %s', 77)  # the connection is used\n\n    # the connection released there\n```\n\n### Manage transactions\n\n```python\n    # Start a tranction using the current connection\n    async with db.transaction() as trans1:\n        # do some work ...\n\n        async with db.transaction() as trans2:\n            # do some work ...\n            await trans2.rollback()\n\n        # unnessesary, the transaction will be commited on exit from the\n        # current context\n\n        await trans1.commit()\n\n    # Create a new connection and start a transaction\n    async with db.tranction(True) as trans:\n        # do some work ...\n```\n\n## Bug tracker\n\nIf you have any suggestions, bug reports or annoyances please report them to\nthe issue tracker at https://github.com/klen/aio-databases/issues\n\n\n## Contributing\n\nDevelopment of the project happens at: https://github.com/klen/aio-databases\n\n\n## License\n\nLicensed under a [MIT License](http://opensource.org/licenses/MIT)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklen%2Faio-databases","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklen%2Faio-databases","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklen%2Faio-databases/lists"}