-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
56 lines (49 loc) · 1.33 KB
/
database.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
51
52
53
54
55
56
package utils
import (
"fmt"
// PostgreSQL driver
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
)
// DatabaseConfig represents a configuration to connect to an SQL database
type DatabaseConfig struct {
Host string
Port string
SSLMode string
Name string
Username string
Password string
}
// NewDB Initialises the connection to the database
func NewDB(config DatabaseConfig) (*sqlx.DB, error) {
dbURI := fmt.Sprintf("dbname=%s host=%s user=%s password=%s port=%s sslmode=%s application_name=web-api",
config.Name, config.Host, config.Username, config.Password, config.Port, config.SSLMode) // Build connection string
db, err := sqlx.Open("postgres", dbURI)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
return db, nil
}
// Transact wraps transactions
func Transact(db *sqlx.DB, txFunc func(*sqlx.Tx) error) (err error) {
tx, err := db.Beginx()
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
tx.Rollback() // err is non-nil; don't chang eit
} else {
err = tx.Commit() // err is nil; if Commit returns error update err
}
}()
err = txFunc(tx)
return err
}