I found a great example of transactions between Repositories using a Clean Architecture approach.
This guy is using Gorm .
Gorm has the same type for a DB connection and a transaction, example:
var db *gorm.DB
var tx *gorm.DB
I'm a fan of go-pg
. But here the types are different (maybe it's even better as is), example:
var db *pg.DB
var tx *pg.Tx
And of course the error is: Cannot use 'tx' (type *Tx) as type *pg.DB
A small reproduction:
package main
import (
"github.com/go-pg/pg/v10"
)
type Player struct {
ID int
Name string
}
type PlayerRepo struct {
db *pg.DB
teamRepo *TeamRepo
}
type TeamRepo struct {
db *pg.DB
}
func NewPlayerRepo(db *pg.DB) *PlayerRepo {
return &PlayerRepo{
db: db,
teamRepo: NewTeamRepo(db),
}
}
func NewTeamRepo(db *pg.DB) *TeamRepo {
return &TeamRepo{db: db}
}
func (r *PlayerRepo) Find(id int) (*Player, error) {
var player Player
err := r.db.Model(&player).Where("id = ?", id).Select()
if err != nil {
return nil, err
}
return &player, nil
}
func (r *PlayerRepo) All() ([]*Player, error) {
// Long code
return nil, nil
}
func (r *PlayerRepo) Insert() (*Player, error) {
// Long code
return nil, nil
}
func (r *PlayerRepo) Update() (*Player, error) {
// Long code
return nil, nil
}
func (r *PlayerRepo) Delete() (*Player, error) {
// Long code
return nil, nil
}
func (r *PlayerRepo) WithTransaction(txFunc func(*PlayerRepo) error) (err error) {
tx, _ := r.db.Begin()
manager := NewPlayerRepo(tx) // <<<--- here the problem! tx is not good here, it's `pg.Tx` not `pg.DB`
err = txFunc(manager)
return
}
What can I do to fix this?
Thanks in advance. ❤️
You can define an interface that is already, implicitly implemented by both:
type DB interface {
Begin() (*Tx, error)
Close() error
Context() context.Context
CopyFrom(r io.Reader, query interface{}, params ...interface{}) (res Result, err error)
CopyTo(w io.Writer, query interface{}, params ...interface{}) (res Result, err error)
Exec(query interface{}, params ...interface{}) (Result, error)
ExecContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
ExecOne(query interface{}, params ...interface{}) (Result, error)
ExecOneContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
Formatter() orm.QueryFormatter
Model(model ...interface{}) *orm.Query
ModelContext(c context.Context, model ...interface{}) *orm.Query
Prepare(q string) (*Stmt, error)
Query(model interface{}, query interface{}, params ...interface{}) (Result, error)
QueryContext(c context.Context, model interface{}, query interface{}, params ...interface{}) (Result, error)
QueryOne(model interface{}, query interface{}, params ...interface{}) (Result, error)
QueryOneContext(c context.Context, model interface{}, query interface{}, params ...interface{}) (Result, error)
RunInTransaction(ctx context.Context, fn func(*Tx) error) error
}
NOTE: I only know that the method names match, I didn't bother checking if the signatures also do, if they don't, you'll need to edit the interface accordingly.
You can add a simple "compiler check":
var _ DB = (*pg.DB)(nil)
var _ DB = (*pg.Tx)(nil)
And then you can change the type of the PlayerRepo.db
field from *pg.DB
to your new DB
interface.
type PlayerRepo struct {
db DB
teamRepo *TeamRepo
}
type TeamRepo struct {
db DB
}
func NewPlayerRepo(db DB) *PlayerRepo {
return &PlayerRepo{
db: db,
teamRepo: NewTeamRepo(db),
}
}
func NewTeamRepo(db DB) *TeamRepo {
return &TeamRepo{db: db}
}
func (r *PlayerRepo) WithTransaction(txFunc func(*PlayerRepo) error) (err error) {
tx, err := r.db.Begin()
if err != nil {
return err
}
defer func() {
// rollback if err; commit if no err
}()
manager := NewPlayerRepo(tx)
err = txFunc(manager)
return
}
If your repo types need to be able to invoke some of the methods that are not common to both pg.DB
and pg.Tx
, and therefore not defined by the new DB
interface, then, one approach would be to retain the original types for such use, for example:
type PlayerRepo struct {
db DB
pg *pg.DB
teamRepo *TeamRepo
}
type TeamRepo struct {
db DB
pg *pg.DB
}
func NewPlayerRepo(db DB, pg *pg.DB) *PlayerRepo {
return &PlayerRepo{
db: db,
pg: pg,
teamRepo: NewTeamRepo(db, pg),
}
}
func NewTeamRepo(db DB, pg *pg.DB) *TeamRepo {
return &TeamRepo{db: db, pg: pg}
}
func (r *PlayerRepo) WithTransaction(txFunc func(*PlayerRepo) error) (err error) {
tx, err := r.db.Begin()
if err != nil {
return err
}
defer func() {
// rollback if err; commit if no err
}()
manager := NewPlayerRepo(tx, r.pg)
err = txFunc(manager)
return
}
Note that if you decide to use orm.DB
, which is reasonable, but it is missing some of the methods that you need and that are already implemented by both pg.DB
and pg.Tx
, then you could embed orm.DB
into your custom interface and add only those methods that are missing.
type DB interface {
Begin() (*Tx, error)
orm.DB
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.