Skip to content

AnswerDotAI/fastasyncpg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastasyncpg

Installation

Install latest from pypi

$ pip install fastasyncpg

How to use

Create 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.

About

A simple wrapper for asyncpg

Resources

License

Stars

3 stars

Watchers

0 watching

Forks

Contributors