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):
db_session = sa_session(request)
async with db_session.bind.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
async with db_session.begin():
db_session.add_all([MyModel()])
result = await db_session.execute(sa.select(MyModel))
data = {record.id: record.timestamp.isoformat()
for record 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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf3cb92e8062656b309dfad3919d38f1d725777c56fc240c6e2b7f3091627459 |
|
MD5 | d190eb156c42ef8e29cc109604e64773 |
|
BLAKE2b-256 | 7160c61b8c47e45290577082d11f6105cb2eddd0de394600b51067d37b5ed111 |
Close
Hashes for aiohttp_sqlalchemy-0.11.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ffdcb1cc35b441de7fb5455d779940214636e467dcf77003a7fac25679e1f91 |
|
MD5 | 230a3fa2399f7728b212bc57262bfa11 |
|
BLAKE2b-256 | 31711d8f73ed44602acc011ed85ca62632a7f65247efd84e8e86f40d60cdbaf8 |