-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.go
More file actions
51 lines (46 loc) · 911 Bytes
/
postgres.go
File metadata and controls
51 lines (46 loc) · 911 Bytes
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
51
package gg
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// PostgresConfig represents a Postgres Database configuration
type PostgresConfig struct {
Host string
Port int
User string
DatabaseName string
Password string
SSL bool
TimeZone string
}
func (cfg PostgresConfig) Configure() gorm.Dialector {
return postgres.Open(cfg.GetDSN())
}
func (cfg *PostgresConfig) setDefaults() {
if cfg.Host == "" {
cfg.Host = "localhost"
}
if cfg.Port == 0 {
cfg.Port = 5432
}
if cfg.TimeZone == "" {
cfg.TimeZone = "UTC"
}
}
func (cfg PostgresConfig) GetDSN() string {
sslMode := "disable"
if cfg.SSL {
sslMode = "enable"
}
cfg.setDefaults()
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
cfg.Host,
cfg.User,
cfg.Password,
cfg.DatabaseName,
cfg.Port,
sslMode,
cfg.TimeZone,
)
}