aio-databases


Nameaio-databases JSON
Version 1.1.1 PyPI version JSON
download
home_pagehttps://github.com/klen/aio-databases
SummaryAsync support for various databases
upload_time2024-11-05 09:40:44
maintainerNone
docs_urlNone
authorKirill Klenov
requires_python<4.0,>=3.9
licenseMIT
keywords asyncio trio databases mysql sqlite postgres postgresql
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AIO-Databases

The package gives you async support for a range of databases (SQLite,
PostgreSQL, MySQL).

[![Tests Status](https://github.com/klen/aio-databases/workflows/tests/badge.svg)](https://github.com/klen/aio-databases/actions)
[![PYPI Version](https://img.shields.io/pypi/v/aio-databases)](https://pypi.org/project/aio-databases/)
[![Python Versions](https://img.shields.io/pypi/pyversions/aio-databases)](https://pypi.org/project/aio-databases/)

## Features

* Has no dependencies (except databases drivers)
* Supports [asyncio](https://docs.python.org/3/library/asyncio.html) and [trio](https://github.com/python-trio/trio)
* Supports [aiosqlite](https://github.com/omnilib/aiosqlite),
  [aiomysql](https://github.com/aio-libs/aiomysql),
  [aiopg](https://github.com/aio-libs/aiopg),
  [asyncpg](https://github.com/MagicStack/asyncpg),
  [triopg](https://github.com/python-trio/triopg),
  [trio_mysql](https://github.com/python-trio/trio-mysql)
* Manage pools of connections
* Manage transactions

## Requirements

* python >= 3.9

## Installation

**aio-databases** should be installed using pip:

```shell
$ pip install aio-databases
```

You have to choose and install the required database drivers with:

```shell
# To support SQLite
$ pip install aio-databases[aiosqlite]  # asyncio

# To support MySQL
$ pip install aio-databases[aiomysql]   # asyncio
$ pip install aio-databases[trio_mysql] # trio

# To support PostgreSQL (choose one)
$ pip install aio-databases[aiopg]      # asyncio
$ pip install aio-databases[asyncpg]    # asyncio
$ pip install aio-databases[triopg]     # trio

# To support ODBC (alpha state)
$ pip install aio-databases[aioodbc]    # asyncio
```


## Usage

### Init a database

```python
    from aio_databases import Database

    # Initialize a database
    db = Database('sqlite:///:memory:')  # with default driver

    # Flesh out the driver
    db = Database('asyncpg+pool://test:test@localhost:5432/tests', maxsize=10)
```

### Supported schemas

- `aiomyql`
- `aiomyql+pool`
- `aiopg`
- `aiopg+pool`
- `asyncpg`
- `asyncpg+pool`
- `aioodbc`
- `aioodbc+pool`
- `aiosqlite`
- `trio-mysql`
- `triopg`

### Setup a pool of connections (optional)

Setup a pool of connections

```python
    # Initialize a database's pool
    async def my_app_starts():
        await db.connect()

    # Close the pool
    async def my_app_ends():
        await db.disconnect()

    # As an alternative users are able to use the database
    # as an async context manager

    async with db:
        await my_main_coroutine()
```

### Get a connection

```python
    # Acquire and release (on exit) a connection
    async with db.connection():
        await my_code()

    # Acquire a connection only if it not exist
    async with db.connection(False):
        await my_code()
```

If a pool is setup it will be used

### Run SQL queries

```python
    await db.execute('select $1', '1')
    await db.executemany('select $1', '1', '2', '3')

    records = await db.fetchall('select (2 * $1) res', 2)
    assert records == [(4,)]

    record = await db.fetchone('select (2 * $1) res', 2)
    assert record == (4,)
    assert record['res'] == 4

    result = await db.fetchval('select 2 * $1', 2)
    assert result == 4
```

* Iterate through rows one by one

```python

    async for rec in db.iterate('select name from users'):
        print(rec)

```

### Manage connections

By default the database opens and closes a connection for a query.

```python
    # Connection will be acquired and released for the query
    await db.fetchone('select %s', 42)

    # Connection will be acquired and released again
    await db.fetchone('select %s', 77)
```

Manually open and close a connection

```python

    # Acquire a new connection object
    async with db.connection():
        # Only one connection will be used
        await db.fetchone('select %s', 42)
        await db.fetchone('select %s', 77)
        # ...

    # Acquire a new connection or use an existing
    async with db.connection(False):
        # ...
```

If there any connection already `db.method` would be using the current one
```python
    async with db.connection(): # connection would be acquired here
        await db.fetchone('select %s', 42)  # the connection is used
        await db.fetchone('select %s', 77)  # the connection is used

    # the connection released there
```

### Manage transactions

```python
    # Start a tranction using the current connection
    async with db.transaction() as trans1:
        # do some work ...

        async with db.transaction() as trans2:
            # do some work ...
            await trans2.rollback()

        # unnessesary, the transaction will be commited on exit from the
        # current context

        await trans1.commit()

    # Create a new connection and start a transaction
    async with db.tranction(True) as trans:
        # do some work ...
```

## Bug tracker

If you have any suggestions, bug reports or annoyances please report them to
the issue tracker at https://github.com/klen/aio-databases/issues


## Contributing

Development of the project happens at: https://github.com/klen/aio-databases


## License

Licensed under a [MIT License](http://opensource.org/licenses/MIT)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klen/aio-databases",
    "name": "aio-databases",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "asyncio, trio, databases, mysql, sqlite, postgres, postgresql",
    "author": "Kirill Klenov",
    "author_email": "horneds@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4e/f2/4ae4803f4b25f1c9cf6bf6a494cbda603f24a79ca538d03b9dd67ec8bf3e/aio_databases-1.1.1.tar.gz",
    "platform": null,
    "description": "# 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 >= 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",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Async support for various databases",
    "version": "1.1.1",
    "project_urls": {
        "Homepage": "https://github.com/klen/aio-databases",
        "Repository": "https://github.com/klen/aio-databases"
    },
    "split_keywords": [
        "asyncio",
        " trio",
        " databases",
        " mysql",
        " sqlite",
        " postgres",
        " postgresql"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2644165078edf5372f10d1f9a2ff5b0624c850b21c57246640eb940fae5a512f",
                "md5": "214e9925f367ae1f5a2d5093f6741a6c",
                "sha256": "b61be7bc942fc793cbc8f82396ccdc86ab030b23daa76c3131eb1b3b504f7d30"
            },
            "downloads": -1,
            "filename": "aio_databases-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "214e9925f367ae1f5a2d5093f6741a6c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 17036,
            "upload_time": "2024-11-05T09:40:43",
            "upload_time_iso_8601": "2024-11-05T09:40:43.348381Z",
            "url": "https://files.pythonhosted.org/packages/26/44/165078edf5372f10d1f9a2ff5b0624c850b21c57246640eb940fae5a512f/aio_databases-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ef24ae4803f4b25f1c9cf6bf6a494cbda603f24a79ca538d03b9dd67ec8bf3e",
                "md5": "2624a24da3ce22c1571eb15bd0c7506d",
                "sha256": "fd4006524224983312dd8bd55d9231be82b3962f8746103460577d08fe5f0a13"
            },
            "downloads": -1,
            "filename": "aio_databases-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2624a24da3ce22c1571eb15bd0c7506d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 12356,
            "upload_time": "2024-11-05T09:40:44",
            "upload_time_iso_8601": "2024-11-05T09:40:44.549535Z",
            "url": "https://files.pythonhosted.org/packages/4e/f2/4ae4803f4b25f1c9cf6bf6a494cbda603f24a79ca538d03b9dd67ec8bf3e/aio_databases-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-05 09:40:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "klen",
    "github_project": "aio-databases",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aio-databases"
}
        
Elapsed time: 0.39370s