Skip to main content

Async Support for various databases

Project description

AIO-Databases

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

Tests Status PYPI Version Python Versions

Features

Requirements

  • python >= 3.7

Installation

aio-databases should be installed using pip:

$ pip install aio-databases

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

# 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

    from aio_databases import Database

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

    # Flesh out the driver 
    db = Database('aiosqlite:///:memory:', **driver_params)

Setup a pool of connections (optional)

Setup a pool of connections

    # 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

    # 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

    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
    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.

    # 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

    # 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

    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

    # 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio-databases-0.12.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

aio_databases-0.12.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file aio-databases-0.12.0.tar.gz.

File metadata

  • Download URL: aio-databases-0.12.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for aio-databases-0.12.0.tar.gz
Algorithm Hash digest
SHA256 02bf4de321f5a6c1fc0e0d9fae3d343a95494fdf8c3bef3a92db901d2a6b6307
MD5 f3ed9413530b3d1119092b0dfa5a594a
BLAKE2b-256 f37e15968733dc90910e3c014d9ab9e47999a83ce55d0aa76ad48782c4d6b24a

See more details on using hashes here.

File details

Details for the file aio_databases-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: aio_databases-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for aio_databases-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b69b5ccdca4e4fc96564f2f1aa3dda1752e3b80474b992f258a0e74166c6fe7c
MD5 ba383d1e37e3deaed4f018a4954fce95
BLAKE2b-256 bbf1767c28d59f757577fd647b4e48dbb79ce6cb7896417ead9e248041da2c1d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page