Skip to main content

Async Support for various databases

Project description

AIO-Databases

The package gives you asycio 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]

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

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

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

Usage

  • Init a database
    from aio_databases import Database

    db = Database('sqlite:///:memory:')
  • Prepare the database to work
    # 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()
  • 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 (please ensure that you have released a connection after acquiring)
    # Create a new connection object
    conn = db.connection()

    # or use the existing which one is binded for the current task
    conn = db.connection(False)

    # Acquire DB connection
    await conn.acquire()
    # ...
    # Release DB connection
    await conn.relese()

    # an alternative (acquire/release as an async context)
    async with db.connection():
        # ...
  • Use transactions
    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()

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.3.7.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

aio_databases-0.3.7-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aio-databases-0.3.7.tar.gz
  • Upload date:
  • Size: 10.0 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.2 CPython/3.9.7

File hashes

Hashes for aio-databases-0.3.7.tar.gz
Algorithm Hash digest
SHA256 e972e2b1a9f93ef6fb14a8defe38710441291bc1ea5d8896f636c4b6839ce652
MD5 40866b8c0886e286c2a8a263db200178
BLAKE2b-256 8433deeca1bd4f44db80f8f5351f4dbf884f5b5e9983acad99c59bdda830b261

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aio_databases-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 12.6 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.2 CPython/3.9.7

File hashes

Hashes for aio_databases-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 fc21bbd9db20f27fa4a9f7c18db956d9a1141518e9d3dc7613be45d5d68ff89a
MD5 eb71f46677cc62a0cd2e8c3b85c1881a
BLAKE2b-256 92684581e43b712f8151023faac4eae92ac212dd39d41bbd5eb9004a4060fed4

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