Skip to main content

SQLAlchemy 1.4 / 2.0 support for aiohttp.

Project description

Documentation Status https://img.shields.io/badge/Python-3.7%20%7C%203.8%20%7C%203.9-blue Downloads count https://travis-ci.com/ri-gilfanov/aiohttp-sqlalchemy.svg?branch=master https://coveralls.io/repos/github/ri-gilfanov/aiohttp-sqlalchemy/badge.svg?branch=master

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

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_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

aiohttp-sqlalchemy-0.11.2.tar.gz (5.6 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.11.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiohttp-sqlalchemy-0.11.2.tar.gz
  • Upload date:
  • Size: 5.6 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.11.2.tar.gz
Algorithm Hash digest
SHA256 aa7c11cc2053933bd85b237eda6cec5a8218e60f5c37f40d56ce15bfc1a6bf52
MD5 30c192f46e9239a9ba43507883d0bf6f
BLAKE2b-256 b3799653405bd0f42b8de33576a6543ba61bbda5a0f59b3a38fdfd1e4b52da25

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiohttp_sqlalchemy-0.11.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.11.2-py3-none-any.whl
Algorithm Hash digest
SHA256 533070be9b2c106e9a9031892c7ad5474eacddb4f088ab0c36c2591665dc3072
MD5 76d537378087926b7c2c63d9befa2bad
BLAKE2b-256 6f4527de289292ba6d2dccc32803686e7c5efe0cfa4dc2eb8dad1a33ae9ffca6

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