/
data.go
50 lines (44 loc) · 1.09 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package data
import (
"database/sql"
"github.com/hoquangnam45/pharmacy-common-go/helper/common"
h "github.com/hoquangnam45/pharmacy-common-go/util/errorHandler"
"github.com/hoquangnam45/pharmacy-user/internal/conf"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"github.com/google/wire"
"github.com/hoquangnam45/pharmacy-common-go/util/log"
)
// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData, NewLoginDetailRepo, NewRefreshTokenRepo, NewTransactionManager, NewClientRepo)
// Data .
type Data struct {
*gorm.DB
}
// NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
dbConfig := c.Database
db := common.InitializePostgresDb(
dbConfig.Host,
dbConfig.Username,
dbConfig.Password,
dbConfig.Database,
int(dbConfig.Port),
&gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "user.",
SingularTable: true,
},
},
dbConfig.MigratePath,
1)
cleanup := func() {
h.FlatMap(
h.FactoryM(db.DB),
h.PeekE(func(con *sql.DB) error {
return con.Close()
}),
).PanicEval()
}
return &Data{db}, cleanup, nil
}