Skip to main content

A thin Trio-backed wrapper for SQLAlchemy's asyncpg PostgreSQL driver

Project description

sqlalchemy-triopg

PyPI - Downloads GitHub Buy a tree

A thin Trio-backed wrapper for SQLAlchemy's asyncpg PostgreSQL driver.

This wrapper is named after the existing raw asyncpg wrapper triopg, but does not actually use it. If you want to use asyncpg directly, use that package instead.

$ pdm add sqlalchemy-triopg
# or
$ pip install --user sqlalchemy-triopg

Usage

SQLAlchemy is able to automatically discover the dialect available through this package as soon as you install it. When creating your engine, just pass the triopg driver like so:

engine = create_async_engine("triopg://postgres:password@db/postgres")

Requisites

This package uses the trio-asyncio package to provide a compatibility layer with asyncio; it is what enables the "wrapping" in favor of just a re-implementation. However it also means that your program needs to ensure the Trio event loop is running, either via manually calling your program with trio_asyncio.run or by wrapping your main coroutine definition in trio_asyncio.open_loop.

For many projects like web apps, the terminal command for starting the application does not let you change how to start the ASGI server. Aside from creating a custom worker class and specifying it (like a custom Hypercorn or Gunicorn worker), you'll need to create a bootstrapping script where you can manually call trio_asyncio.run to your server's API.

This is because, unfortunately, binding an async context manager to the lifecycle of your application (a feature supported by both BlackSheep and FastAPI) in which you call trio_asyncio.open_loop doesn't seem to work.

Using Hypercorn's API, an example boot.py script could look like this:

#!/bin/python

import trio_asyncio
from hypercorn.config import Config
from hypercorn.trio import serve

from application import app

if __name__ == "__main__":
    trio_asyncio.run(serve, app, Config.from_toml("application.toml"))

Considerations

This package uses trio-asyncio, and you'll need to take that into account if you're combining Trio and existing asyncio tooling. If those tools do not use a bridge library like anyio, it's very likely that you'll need to extend them the coroutines by wrapping them in trio_asyncio.aio_as_trio (or using it as a decorator on your own coroutine functions).

This is an intentional anti-feature of trio-asyncio.

License

This package makes use of SQLAlchemy and Trio-Asyncio, both of which are licensed under MIT.

This package is licensed under the BSD 3-Clause Clear License.

This package is Treeware. If you use it in production, consider buying the world a tree to thank me for my work. By contributing to my forest, you’ll be creating employment for local families and restoring wildlife habitats.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_triopg-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_triopg-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_triopg-1.0.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_triopg-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.8.2 CPython/3.11.2

File hashes

Hashes for sqlalchemy_triopg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 400b07d9aa71b704cc3d0d3551d511c70486c6b5142a5a0ff21637e64caed427
MD5 e5f7f8dc4514a6ba6cbcf4257928992c
BLAKE2b-256 a01899bc5934a374826d294210785a0b3dfa5927f3348179821033fc2f7be926

See more details on using hashes here.

File details

Details for the file sqlalchemy_triopg-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_triopg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 afaedc89f217d8abf2ceeeabe87f23454ae3f71b8d2bbd4b005586acf6dce0b9
MD5 1c4dd1d6d82d42b59af23f41a69ba7cc
BLAKE2b-256 dfb4c54e6ab1fe4865d7890cbe75d3b84d3d018d7ffa86a9535f01b628485ffc

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