forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
33 lines (23 loc) · 817 Bytes
/
Copy pathdatabase.py
File metadata and controls
33 lines (23 loc) · 817 Bytes
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
"""
Database configuration and session management.
"""
from typing import Annotated, Generator
from fastapi import Depends
from sqlmodel import Session, SQLModel, create_engine
# Database configuration
DATABASE_URL = "sqlite:///sns_api.db"
# Create engine with SQLite-specific configuration
engine = create_engine(
DATABASE_URL,
echo=True, # Log SQL queries for debugging
connect_args={"check_same_thread": False} # Allow SQLite to be used with FastAPI
)
def create_db_and_tables():
"""Create database tables."""
SQLModel.metadata.create_all(engine)
def get_session() -> Generator[Session, None, None]:
"""Get database session dependency."""
with Session(engine) as session:
yield session
# Session dependency type
SessionDep = Annotated[Session, Depends(get_session)]