-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-db.go
94 lines (83 loc) · 2.29 KB
/
read-db.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package datastore
import (
"cloud.google.com/go/datastore"
"context"
"fmt"
"github.com/uxland/go-cqrs-firestore/shared"
"google.golang.org/api/iterator"
)
type readDB struct {
kind string
namespace string
itemFactory shared.ItemFactory
client *datastore.Client
}
func NewGenericDBImpl(kind, namespace string, itemFactory shared.ItemFactory, client *datastore.Client) shared.GenericReadDB {
return &readDB{kind, namespace, itemFactory, client}
}
func (db *readDB) SaveItem(tx interface{}, id string, item interface{}) error {
transaction := tx.(*datastore.Transaction)
key := newKey(db.namespace, db.kind, id, nil)
key.Namespace = db.namespace
_, err := transaction.Put(key, item)
return err
}
func (db *readDB) readIterator(it *datastore.Iterator) ([]interface{}, error) {
docs := make([]interface{}, 0)
for {
item := db.itemFactory()
_, err := it.Next(item)
if err == iterator.Done {
break
}
if err != nil {
return docs, err
}
docs = append(docs, item)
}
return docs, nil
}
func (db *readDB) LoadAllItems() ([]interface{}, error) {
query := db.createQuery().
Limit(1000)
it := db.client.Run(context.Background(), query)
return db.readIterator(it)
}
func (db *readDB) LoadItem(id string) (interface{}, error) {
key := newKey(db.namespace, db.kind, id, nil)
key.Namespace = db.namespace
item := db.itemFactory()
err := db.client.Get(context.Background(), key, item)
if err != nil {
return nil, err
}
return item, nil
}
func (db *readDB) DeleteItem(tx interface{}, id string) error {
transaction := tx.(*datastore.Transaction)
key := newKey(db.namespace, db.kind, id, nil)
return transaction.Delete(key)
}
func (db *readDB) UpdateItem(transaction interface{}, id string, updates interface{}) error {
panic("implement me")
}
func (db *readDB) ListItems(filter []shared.Filter, limit int) ([]interface{}, error) {
query := db.createQuery()
for _, s := range filter {
op := s.Op
if op == "==" {
op = "="
}
query = query.Filter(fmt.Sprintf("%s %s", s.Path, op), s.Value)
}
query = query.Limit(limit)
it := db.client.Run(context.Background(), query)
return db.readIterator(it)
}
func (db *readDB) createQuery() *datastore.Query {
query := datastore.NewQuery(db.kind)
if db.namespace != "" {
query = query.Namespace(db.namespace)
}
return query
}