Skip to main content

SQLAlchemy 2.0 support for aiohttp.

Project description

Read The Docs build Release MIT License Python version support PyPI downloads count GitHub continuous integration

SQLAlchemy 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 different types of request handlers;

  • support nested applications.

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 datetime import datetime

import sqlalchemy as sa
from aiohttp import web
from sqlalchemy import orm

import aiohttp_sqlalchemy as ahsa


class Base(orm.DeclarativeBase): ...


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):
    sa_session = aiohttp_sqlalchemy.get_session(request)

    async with sa_session.begin():
        sa_session.add(MyModel())
        result = await sa_session.execute(sa.select(MyModel))
        result = result.scalars()

    data = {instance.pk: instance.timestamp.isoformat() for instance in result}
    return web.json_response(data)


async def app_factory():
    app = web.Application()

    aiohttp_sqlalchemy.setup(
        app,
        [
            aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///"),
        ],
    )
    await aiohttp_sqlalchemy.init_db(app, Base.metadata)

    app.add_routes([web.get("/", main)])
    return app


if __name__ == "__main__":
    web.run_app(app_factory(), port=8087)

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-1.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

aiohttp_sqlalchemy-1.1.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_sqlalchemy-1.1.1.tar.gz.

File metadata

  • Download URL: aiohttp_sqlalchemy-1.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-59-generic

File hashes

Hashes for aiohttp_sqlalchemy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 336d38a94913eea41698cc182f46f10ace0a040f0b03ca494077d75a4b817eec
MD5 123e5631f5c09334e54fdef47aa9cee8
BLAKE2b-256 bf17147fd611c2f5297baa594a8d8966c2147e27b6e00dc4ec923fc4d13812c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiohttp_sqlalchemy-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-59-generic

File hashes

Hashes for aiohttp_sqlalchemy-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 76872ba62267e63350cbfa16acc3c793939e8180059638bf6cdd40943de9625b
MD5 e377a4083c1791ea17d1ddfe99303565
BLAKE2b-256 0aca36a9232dbe0d9df520201171a8af2cfe214ee0d2528706f944ff45701eb5

See more details on using hashes here.

Supported by

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