You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
984 B

from typing import List
from fastapi import Depends, FastAPI
from typing_extensions import Self
class MyDatabaseConnection:
"""
This is a mock just for example purposes.
"""
async def __aenter__(self) -> Self:
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
async def get_records(self, table_name: str) -> List[dict]:
pass
app = FastAPI()
async def get_database_connection():
async with MyDatabaseConnection() as connection:
yield connection
GlobalDatabaseConnection = Depends(get_database_connection, dependency_scope="lifespan")
@app.get("/users/")
async def read_users(
database_connection: MyDatabaseConnection = GlobalDatabaseConnection,
):
return await database_connection.get_records("users")
@app.get("/items/")
async def read_items(
database_connection: MyDatabaseConnection = GlobalDatabaseConnection,
):
return await database_connection.get_records("items")