完成基本的创建使用功能

This commit is contained in:
2024-09-11 20:19:47 +08:00
parent 28a84ad4d7
commit 951572a1f5
22 changed files with 783 additions and 31 deletions
+27
View File
@@ -0,0 +1,27 @@
package gormx
import (
"errors"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Config struct {
Type string `ini:"type"`
DSN string `ini:"dsn"`
}
func New(cfg Config) (db *gorm.DB, err error) {
switch cfg.Type {
case "sqlite3":
// dsn := "exe.db"
db, err = gorm.Open(sqlite.Open(cfg.DSN), &gorm.Config{})
case "mysql":
// dsn := "userRepo:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err = gorm.Open(mysql.Open(cfg.DSN), &gorm.Config{})
default:
err = errors.New("mode not supported")
}
return
}