Skip to main content

Alternative approach to database API with full native async support and connection pool.

Project description

gcm-asyncdb

This library provides better, more robust database API with strong enforcement of transaction logic when working with relational databases.

Currently, only MySQL/MariaDB (aiomysql) is supported, but support for other databases should be easy to implement, when required.

Documentation

API documentation is available at https://gcm-cz.gitlab.io/gcm-asyncdb

Features

  • Fully type-safe, based on modern Python.
  • Fully async.
  • Connection pool with health checking and metrics.
  • Strong transaction usage enforcement, automatic rollback.
  • Advanced result fetching methods to simplify the usage.
  • Observer support for transaction and query events.
  • Structured logging with various loggers for various tasks, per-database logger, correct stack level.
  • Exception hierarchy for correct and easier error handling.
  • Timezone-aware datetimes (with possible fallback to naive datetimes).
  • Native support for sqlfactory statements.
  • OpenTracing and OpenTelemetry support.

Installation

pip install gcm-asyncdb

Extras

  • opentracing - for OpenTracing support
  • opentelemetry - for OpenTelemetry support
  • sqlfactory - for sqlfactory statement support
  • pydantic - for pydantic model support in fetch_object calls.

Usage

from asyncdb.aiomysql import TransactionFactory, MySQLConfig

db = TransactionFactory(MySQLConfig(
    host='localhost',
    user='root',
    database='test'
))


async def main():
    async with db.transaction() as t:
        await t.query('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
        await t.query('INSERT INTO test (id, name) VALUES (1, "test")')
        result = await t.query('SELECT * FROM test')
        async for row in result.fetch_all_dict():
            print(row)

        await t.commit()

License

MIT

Maturity

This library is still very new, but grew from multiple projects where it gradually evolved. So it is already used in production, but it is still recommended to test it thoroughly before using it in production.

Historically, there has been problems with losing connections from connection pool in rare unpredictable error cases, it seems to be resolved now, but there is no regression test for this, so be careful.

Contributing

Feel free to open an issue or a pull request.

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

gcm_asyncdb-2.4.2.tar.gz (59.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gcm_asyncdb-2.4.2-py3-none-any.whl (76.0 kB view details)

Uploaded Python 3

File details

Details for the file gcm_asyncdb-2.4.2.tar.gz.

File metadata

  • Download URL: gcm_asyncdb-2.4.2.tar.gz
  • Upload date:
  • Size: 59.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.0 CPython/3.11.14 Linux/6.8.0-90-generic

File hashes

Hashes for gcm_asyncdb-2.4.2.tar.gz
Algorithm Hash digest
SHA256 ad887572445c858a90f2c25e079f5629fabc0362d2e0e4f8e4f19d4d515d5e9e
MD5 1c9f19d4e6e88659d577bb470bd481e3
BLAKE2b-256 0d8ce07e1f15f763cd3ab54e484414b2114d8edec4b123a2140d7c0b371629db

See more details on using hashes here.

File details

Details for the file gcm_asyncdb-2.4.2-py3-none-any.whl.

File metadata

  • Download URL: gcm_asyncdb-2.4.2-py3-none-any.whl
  • Upload date:
  • Size: 76.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.0 CPython/3.11.14 Linux/6.8.0-90-generic

File hashes

Hashes for gcm_asyncdb-2.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0c93c99928e4071eaf645cdb1b9e9b2000fbb86a7977a312646a19b88ee729a
MD5 3283969baa7f119343ed8617eefefc09
BLAKE2b-256 dcf285d75881e1dd4416bc0035671cae84f383e62fec1fa15dfb9ebd83f3a786

See more details on using hashes here.

Supported by

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