-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
63 lines (52 loc) · 1.26 KB
/
common.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
61
62
63
package model
import (
"os"
"log"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type Stores struct {
db *sqlx.DB
Protocol *ProtocolStore
Chain *ChainStore
ProtocolInstance *ProtocolInstanceStore
Account *AccountStore
Event *EventStore
Token *TokenStore
}
func (s Stores) Close() {
s.db.Close()
}
func NewStores() (Stores, error) {
db, err := DbConnect()
if err != nil {
return Stores{}, errors.Wrap(err, "Unable to setup Stores")
}
stores := GenerateStores(db)
return stores, err
}
func GenerateStores(db *sqlx.DB) Stores {
protocolStore := NewProtocolStore(db)
chainStore := NewChainStore(db)
protocolInstanceStore := NewProtocolInstanceStore(db)
accountStore := NewAccountStore(db)
eventStore := NewEventStore(db)
tokenStore := NewTokenStore(db)
stores := Stores{
Protocol: protocolStore,
Chain: chainStore,
ProtocolInstance: protocolInstanceStore,
Account: accountStore,
Event: eventStore,
Token: tokenStore,
db: db,
}
return stores
}
func DbConnect() (*sqlx.DB, error) {
url := os.Getenv("DATABASE_URL")
if len(url) == 0 {
log.Println("DATABASE_URL is not set")
}
return sqlx.Connect("pgx", url)
}