/
factory.go
37 lines (30 loc) · 872 Bytes
/
factory.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
package database
import (
"context"
"github.com/alexdunne/gs-onboarding/internal/models"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
)
// Database is a interface to expose methods to fetch and store items
type Database interface {
GetAll(ctx context.Context) ([]models.Item, error)
GetStories(ctx context.Context) ([]models.Item, error)
GetJobs(ctx context.Context) ([]models.Item, error)
Write(ctx context.Context, item models.Item) error
}
// Client for database
type Client struct {
pool *pgxpool.Pool
}
// New starts db connection
func New(ctx context.Context, connStr string) (*Client, error) {
pool, err := pgxpool.Connect(ctx, connStr)
if err != nil {
return nil, errors.Wrap(err, "failed connecting to the database")
}
return &Client{pool: pool}, nil
}
// Close closes db connection
func (c *Client) Close() {
c.pool.Close()
}