repository.NewPostgresDB   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 1
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 12
rs 9.95
c 0
b 0
f 0
1
package repository
2
3
import (
4
	"fmt"
5
6
	"github.com/jmoiron/sqlx"
7
	_ "github.com/lib/pq"
8
)
9
10
const (
11
	roomsTable    = "rooms"
12
	bookingsTable = "bookings"
13
)
14
15
type Config struct {
16
	Host     string
17
	Port     string
18
	Username string
19
	Password string
20
	DBName   string
21
	SSLMode  string
22
}
23
24
func NewPostgresDB(cfg Config) (*sqlx.DB, error) {
25
	db, err := sqlx.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s",
26
		cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))
27
	if err != nil {
28
		return nil, err
29
	}
30
31
	err = db.Ping()
32
	if err != nil {
33
		return nil, err
34
	}
35
36
	return db, nil
37
}
38