You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/examples/transaction/gorm/internal/data/data.go

72 lines
1.6 KiB

package data
import (
"context"
"github.com/go-kratos/kratos/examples/transaction/gorm/internal/biz"
"github.com/go-kratos/kratos/examples/transaction/gorm/internal/conf"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/wire"
"gorm.io/driver/mysql"
"gorm.io/gorm"
// init mysql driver
_ "github.com/go-sql-driver/mysql"
)
// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData, NewDB, NewTransaction, NewUserRepo, NewCardRepo)
// Data .
type Data struct {
db *gorm.DB
log *log.Helper
}
type contextTxKey struct{}
func (d *Data) ExecTx(ctx context.Context, fn func(ctx context.Context) error) error {
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
ctx = context.WithValue(ctx, contextTxKey{}, tx)
return fn(ctx)
})
}
func (d *Data) DB(ctx context.Context) *gorm.DB {
tx, ok := ctx.Value(contextTxKey{}).(*gorm.DB)
if ok {
return tx
}
return d.db
}
// NewTransaction .
func NewTransaction(d *Data) biz.Transaction {
return d
}
// NewData .
func NewData(db *gorm.DB, logger log.Logger) (*Data, func(), error) {
l := log.NewHelper(log.With(logger, "module", "transaction/data"))
d := &Data{
db: db,
log: l,
}
return d, func() {
}, nil
}
// NewDB gorm Connecting to a Database
func NewDB(conf *conf.Data, logger log.Logger) *gorm.DB {
log := log.NewHelper(log.With(logger, "module", "order-service/data/gorm"))
db, err := gorm.Open(mysql.Open(conf.Database.Source), &gorm.Config{})
if err != nil {
log.Fatalf("failed opening connection to mysql: %v", err)
}
if err := db.AutoMigrate(&User{}, &Card{}); err != nil {
log.Fatal(err)
}
return db
}