-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
44 lines (36 loc) · 1013 Bytes
/
db.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
// Package db provides utilities to configure and connect to a database
package db
import (
"log"
"github.com/rs401/lg/auth/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// ConnectionError custom error type
type ConnectionError struct{}
// Error implements the error interface
func (ce *ConnectionError) Error() string {
return "error connecting to database"
}
// Connection interface defines a method for retrieving the *gorm.DB
type Connection interface {
DB() *gorm.DB
}
type conn struct {
db *gorm.DB
}
// DB implements the Connection interface
func (c *conn) DB() *gorm.DB {
return c.db
}
// NewConnection takes a db.Config and returns a Connection and an error
func NewConnection(cfg Config) (Connection, error) {
dbc, err := gorm.Open(postgres.Open(cfg.ConnStr()), &gorm.Config{})
if err != nil {
log.Printf("Error, could not connect to database: %v", err)
return nil, &ConnectionError{}
}
// I guess do this here
dbc.AutoMigrate(&models.User{})
return &conn{db: dbc}, nil
}