-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
59 lines (48 loc) · 1.3 KB
/
module.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
58
59
package db
import (
"github.com/im-kulikov/helium/module"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
"upper.io/db.v3/lib/sqlbuilder"
)
var (
// MySQLModule default connection to mysql
MySQLModule = module.Module{
{Constructor: newMySQLConnection},
}
// PostgresModule default connection to postgres
PostgresModule = module.Module{
{Constructor: newPostgresConnection},
}
)
func newMySQLConnection(v *viper.Viper, l *zap.Logger) (MySQL, error) {
opts, err := PrepareConfig("database.mysql", v, l)
if err != nil {
return nil, errors.Wrap(err, "could not prepare mysql config")
}
con, err := NewConnection(opts...)
if err != nil {
return nil, errors.Wrap(err, "could not create connection")
}
builder := con.(sqlbuilder.SQLBuilder)
return &mysqlConnection{
Database: con,
SQLBuilder: builder,
}, nil
}
func newPostgresConnection(v *viper.Viper, l *zap.Logger) (PG, error) {
opts, err := PrepareConfig("database.postgres", v, l)
if err != nil {
return nil, errors.Wrap(err, "could not prepare postgres config")
}
con, err := NewConnection(opts...)
if err != nil {
return nil, errors.Wrap(err, "could not create postgres connection")
}
builder := con.(sqlbuilder.SQLBuilder)
return &postgresConnection{
Database: con,
SQLBuilder: builder,
}, nil
}