Skip to main content

SQLAlchemy 1.4 / 2.0 support for aiohttp.

Project description

SQLAlchemy 1.4 / 2.0 support for aiohttp.

Library forward a sqlalchemy.ext.asyncio.AsyncSession object as request['sa_main'] or SAView.sa_session() by default.

Documentation

https://aiohttp-sqlalchemy.readthedocs.io/

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 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 request['sa_main'].bind.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:///')
Session = orm.sessionmaker(engine, AsyncSession)
aiohttp_sqlalchemy.setup(app, [sa_bind(Session)])

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

aiohttp-sqlalchemy-0.7.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_sqlalchemy-0.7.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-sqlalchemy-0.7.0.tar.gz.

File metadata

  • Download URL: aiohttp-sqlalchemy-0.7.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.5 Linux/5.4.0-74-generic

File hashes

Hashes for aiohttp-sqlalchemy-0.7.0.tar.gz
Algorithm Hash digest
SHA256 6801c250caff7f7cde8d30ccd0e01bee71e234d34f066166a605dc50a9b5e5e0
MD5 6fccd7421426c65b534f8647ebaa93eb
BLAKE2b-256 aefc9e72d3a23a2a4ae40d59138e09b474261a19481341154da1c2b51877b660

See more details on using hashes here.

File details

Details for the file aiohttp_sqlalchemy-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_sqlalchemy-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.5 Linux/5.4.0-74-generic

File hashes

Hashes for aiohttp_sqlalchemy-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79f2474861c44328a7ecf89c78b1a71f57224cbbe0f13f07208855eef1aa8822
MD5 417ef2b3e8f9d70173ca6a47b77ba089
BLAKE2b-256 fb03348f583c42f564f2761d2299afc5cce37456f8922df691a9d6feb0606628

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page