45 lines
823 B
Go
45 lines
823 B
Go
|
package tx
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/glebarez/sqlite"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"gorm.io/gorm"
|
||
|
"nf-repo/internal/interfaces"
|
||
|
)
|
||
|
|
||
|
type tx struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
|
||
|
func (t *tx) TX(ctx context.Context) *gorm.DB {
|
||
|
return t.db.Session(&gorm.Session{}).WithContext(ctx)
|
||
|
}
|
||
|
|
||
|
func newTX(db *gorm.DB) interfaces.Database {
|
||
|
return &tx{db: db}
|
||
|
}
|
||
|
|
||
|
func Must(database interfaces.Database, err error) interfaces.Database {
|
||
|
if err != nil {
|
||
|
logrus.
|
||
|
WithField("path", "tx.Must").
|
||
|
WithField("err", err.Error()).
|
||
|
Panic()
|
||
|
}
|
||
|
|
||
|
if database == nil {
|
||
|
logrus.
|
||
|
WithField("path", "tx.Must").
|
||
|
WithField("err", "database is nil").
|
||
|
Panic()
|
||
|
}
|
||
|
|
||
|
return database
|
||
|
}
|
||
|
|
||
|
func NewSqliteTX(filepath string) (interfaces.Database, error) {
|
||
|
db, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{})
|
||
|
return newTX(db), err
|
||
|
}
|