/
datastore.go
58 lines (48 loc) · 1.12 KB
/
datastore.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
package datastore
import (
"log"
"github.com/jmoiron/sqlx"
//imported so pq can register its drivers with sql
_ "github.com/lib/pq"
)
//M is a map
type M map[string]interface{}
//Store holds all database collections
type Store struct {
DB *sqlx.DB
Collections map[string]*Collection
}
func exec(q string, db *sqlx.DB) error {
if _, err := db.Exec(q); err != nil {
log.Println("error with query: " + q)
log.Println(err)
return err
}
return nil
}
//NewConnection creates a sql database connection and contentful client
func NewConnection(uri string, seed func(db *sqlx.DB) error) (*Store, error) {
conn, err := sqlx.Open("postgres", uri)
if err != nil {
log.Println(err)
return nil, err
}
err = seed(conn)
if err != nil {
log.Println("seed error")
return nil, err
}
return &Store{
DB: conn,
}, nil
}
func (s *Store) AddCollection(tableName string) *Collection {
if s.Collections == nil {
s.Collections = make(map[string]*Collection)
}
if coll, ok := s.Collections[tableName]; ok {
return coll
}
s.Collections[tableName] = NewCollection(s.DB, tableName)
return s.Collections[tableName]
}