Inject DB into DataStore, instead of hardcode the dependency

This commit is contained in:
Deluan 2021-07-31 20:15:20 -04:00
parent c0fc36da63
commit 344d7a4392
4 changed files with 26 additions and 22 deletions

View file

@ -2,6 +2,7 @@ package persistence
import (
"context"
"database/sql"
"reflect"
"github.com/astaxie/beego/orm"
@ -12,10 +13,11 @@ import (
type SQLStore struct {
orm orm.Ormer
db *sql.DB
}
func New() model.DataStore {
return &SQLStore{}
func New(db *sql.DB) model.DataStore {
return &SQLStore{db: db}
}
func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
@ -100,7 +102,7 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe
}
func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
if err != nil {
return err
}
@ -177,7 +179,7 @@ func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
func (s *SQLStore) getOrmer() orm.Ormer {
if s.orm == nil {
o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
if err != nil {
log.Error("Error obtaining new orm instance", err)
}