SQLAlchemy 1.4 / 2.0 support for aiohttp.
Project description
SQLAlchemy 1.4 / 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;
support for different types of request handlers.
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 aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_bind, sa_session
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
class MyModel(Base):
__tablename__ = 'my_table'
id = sa.Column(sa.Integer, primary_key=True)
timestamp = sa.Column(sa.DateTime(), default=datetime.now)
async def main(request):
async with sa_session(request).bind.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with sa_session(request).begin():
sa_session(request).add_all([MyModel()])
result = await sa_session(request).execute(sa.select(MyModel))
data = {r.id: r.timestamp.isoformat() for r in result.scalars()}
return web.json_response(data)
app = web.Application()
engine = create_async_engine('sqlite+aiosqlite:///')
Session = orm.sessionmaker(engine, AsyncSession)
aiohttp_sqlalchemy.setup(app, [sa_bind(Session)])
app.add_routes([web.get('/', main)])
if __name__ == '__main__':
web.run_app(app)
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
Close
Hashes for aiohttp-sqlalchemy-0.11.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04e00d2c71e4796917416deee8d1e20b1545ab836fe5db93be179bb05cbe2d17 |
|
MD5 | 0797ff81663bb70ae9e68370efad231d |
|
BLAKE2b-256 | 9606c84972075e4acbe7d0982232da3b15537af370c046c958c7a201f75f39b2 |
Close
Hashes for aiohttp_sqlalchemy-0.11.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd539c28f1949e3b87b899b51d0eea76f049206eb12f73b381db478bdd29efd9 |
|
MD5 | 52b2f7a82e8210cafe6bdcf946442729 |
|
BLAKE2b-256 | 1de38749770655af252ecd353a0b80053d8289574ff221b46b14fe7eb49273cb |