test
This commit is contained in:
0
backend/__init__.py
Normal file
0
backend/__init__.py
Normal file
10
backend/config.py
Normal file
10
backend/config.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
|
||||
load_dotenv(find_dotenv())
|
||||
PG_LOGIN = os.environ.get('PG_LOGIN')
|
||||
PG_PASSWORD = os.environ.get('PG_PASSWORD')
|
||||
PG_PORT = os.environ.get('PG_PORT')
|
||||
PG_HOST = os.environ.get('PG_HOST')
|
||||
PG_DATABASE = os.environ.get('PG_DATABASE')
|
||||
24
backend/session.py
Normal file
24
backend/session.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
|
||||
from .config import (
|
||||
PG_HOST,
|
||||
PG_LOGIN,
|
||||
PG_PORT,
|
||||
PG_DATABASE,
|
||||
PG_PASSWORD,
|
||||
)
|
||||
|
||||
database_url = (
|
||||
f"postgresql+asyncpg://"
|
||||
f"{PG_LOGIN}:{PG_PASSWORD}@"
|
||||
f"{PG_HOST}:{PG_PORT}/{PG_DATABASE}"
|
||||
)
|
||||
engine = create_async_engine(database_url)
|
||||
session_factory = async_sessionmaker(engine, expire_on_commit=False, autoflush=False, autocommit=False)
|
||||
|
||||
|
||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with session_factory() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user