SQLAlchemy 1.4 / 2.0 support for aiohttp.
Project description
SQLAlchemy 1.4 / 2.0 support for aiohttp.
By default, library forwards:
sqlalchemy.ext.asyncio.AsyncSession object as request['sa_main'] or SAView.sa_main_session
sqlalchemy.ext.asyncio.AsyncEngine object as request.app['sa_main']
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_middleware
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import 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 request.app['sa_main'].begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with request['sa_main'].begin():
request['sa_main'].add_all([MyModel()])
result = await request['sa_main'].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:///')
aiohttp_sqlalchemy.setup(app, [sa_bind(engine)])
app.add_routes([web.get('/', 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.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16a869303fd9f2ad1624ea999b81d37db1e548d9274c8584a33f49be56a01bc2 |
|
MD5 | d57d3160440572b46ceaa75d5ff30b23 |
|
BLAKE2b-256 | aa39b469b8e42aece37dec29709802d50e7fa9c7c34c461ead0fc5fe0296be6e |