Phase 4 구현 완료: Neo4j 벡터 검색 + 그래프 저장소
This commit is contained in:
49
ontology_platform/ont_platform/api/db_deps.py
Normal file
49
ontology_platform/ont_platform/api/db_deps.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""Database dependencies for FastAPI."""
|
||||
|
||||
from typing import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from ont_platform.config import load_settings
|
||||
|
||||
# Initialize database engine (lazy singleton)
|
||||
_engine = None
|
||||
_SessionLocal = None
|
||||
|
||||
|
||||
def get_db_engine():
|
||||
"""Get or create database engine."""
|
||||
global _engine
|
||||
if _engine is None:
|
||||
settings = load_settings()
|
||||
database_url = settings.database_url
|
||||
_engine = create_engine(
|
||||
database_url,
|
||||
connect_args={"timeout": 30} if "sqlite" in database_url else {},
|
||||
pool_pre_ping=True,
|
||||
echo=False,
|
||||
)
|
||||
return _engine
|
||||
|
||||
|
||||
def get_session_factory():
|
||||
"""Get or create session factory."""
|
||||
global _SessionLocal
|
||||
if _SessionLocal is None:
|
||||
engine = get_db_engine()
|
||||
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
return _SessionLocal
|
||||
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
"""FastAPI dependency for database session."""
|
||||
SessionLocal = get_session_factory()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
__all__ = ["get_db", "get_db_engine", "get_session_factory"]
|
||||
Reference in New Issue
Block a user