Async support for various databases
Project description
AIO-Databases
The package gives you async support for a range of databases (SQLite, PostgreSQL, MySQL).
Features
- Has no dependencies (except databases drivers)
- Supports asyncio and trio
- Supports aiosqlite, aiomysql, aiopg, asyncpg, triopg, trio_mysql
- Manage pools of connections
- Manage transactions
Requirements
- python >= 3.9
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('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
# 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aio_databases-1.1.1.tar.gz
.
File metadata
- Download URL: aio_databases-1.1.1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd4006524224983312dd8bd55d9231be82b3962f8746103460577d08fe5f0a13 |
|
MD5 | 2624a24da3ce22c1571eb15bd0c7506d |
|
BLAKE2b-256 | 4ef24ae4803f4b25f1c9cf6bf6a494cbda603f24a79ca538d03b9dd67ec8bf3e |
File details
Details for the file aio_databases-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: aio_databases-1.1.1-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b61be7bc942fc793cbc8f82396ccdc86ab030b23daa76c3131eb1b3b504f7d30 |
|
MD5 | 214e9925f367ae1f5a2d5093f6741a6c |
|
BLAKE2b-256 | 2644165078edf5372f10d1f9a2ff5b0624c850b21c57246640eb940fae5a512f |