Created
July 25, 2024 09:47
-
-
Save fliepeltje/5a2b63d4b1169f5cb200a665cee3f926 to your computer and use it in GitHub Desktop.
FastAPI app with asyncpg minimal example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import asyncpg | |
from fastapi import FastAPI | |
DATABASE_URL = os.getenv("DATABASE_URL") | |
app = FastAPI() | |
async def get_db_meta(): | |
conn = await asyncpg.connect(DATABASE_URL) | |
records = await conn.fetch("select nspname from pg_namespace;") | |
return records | |
@app.get("/") | |
async def read_root(): | |
db_meta = await get_db_meta() | |
names = [record["nspname"] for record in db_meta] | |
return {"names": names} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment