Initial commit: SIBU 2.0 MISSION
This commit is contained in:
27
backend/app/core/database.py
Normal file
27
backend/app/core/database.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Database connection and session management."""
|
||||
from sqlmodel import SQLModel, create_engine, Session
|
||||
from typing import Generator
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
# Create database engine
|
||||
# Convert asyncpg URL to psycopg2 for synchronous operations
|
||||
database_url = settings.database_url.replace("+asyncpg", "+psycopg2")
|
||||
engine = create_engine(
|
||||
database_url,
|
||||
echo=settings.debug,
|
||||
future=True,
|
||||
)
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
"""Initialize database by creating all tables."""
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
||||
def get_session() -> Generator[Session, None, None]:
|
||||
"""Dependency for getting database session."""
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
Reference in New Issue
Block a user