Refactor API
This commit is contained in:
parent
9c813cdfbe
commit
8aa4828239
21 changed files with 731 additions and 363 deletions
26
API/utils/database.py
Normal file
26
API/utils/database.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from os import getenv
|
||||
from typing import AsyncGenerator
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker,AsyncEngine
|
||||
|
||||
engine: AsyncEngine = create_async_engine(
|
||||
#Todo Изменить на env, плдключение к db
|
||||
f"postgresql+asyncpg://spectral:spectral@localhost:5432/spectral",
|
||||
echo=True, #Todo удалить
|
||||
pool_pre_ping=True
|
||||
)
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
bind=engine,
|
||||
class_=AsyncSession,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
Loading…
Add table
Add a link
Reference in a new issue