SQLAlchemy 2.0 support for AIOHTTP.
The library provides the next features:
initializing asynchronous sessions through a middlewares;
initializing asynchronous sessions through a decorators;
simple access to one asynchronous session by default key;
preventing attributes from being expired after commit by default;
support different types of request handlers;
support nested applications.
Documentation
Installation
pip install aiohttp-sqlalchemy
Simple example
Install aiosqlite for work with sqlite3:
pip install aiosqlite
Copy and paste this code in a file and run:
from datetime import datetime
import sqlalchemy as sa
from aiohttp import web
from sqlalchemy import orm
import aiohttp_sqlalchemy as ahsa
class Base(orm.DeclarativeBase): ...
class MyModel(Base):
__tablename__ = "my_table"
pk = sa.Column(sa.Integer, primary_key=True)
timestamp = sa.Column(sa.DateTime(), default=datetime.now)
async def main(request):
sa_session = aiohttp_sqlalchemy.get_session(request)
async with sa_session.begin():
sa_session.add(MyModel())
result = await sa_session.execute(sa.select(MyModel))
result = result.scalars()
data = {instance.pk: instance.timestamp.isoformat() for instance in result}
return web.json_response(data)
async def app_factory():
app = web.Application()
aiohttp_sqlalchemy.setup(
app,
[
aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///"),
],
)
await aiohttp_sqlalchemy.init_db(app, Base.metadata)
app.add_routes([web.get("/", main)])
return app
if __name__ == "__main__":
web.run_app(app_factory(), port=8087)
Release files for aiohttp-sqlalchemy 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiohttp_sqlalchemy-1.2.0.tar.gz | 6.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiohttp_sqlalchemy-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / aiohttp_sqlalchemy-1.2.0.tar.gz
| Download URL | aiohttp_sqlalchemy-1.2.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37e812728a25d0e509e2ffec5382ca948e2c2b361aa17340939a46684b9231cc
|
|
BLAKE2b-256 checksum How to use checksums |
80396ac244be2a9246974a224aaaf38a01def4ef34b8f1f005b17d4724c51ead
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.12.3 Linux/6.8.0-90-generic
|
Release files / aiohttp_sqlalchemy-1.2.0-py3-none-any.whl
| Download URL | aiohttp_sqlalchemy-1.2.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
219b66cc3c11f1d1e6eb61fd2cdefd8ed7172a79cf856cbd5f7bb586dd3b5326
|
|
BLAKE2b-256 checksum How to use checksums |
2fad5e2dbb7b543629db752ffe10f2b0ae41f994484b55fc5c0eee4271bc3344
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.12.3 Linux/6.8.0-90-generic
|