project structure and database
This commit is contained in:
21
backend/session.py
Normal file
21
backend/session.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from .config import PG_DATABASE, PG_HOST, PG_PASSWORD, PG_LOGIN
|
||||
|
||||
DATABASE_URL = f"postgresql+asyncpg://{PG_LOGIN}:{PG_PASSWORD}@{PG_HOST}/{PG_DATABASE}"
|
||||
engine = create_async_engine(DATABASE_URL)
|
||||
session_maker = sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
||||
|
||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with session_maker() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user