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;
preventing attributes from being expired after commit by default;
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
metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
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):
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))
items = result.scalars().all()
data = {}
for item in items:
data[item.pk] = item.timestamp.isoformat()
return web.json_response(data)
app = web.Application()
binding = sa_bind('sqlite+aiosqlite:///')
aiohttp_sqlalchemy.setup(app, [binding])
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.13.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e5e3fec737c28fecf7be8f5dbf3f9316cc21e4e8b96d5dcc0f09f60d28eb39a |
|
MD5 | 8299121c2aac9de17b6e36a0c637a5cd |
|
BLAKE2b-256 | c18465def36cd3db158b56c020fd8ded04224490bc2b2966bf441967ac6c3237 |
Close
Hashes for aiohttp_sqlalchemy-0.13.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9effcc468dfc4b17a43041f1db642520cd29d43ecf9125e404531a8d7391595 |
|
MD5 | c47c09c4c3cf2574e81111a4b79c5216 |
|
BLAKE2b-256 | a6740666a3c3ad26b22fd482a99078fc1db9e0b63bc75a4168c43e5923497e47 |