"""数据库配置模型""" from loguru import logger from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy import Integer, String, Text, Boolean, JSON from ..db.base import BaseModel # 在现有的DatabaseConfig类中添加关系 from sqlalchemy.orm import relationship class DatabaseConfig(BaseModel): """数据库配置表""" __tablename__ = "database_configs" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) name: Mapped[str] = mapped_column(String(100), nullable=False) # 配置名称 db_type: Mapped[str] = mapped_column(String(20), nullable=False, unique=True) # 数据库类型:postgresql, mysql等 host: Mapped[str] = mapped_column(String(255), nullable=False) port: Mapped[int] = mapped_column(Integer, nullable=False) database: Mapped[str] = mapped_column(String(100), nullable=False) username: Mapped[str] = mapped_column(String(100), nullable=False) password: Mapped[str] = mapped_column(Text, nullable=False) # 加密存储 is_active: Mapped[bool] = mapped_column(Boolean, default=True) is_default: Mapped[bool] = mapped_column(Boolean, default=False) connection_params: Mapped[dict | None] = mapped_column(JSON, nullable=True) # 额外连接参数 def to_dict(self, include_password=False, decrypt_service=None): result = { "id": self.id, "created_by": self.created_by, "name": self.name, "db_type": self.db_type, "host": self.host, "port": self.port, "database": self.database, "username": self.username, "is_active": self.is_active, "is_default": self.is_default, "connection_params": self.connection_params, "created_at": self.created_at.isoformat() if self.created_at else None, "updated_at": self.updated_at.isoformat() if self.updated_at else None } # 如果需要包含密码且提供了解密服务 if include_password and decrypt_service: logger.info(f"begin decrypt password for db config {self.id}") result["password"] = decrypt_service._decrypt_password(self.password) return result # 添加关系 # table_metadata = relationship("TableMetadata", back_populates="database_config")