Initial commit
This commit is contained in:
35
backend/app/database.py
Normal file
35
backend/app/database.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import declarative_base
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Try loading ../db.conf into environment variables
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
DB_CONF = PROJECT_ROOT / "db.conf"
|
||||
if DB_CONF.exists():
|
||||
load_dotenv(DB_CONF)
|
||||
else:
|
||||
load_dotenv()
|
||||
|
||||
DB_HOST = os.getenv("PSQL_HOST", "localhost")
|
||||
DB_PORT = os.getenv("PSQL_PORT", "5432")
|
||||
DB_USER = os.getenv("PSQL_USER", "postgres")
|
||||
DB_PASSWORD = os.getenv("PSQL_PASSWORD", "")
|
||||
DB_NAME = os.getenv("PSQL_DBNAME", "ffxi_items")
|
||||
|
||||
DATABASE_URL = (
|
||||
f"postgresql+asyncpg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
||||
)
|
||||
|
||||
engine = create_async_engine(DATABASE_URL, echo=False, pool_size=10, max_overflow=20)
|
||||
AsyncSessionLocal: async_sessionmaker[AsyncSession] = async_sessionmaker(
|
||||
bind=engine, expire_on_commit=False
|
||||
)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
async def get_session() -> AsyncSession:
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user