Skip to content

Commit 34142f5

Browse files
committed
Move database config struct to database package
1 parent 6275cbb commit 34142f5

3 files changed

Lines changed: 29 additions & 33 deletions

File tree

config.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

connections/database/interface.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package database
22

3-
import "gorm.io/gorm"
3+
import (
4+
"fmt"
5+
"gorm.io/gorm"
6+
)
47

58
type DBConnection interface {
69
GetDB() *gorm.DB
@@ -14,3 +17,22 @@ type IDBQueryClient interface {
1417
type DBQueryClient struct {
1518
Client IDBQueryClient
1619
}
20+
21+
type DBConnectionParams struct {
22+
User string
23+
Password string
24+
Name string
25+
Host string
26+
Port string
27+
}
28+
29+
type GraphqlClientParams struct {
30+
Host string
31+
Token string
32+
}
33+
34+
func (p *DBConnectionParams) GetDSN() (string, error) {
35+
return fmt.Sprintf(
36+
"user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
37+
p.User, p.Password, p.Name, p.Host, p.Port), nil
38+
}

connections/database/postgres.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package database
22

33
import (
44
"fmt"
5-
"github.com/Zondax/zindexer"
65
"gorm.io/driver/postgres"
76
"gorm.io/gorm"
87
)
@@ -11,7 +10,11 @@ type GormConnection struct {
1110
db *gorm.DB
1211
}
1312

14-
func NewPostgresConnection(params *zindexer.DBConnectionParams, config *zindexer.DBConnectionConfig) (*GormConnection, error) {
13+
type DBConnectionConfig struct {
14+
Gorm *gorm.Config
15+
}
16+
17+
func NewPostgresConnection(params *DBConnectionParams, config *DBConnectionConfig) (*GormConnection, error) {
1518
dsn, err := params.GetDSN()
1619
if err != nil {
1720
return nil, fmt.Errorf("failed to retrieve dsn")
@@ -32,7 +35,7 @@ func (c *GormConnection) GetDB() *gorm.DB {
3235
return c.db
3336
}
3437

35-
func ConnectDB(params zindexer.DBConnectionParams, config zindexer.DBConnectionConfig) (*gorm.DB, error) {
38+
func ConnectDB(params DBConnectionParams, config DBConnectionConfig) (*gorm.DB, error) {
3639
conn, err := NewPostgresConnection(&params, &config)
3740
if err != nil {
3841
return nil, err

0 commit comments

Comments
 (0)