Skip to main content

SQLAlchemy 1.4 / 2.0 support for aiohttp.

Project description

SQLAlchemy 1.4 / 2.0 support for aiohttp.

By default, library provides:

  • AsyncSession as request['sa_main'] or SAView.sa_main_session

  • AsyncEngine as request.app['sa_main']

Documentation

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

Installation

pip install aiohttp-sqlalchemy

Simple example

Install aiosqlite for work with sqlite3:

pip install aiosqlite

Run this code:

from aiohttp import web
import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import sa_engine, sa_middleware
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import 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.app['sa_main'].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:///')
aiohttp_sqlalchemy.setup(app, [sa_engine(engine)])

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.1b2.tar.gz (4.3 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.1b2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-sqlalchemy-0.1b2.tar.gz.

File metadata

  • Download URL: aiohttp-sqlalchemy-0.1b2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/5.4.0-70-generic

File hashes

Hashes for aiohttp-sqlalchemy-0.1b2.tar.gz
Algorithm Hash digest
SHA256 9c1c7d1712dffbbfd1b2103413f298d0bd0312460945f3dce6242dafca769a50
MD5 3ecad9cf7524c471abd2e1b6459487d9
BLAKE2b-256 9c1e1308c24b3ec6265e92fecdbbfae110318d58cdded536f9b0461d1197f9f5

See more details on using hashes here.

File details

Details for the file aiohttp_sqlalchemy-0.1b2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_sqlalchemy-0.1b2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/5.4.0-70-generic

File hashes

Hashes for aiohttp_sqlalchemy-0.1b2-py3-none-any.whl
Algorithm Hash digest
SHA256 8734e04d850d319a2e7e544212c8af7ec5d53cb90847bc1de640cd86017b3909
MD5 7ba1d8f4b639222669c63cd8dfec3201
BLAKE2b-256 71c33cd91f6af576d33841864cae27eff0ed68a0f49851e4dde19359cac0f948

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