Initial commit: Tianpu Zero-Carbon EMS Platform
Full-stack energy management system for Tianpu Daxing campus. - Frontend: React 19 + TypeScript + Ant Design + ECharts - Backend: FastAPI + SQLAlchemy + PostgreSQL/TimescaleDB - Features: PV monitoring, heat pump management, carbon tracking, alarms, reports Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
29
backend/app/core/database.py
Normal file
29
backend/app/core/database.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from app.core.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
db_url = settings.DATABASE_URL_LOCAL if settings.DATABASE_URL_LOCAL else settings.DATABASE_URL
|
||||
|
||||
engine_kwargs = {"echo": settings.DEBUG}
|
||||
if "sqlite" not in db_url:
|
||||
engine_kwargs["pool_size"] = 20
|
||||
engine_kwargs["max_overflow"] = 10
|
||||
|
||||
engine = create_async_engine(db_url, **engine_kwargs)
|
||||
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with async_session() as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
Reference in New Issue
Block a user