feat: add gorm utils.
This commit is contained in:
48
gorm/go.mod
Normal file
48
gorm/go.mod
Normal file
@@ -0,0 +1,48 @@
|
||||
module github.com/tx7do/go-utils/gorm
|
||||
|
||||
go 1.21
|
||||
|
||||
toolchain go1.22.1
|
||||
|
||||
require (
|
||||
gorm.io/driver/clickhouse v0.6.0
|
||||
gorm.io/driver/mysql v1.5.6
|
||||
gorm.io/driver/postgres v1.5.7
|
||||
gorm.io/driver/sqlite v1.5.5
|
||||
gorm.io/driver/sqlserver v1.5.3
|
||||
gorm.io/gorm v1.25.10
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/ch-go v0.58.2 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.15.0 // indirect
|
||||
github.com/andybalholm/brotli v1.0.6 // indirect
|
||||
github.com/go-faster/city v1.0.1 // indirect
|
||||
github.com/go-faster/errors v0.6.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.0 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/google/uuid v1.3.1 // indirect
|
||||
github.com/hashicorp/go-version v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/klauspost/compress v1.16.7 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.17 // indirect
|
||||
github.com/microsoft/go-mssqldb v1.6.0 // indirect
|
||||
github.com/paulmach/orb v0.10.0 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.18 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/segmentio/asm v1.2.0 // indirect
|
||||
github.com/shopspring/decimal v1.3.1 // indirect
|
||||
go.opentelemetry.io/otel v1.19.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.19.0 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/tx7do/go-utils => ../
|
||||
2926
gorm/go.sum
Normal file
2926
gorm/go.sum
Normal file
File diff suppressed because it is too large
Load Diff
77
gorm/gorm_client.go
Normal file
77
gorm/gorm_client.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"gorm.io/driver/clickhouse"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/driver/sqlserver"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
*gorm.DB
|
||||
|
||||
err error
|
||||
}
|
||||
|
||||
func NewClient(driverName, dsn string, enableMigrate bool, gormCfg *gorm.Config) *Client {
|
||||
c := &Client{}
|
||||
|
||||
if gormCfg == nil {
|
||||
gormCfg = &gorm.Config{}
|
||||
}
|
||||
|
||||
c.err = c.createGormClient(driverName, dsn, enableMigrate, gormCfg)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Error() error {
|
||||
return c.err
|
||||
}
|
||||
|
||||
// createGormClient 创建GORM的客户端
|
||||
func (c *Client) createGormClient(driverName, dsn string, enableMigrate bool, gormCfg *gorm.Config) error {
|
||||
var driver gorm.Dialector
|
||||
switch driverName {
|
||||
default:
|
||||
fallthrough
|
||||
case "mysql":
|
||||
driver = mysql.Open(dsn)
|
||||
break
|
||||
case "postgres":
|
||||
driver = postgres.Open(dsn)
|
||||
break
|
||||
case "clickhouse":
|
||||
driver = clickhouse.Open(dsn)
|
||||
break
|
||||
case "sqlite":
|
||||
driver = sqlite.Open(dsn)
|
||||
break
|
||||
case "sqlserver":
|
||||
driver = sqlserver.Open(dsn)
|
||||
break
|
||||
}
|
||||
|
||||
client, err := gorm.Open(driver, gormCfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed opening connection to db: %v", err)
|
||||
}
|
||||
|
||||
// 运行数据库迁移工具
|
||||
if enableMigrate {
|
||||
if err = client.AutoMigrate(
|
||||
getMigrateModels()...,
|
||||
); err != nil {
|
||||
return fmt.Errorf("failed creating schema resources: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
c.DB = client
|
||||
|
||||
return nil
|
||||
}
|
||||
13
gorm/migrates.go
Normal file
13
gorm/migrates.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package gorm
|
||||
|
||||
var migrateModels []interface{}
|
||||
|
||||
// RegisterMigrateModel 注册用于数据库迁移的数据库模型
|
||||
func RegisterMigrateModel(model interface{}) {
|
||||
migrateModels = append(migrateModels, &model)
|
||||
}
|
||||
|
||||
// getMigrateModels 获取用于数据库迁移的数据库模型
|
||||
func getMigrateModels() []interface{} {
|
||||
return migrateModels
|
||||
}
|
||||
Reference in New Issue
Block a user