/
database.go
57 lines (46 loc) · 1.16 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
57
package postgres
import (
"context"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/rai-project/database"
"github.com/rai-project/database/relational"
"upper.io/db.v3/postgresql"
)
const (
gormDialect = "postgres"
)
type postgresDatabase struct {
database.Database
}
// NewDatabase ...
func NewDatabase(databaseName string, opts ...database.Option) (database.Database, error) {
options := database.Options{
Endpoints: Config.Endpoints,
Username: Config.Username,
Password: Config.Password,
TLSConfig: nil,
MaxConnections: Config.MaxConnections,
Context: context.Background(),
}
if Config.Certificate != "" {
database.TLSCertificate(Config.Certificate)(&options)
}
for _, o := range opts {
o(&options)
}
connectionURL := postgresql.ConnectionURL{
User: options.Username,
Password: options.Password,
Host: options.Endpoints[0],
Database: databaseName,
}
d, err := relational.NewDatabase(gormDialect, databaseName, connectionURL, options)
if err != nil {
return nil, err
}
return &postgresDatabase{d}, nil
}
// String ...
func (conn *postgresDatabase) String() string {
return "PostgreSQL"
}