Total Lines | 36 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 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 |