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.2.0.tar.gz (6.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-1.2.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aiohttp_sqlalchemy-1.2.0.tar.gz
Algorithm Hash digest
SHA256 37e812728a25d0e509e2ffec5382ca948e2c2b361aa17340939a46684b9231cc
MD5 e7acf76c301c27b6516688ee6b1f5167
BLAKE2b-256 80396ac244be2a9246974a224aaaf38a01def4ef34b8f1f005b17d4724c51ead

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for aiohttp_sqlalchemy-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 219b66cc3c11f1d1e6eb61fd2cdefd8ed7172a79cf856cbd5f7bb586dd3b5326
MD5 6c33f6ea642549b81e1ad888e256d52f
BLAKE2b-256 2fad5e2dbb7b543629db752ffe10f2b0ae41f994484b55fc5c0eee4271bc3344

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