-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathplayer_database.py
More file actions
41 lines (32 loc) · 1.17 KB
/
player_database.py
File metadata and controls
41 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# ------------------------------------------------------------------------------
# Database
# ------------------------------------------------------------------------------
import logging
import os
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
storage_path = os.getenv("STORAGE_PATH", "./storage/players-sqlite3.db")
DATABASE_URL = f"sqlite+aiosqlite:///{storage_path}"
logger = logging.getLogger("uvicorn")
logging.getLogger("sqlalchemy.engine.Engine").handlers = logger.handlers
async_engine = create_async_engine(
DATABASE_URL,
connect_args={"check_same_thread": False},
echo=True
)
async_sessionmaker = sessionmaker(
bind=async_engine,
class_=AsyncSession,
autocommit=False,
autoflush=False
)
Base = declarative_base()
async def generate_async_session() -> AsyncGenerator[AsyncSession, None]:
"""
Dependency function to yield an async SQLAlchemy ORM session.
Yields:
AsyncSession: An instance of an async SQLAlchemy ORM session.
"""
async with async_sessionmaker() as async_session:
yield async_session