openreplay/api/NOTES.md
ⵄⵎⵉⵔⵓⵛ 2119761aa3
fix(api): store async postgresql pool as a fastapi state. (#1763)
* fix(api): store async postgresql pool as a fastapi state.

* fix(api): avoid circular import
2023-12-11 14:59:02 +01:00

25 lines
826 B
Markdown

#### psycopg3 API
I mis-remember the psycopg v2 vs. v3 API.
For the record, the expected psycopg3's async api looks like the
following pseudo code:
```python
async with app.state.postgresql.connection() as cnx:
async with cnx.transaction():
row = await cnx.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
row = await row.fetchone()
return row["exists"]
```
Minding the following:
- Where `app.state.postgresql` is the postgresql connection pooler.
- Wrap explicit transaction with `async with cnx.transaction():
foobar()`
- Most of the time the transaction object is not used;
- Do execute await operation against `cnx`;
- `await cnx.execute` returns a cursor object;
- Do the `await cursor.fetchqux...` calls against the object return by
a call to execute.