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.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)
Prepare the database to work
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()
or get a single connection
async with db.connection():
await my_code()
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
# 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 the connection
await conn.acquire()
# ...
# Release the connection
await conn.relese()
# an alternative (acquire/release as an async context)
async with conn:
# ...
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
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
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-0.9.1.tar.gz
.
File metadata
- Download URL: aio-databases-0.9.1.tar.gz
- Upload date:
- Size: 11.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 754d2d26f25267074157d01adc9d41533f7914081cdc9af5be35da8882b99c96 |
|
MD5 | ac99cabe592ce9e2c0a89287b940735b |
|
BLAKE2b-256 | a27b62fe8dfa8a6ac613bf19c99b34b1bfc68f6edee32450a2768ad66e29318d |
File details
Details for the file aio_databases-0.9.1-py3-none-any.whl
.
File metadata
- Download URL: aio_databases-0.9.1-py3-none-any.whl
- Upload date:
- Size: 15.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | af2ac1cb0e8661c78e787d77f1c66febf83fae12072ad5aaf0f2fc8597e5ec0a |
|
MD5 | 0e7315cbfa69fc0bba2a97e0d3c74ebf |
|
BLAKE2b-256 | 49f1441c6ac9bc1518745ddcafa1e0e4c9105742cb9676fad87ea06f0ba89ff3 |