2023-11-17 11:35:09 +05:30
|
|
|
from contextlib import asynccontextmanager
|
2023-12-16 00:49:01 +05:30
|
|
|
from typing import Annotated
|
2023-11-17 11:35:09 +05:30
|
|
|
|
2023-11-06 17:04:17 +05:30
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
2023-12-16 00:49:01 +05:30
|
|
|
from fastapi import Depends
|
2023-11-06 17:04:17 +05:30
|
|
|
import pandas as pd
|
|
|
|
|
2023-11-17 11:35:09 +05:30
|
|
|
from .config import conf
|
|
|
|
|
2023-11-06 17:04:17 +05:30
|
|
|
echo = False
|
|
|
|
pg_url = "postgresql+asyncpg://avgis@localhost/avgis"
|
|
|
|
|
2023-11-17 11:35:09 +05:30
|
|
|
engine = create_async_engine(
|
|
|
|
pg_url,
|
|
|
|
echo=echo,
|
|
|
|
pool_size=conf.db.pool_size,
|
|
|
|
max_overflow=conf.db.max_overflow,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def get_db_session() -> AsyncSession:
|
|
|
|
async with AsyncSession(engine) as session:
|
|
|
|
yield session
|
2023-11-06 17:04:17 +05:30
|
|
|
|
2023-11-17 11:35:09 +05:30
|
|
|
@asynccontextmanager
|
|
|
|
async def db_session() -> AsyncSession:
|
2023-11-06 17:04:17 +05:30
|
|
|
async with AsyncSession(engine) as session:
|
|
|
|
yield session
|
|
|
|
|
|
|
|
def pandas_query(session, query):
|
2023-12-16 00:49:01 +05:30
|
|
|
return pd.read_sql_query(query, session.connection())
|
|
|
|
|
|
|
|
fastapi_db_session = Annotated[AsyncSession, Depends(get_db_session)]
|