package database import ( "fmt" "gorm.io/driver/postgres" "gorm.io/gorm" "synoth.com/janus/internal/model" ) // Config 数据库配置 type Config struct { Host string Port int Username string Password string DBName string SSLMode string } // Connect 连接数据库 func Connect(cfg Config) (*gorm.DB, error) { dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.DBName, cfg.SSLMode, ) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { return nil, fmt.Errorf("connect database: %w", err) } return db, nil } // AutoMigrate 自动迁移数据库表结构 func AutoMigrate(db *gorm.DB) error { return db.AutoMigrate(&model.Provider{}, &model.APIKey{}, &model.Channel{}, &model.Model{}) }