init
This commit is contained in:
25
app/database.py
Normal file
25
app/database.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from sqlmodel import SQLModel, Session, create_engine
|
||||
from app.config import DATABASE_URL
|
||||
|
||||
# SQLite requires check_same_thread=False for FastAPI
|
||||
_connect_args: dict = {}
|
||||
if DATABASE_URL.startswith("sqlite"):
|
||||
_connect_args = {"check_same_thread": False}
|
||||
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
echo=True,
|
||||
connect_args=_connect_args,
|
||||
)
|
||||
|
||||
|
||||
def create_db_and_tables() -> None:
|
||||
"""Create all tables from SQLModel metadata.
|
||||
Called once at application startup."""
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
||||
def get_session():
|
||||
"""FastAPI dependency — yields a SQLModel Session."""
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user