Install latest from pypi
$ pip install fastasyncpgCreate one Database at import time, and connect it at app startup:
from fastasyncpg.core import Database
db = Database() # module-level singleton, importable everywhere
async def startup(): await db.connect(database='mydb')Queries run through the pool by default. transaction (and acquire) make a single connection current for the running task via a ContextVar, so everything inside the block – including table helpers and code called indirectly – joins the transaction, with no handle-passing:
async with db.transaction():
await db.execute('SELECT 1 FROM "user" WHERE id=$1 FOR UPDATE', uid)
await db.t.user.update(pk_values=[uid], credits=100)See the API docs for table access (db.t), auto-generated dataclasses, queue helpers (claim_one), and more.