# AIO-Databases
The package gives you async support for a range of databases (SQLite,
PostgreSQL, MySQL).
[![Tests Status](https://github.com/klen/aio-databases/workflows/tests/badge.svg)](https://github.com/klen/aio-databases/actions)
[![PYPI Version](https://img.shields.io/pypi/v/aio-databases)](https://pypi.org/project/aio-databases/)
[![Python Versions](https://img.shields.io/pypi/pyversions/aio-databases)](https://pypi.org/project/aio-databases/)
## Features
* Has no dependencies (except databases drivers)
* Supports [asyncio](https://docs.python.org/3/library/asyncio.html) and [trio](https://github.com/python-trio/trio)
* Supports [aiosqlite](https://github.com/omnilib/aiosqlite),
[aiomysql](https://github.com/aio-libs/aiomysql),
[aiopg](https://github.com/aio-libs/aiopg),
[asyncpg](https://github.com/MagicStack/asyncpg),
[triopg](https://github.com/python-trio/triopg),
[trio_mysql](https://github.com/python-trio/trio-mysql)
* Manage pools of connections
* Manage transactions
## Requirements
* python >= 3.7
## Installation
**aio-databases** should be installed using pip:
```shell
$ pip install aio-databases
```
You have to choose and install the required database drivers with:
```shell
# 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
```python
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
```python
# 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
```python
# 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
```python
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
```python
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.
```python
# 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
```python
# 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
```python
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
```python
# 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](http://opensource.org/licenses/MIT)
Raw data
{
"_id": null,
"home_page": "https://github.com/klen/aio-databases",
"name": "aio-databases",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "asyncio,trio,databases,mysql,sqlite,postgres,postgresql",
"author": "Kirill Klenov",
"author_email": "horneds@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/dc/c7/22db93b9b9ce2d4b290ac54e958b5a2e81815e863bb47c6c0c946c6b52f0/aio_databases-0.15.0.tar.gz",
"platform": null,
"description": "# AIO-Databases\n\nThe package gives you async support for a range of databases (SQLite,\nPostgreSQL, MySQL).\n\n[![Tests Status](https://github.com/klen/aio-databases/workflows/tests/badge.svg)](https://github.com/klen/aio-databases/actions)\n[![PYPI Version](https://img.shields.io/pypi/v/aio-databases)](https://pypi.org/project/aio-databases/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/aio-databases)](https://pypi.org/project/aio-databases/)\n\n## Features\n\n* Has no dependencies (except databases drivers)\n* Supports [asyncio](https://docs.python.org/3/library/asyncio.html) and [trio](https://github.com/python-trio/trio)\n* Supports [aiosqlite](https://github.com/omnilib/aiosqlite),\n [aiomysql](https://github.com/aio-libs/aiomysql),\n [aiopg](https://github.com/aio-libs/aiopg),\n [asyncpg](https://github.com/MagicStack/asyncpg),\n [triopg](https://github.com/python-trio/triopg),\n [trio_mysql](https://github.com/python-trio/trio-mysql)\n* Manage pools of connections\n* Manage transactions\n\n## Requirements\n\n* python >= 3.7\n\n## Installation\n\n**aio-databases** should be installed using pip:\n\n```shell\n$ pip install aio-databases\n```\n\nYou have to choose and install the required database drivers with:\n\n```shell\n# To support SQLite\n$ pip install aio-databases[aiosqlite] # asyncio\n\n# To support MySQL\n$ pip install aio-databases[aiomysql] # asyncio\n$ pip install aio-databases[trio_mysql] # trio\n\n# To support PostgreSQL (choose one)\n$ pip install aio-databases[aiopg] # asyncio\n$ pip install aio-databases[asyncpg] # asyncio\n$ pip install aio-databases[triopg] # trio\n\n# To support ODBC (alpha state)\n$ pip install aio-databases[aioodbc] # asyncio\n```\n\n\n## Usage\n\n### Init a database\n\n```python\n from aio_databases import Database\n\n # Initialize a database\n db = Database('sqlite:///:memory:') # with default driver\n\n # Flesh out the driver \n db = Database('aiosqlite:///:memory:', **driver_params)\n```\n\n### Setup a pool of connections (optional)\n\nSetup a pool of connections\n\n```python\n # Initialize a database's pool\n async def my_app_starts():\n await db.connect()\n\n # Close the pool\n async def my_app_ends():\n await db.disconnect()\n\n # As an alternative users are able to use the database\n # as an async context manager\n\n async with db:\n await my_main_coroutine()\n```\n\n### Get a connection\n\n```python\n # Acquire and release (on exit) a connection\n async with db.connection():\n await my_code()\n\n # Acquire a connection only if it not exist\n async with db.connection(False):\n await my_code()\n```\n\nIf a pool is setup it will be used\n\n### Run SQL queries\n\n```python\n await db.execute('select $1', '1')\n await db.executemany('select $1', '1', '2', '3')\n\n records = await db.fetchall('select (2 * $1) res', 2)\n assert records == [(4,)]\n\n record = await db.fetchone('select (2 * $1) res', 2)\n assert record == (4,)\n assert record['res'] == 4\n\n result = await db.fetchval('select 2 * $1', 2)\n assert result == 4\n```\n\n* Iterate through rows one by one\n\n```python\n\n async for rec in db.iterate('select name from users'):\n print(rec)\n\n```\n\n### Manage connections\n\nBy default the database opens and closes a connection for a query.\n\n```python\n # Connection will be acquired and released for the query\n await db.fetchone('select %s', 42)\n\n # Connection will be acquired and released again\n await db.fetchone('select %s', 77)\n```\n\nManually open and close a connection\n\n```python\n\n # Acquire a new connection object\n async with db.connection():\n # Only one connection will be used\n await db.fetchone('select %s', 42)\n await db.fetchone('select %s', 77)\n # ...\n\n # Acquire a new connection or use an existing\n async with db.connection(False):\n # ...\n```\n\nIf there any connection already `db.method` would be using the current one\n```python\n async with db.connection(): # connection would be acquired here\n await db.fetchone('select %s', 42) # the connection is used\n await db.fetchone('select %s', 77) # the connection is used\n\n # the connection released there\n```\n\n### Manage transactions\n\n```python\n # Start a tranction using the current connection\n async with db.transaction() as trans1:\n # do some work ...\n\n async with db.transaction() as trans2:\n # do some work ...\n await trans2.rollback()\n\n # unnessesary, the transaction will be commited on exit from the\n # current context\n\n await trans1.commit()\n\n # Create a new connection and start a transaction\n async with db.tranction(True) as trans:\n # do some work ...\n```\n\n## Bug tracker\n\nIf you have any suggestions, bug reports or annoyances please report them to\nthe issue tracker at https://github.com/klen/aio-databases/issues\n\n\n## Contributing\n\nDevelopment of the project happens at: https://github.com/klen/aio-databases\n\n\n## License\n\nLicensed under a [MIT License](http://opensource.org/licenses/MIT)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Async support for various databases",
"version": "0.15.0",
"project_urls": {
"Homepage": "https://github.com/klen/aio-databases",
"Repository": "https://github.com/klen/aio-databases"
},
"split_keywords": [
"asyncio",
"trio",
"databases",
"mysql",
"sqlite",
"postgres",
"postgresql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9f18b4d2c4e7cbb575c3346c4b1dfa0ec3c88f52ab8b2b233787affaa9e0a99a",
"md5": "6a9005717e20d184995759e1c3baedf4",
"sha256": "833195fa245ddec3f40b993918ec15cd76bd64cce576d9e9d986269f3bc64b3d"
},
"downloads": -1,
"filename": "aio_databases-0.15.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6a9005717e20d184995759e1c3baedf4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 16971,
"upload_time": "2023-07-07T13:31:26",
"upload_time_iso_8601": "2023-07-07T13:31:26.829981Z",
"url": "https://files.pythonhosted.org/packages/9f/18/b4d2c4e7cbb575c3346c4b1dfa0ec3c88f52ab8b2b233787affaa9e0a99a/aio_databases-0.15.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dcc722db93b9b9ce2d4b290ac54e958b5a2e81815e863bb47c6c0c946c6b52f0",
"md5": "42c1a650db6967d8db845e69deac3cff",
"sha256": "8f9d847685503c98b8d322c3f825573d43d1b8196a5ee25dc7454f702af57980"
},
"downloads": -1,
"filename": "aio_databases-0.15.0.tar.gz",
"has_sig": false,
"md5_digest": "42c1a650db6967d8db845e69deac3cff",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 11982,
"upload_time": "2023-07-07T13:31:28",
"upload_time_iso_8601": "2023-07-07T13:31:28.657773Z",
"url": "https://files.pythonhosted.org/packages/dc/c7/22db93b9b9ce2d4b290ac54e958b5a2e81815e863bb47c6c0c946c6b52f0/aio_databases-0.15.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-07 13:31:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "klen",
"github_project": "aio-databases",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aio-databases"
}