-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.go
60 lines (50 loc) · 1.32 KB
/
connections.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
60
// Copyright (C) 2022, Chain4Travel AG. All rights reserved.
//
// This file is a derived work, based on ava-labs code whose
// original notices appear below.
//
// It is distributed under the same license conditions as the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********************************************************
// (c) 2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"context"
"fmt"
"github.com/lasthyphen/dijetsnodego/utils/wrappers"
"github.com/lasthyphen/mages/cfg"
)
type Connections struct {
Eventer *EventRcvr
db *Conn
}
func NewDBFromConfig(conf cfg.Services, ro bool) (*Connections, error) {
var (
dbConn *Conn
err error
)
eventer := &EventRcvr{}
if conf.DB != nil || conf.DB.Driver == DriverNone {
// Create connection
dbConn, err = New(eventer, *conf.DB, ro)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("invalid database")
}
return &Connections{
db: dbConn,
Eventer: eventer,
}, nil
}
func (c Connections) Stream() *EventRcvr { return c.Eventer }
func (c Connections) DB() *Conn { return c.db }
func (c Connections) Close() error {
errs := wrappers.Errs{}
errs.Add(c.db.Close(context.Background()))
return errs.Err
}