File tree Expand file tree Collapse file tree
src/cool_seq_tool/sources Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313 >>> braf_exists
1414 True
1515
16+ The class breakdown in this module is intended to reflect a *repository pattern*, where
17+ query/data transformation logic is separated from connection lifespan management. See the
18+ following example for how you might employ this in a FastAPI app:
19+
20+ .. code-block:: python
21+
22+ from collections.abc import AsyncGenerator, Generator
23+ from contextlib import asynccontextmanager
24+ from typing import Annotated
25+
26+ from fastapi import FastAPI, Request, Depends
27+ from cool_seq_tool.sources.uta_database import (
28+ UtaRepository,
29+ create_uta_connection_pool,
30+ )
31+
32+ app = FastAPI()
33+
34+
35+ @asynccontextmanager
36+ async def lifespan(app: FastAPI) -> AsyncGenerator:
37+ uta_pool = await create_uta_connection_pool()
38+ app.state.uta_pool = uta_pool
39+ yield
40+ await uta_pool.close()
41+
42+
43+ # dependency function
44+ async def get_uta(request: Request) -> AsyncGenerator[UtaRepository, None, None]:
45+ async with request.app.state.uta_pool.connection() as conn:
46+ yield UtaRepository(conn)
47+
48+
49+ @app.get("/check_gene_exists")
50+ async def check_gene_exists(
51+ gene: str, uta: Annotated[UtaRepository, Depends(get_uta)]
52+ ):
53+ return await uta.gene_exists(gene)
54+
1655"""
1756
1857import ast
You can’t perform that action at this time.
0 commit comments