Created
February 3, 2020 08:30
-
-
Save iKlotho/37da0fe6d0a76ea60db7977f90928ce0 to your computer and use it in GitHub Desktop.
fastapi - asyncpg
This file contains hidden or 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
async def setup_database(): | |
global connection_pool | |
connection_pool = await asyncpg.create_pool( | |
user=os.getenv('PGUSER', 'x'), | |
password=os.getenv('PGPASS', 'xx'), | |
database='xx', | |
host='xxx', | |
port=5432 | |
) | |
connection_pool = None | |
app = FastAPI() | |
@app.on_event("startup") | |
async def startup(): | |
await setup_database() | |
@app.get("/ir/", response_model=List[Note]) | |
async def read_notes(): | |
async with connection_pool.acquire() as connection: | |
await connection.execute("SET search_path TO 'xx'") | |
data = await connection.fetch(READ_IR_SQL) | |
print("data", data) | |
return {'data': 'gee'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment