-
Notifications
You must be signed in to change notification settings - Fork 0
/
oven.go
206 lines (162 loc) · 4.58 KB
/
oven.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package oven
import (
"context"
"github.com/pkg/errors"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cloud.google.com/go/firestore"
)
const (
// MaxBatchSize is the maximum number of items that can be batched together.
MaxBatchSize = 500
agentName = "oven"
)
var (
// ErrDataNotFound is thrown when query does not find the requested data.
ErrDataNotFound = errors.New("data not found")
)
func isDataNotFoundError(err error) bool {
return err != nil && status.Code(err) == codes.NotFound
}
// GetClient returns instantiated Firestore client.
func GetClient(ctx context.Context, projectID string) (client *firestore.Client, err error) {
return firestore.NewClient(ctx, projectID, option.WithUserAgent(agentName))
}
// GetClientOrPanic returns instantiated Firestore client or panics on error.
func GetClientOrPanic(ctx context.Context, projectID string) *firestore.Client {
c, err := firestore.NewClient(ctx, projectID, option.WithUserAgent(agentName))
if err != nil {
panic(err)
}
return c
}
// GetCollection returns specific store collection by name.
func GetCollection(client *firestore.Client, name string) (col *firestore.CollectionRef, err error) {
if client == nil {
return nil, errors.New("nil client")
}
if name == "" {
return nil, errors.New("collection name required")
}
return client.Collection(name), nil
}
// Delete deletes specific record by id.
func Delete(ctx context.Context, client *firestore.Client, col, id string) error {
if client == nil {
return errors.New("nil client")
}
if id == "" {
return errors.New("nil id")
}
c, err := GetCollection(client, col)
if err != nil {
return err
}
_, err = c.Doc(id).Delete(ctx)
if isDataNotFoundError(err) {
return nil
}
if err != nil {
return errors.Wrapf(err, "error deleting %s record with id %s", col, id)
}
return nil
}
// Get sets the in parameter to specific store record by id.
func Get[T any](ctx context.Context, client *firestore.Client, col, id string) (*T, error) {
if client == nil {
return nil, errors.New("nil client")
}
if id == "" {
return nil, errors.New("id required")
}
c, err := GetCollection(client, col)
if err != nil {
return nil, err
}
d, err := c.Doc(id).Get(ctx)
if err != nil {
if isDataNotFoundError(err) {
return nil, ErrDataNotFound
}
return nil, errors.Wrapf(err, "error getting %s record with id %s", col, id)
}
if d == nil || d.Data() == nil {
return nil, errors.Errorf("record with id %s found in %s collection but has not data", id, col)
}
var out T
if err := d.DataTo(&out); err != nil {
return nil, errors.Wrapf(err, "data in %s for id %s is in an incorrect format", col, id)
}
return &out, nil
}
// Save saves the data to the store.
func Save[T any](ctx context.Context, client *firestore.Client, col, id string, in *T) error {
if client == nil {
return errors.New("nil client")
}
if in == nil {
return errors.New("nil in state to save")
}
c, err := GetCollection(client, col)
if err != nil {
return err
}
_, err = c.Doc(id).Set(ctx, in)
if err != nil {
return errors.Wrapf(err, "error saving %s record with id %s", col, id)
}
return nil
}
// Update updates the data in the store.
func Update(ctx context.Context, client *firestore.Client, col, id string, args map[string]interface{}) error {
if client == nil {
return errors.New("nil client")
}
if col == "" || id == "" {
return errors.New("nil collection or id in update")
}
c, err := GetCollection(client, col)
if err != nil {
return err
}
updates := make([]firestore.Update, 0)
for k, v := range args {
updates = append(updates, firestore.Update{Path: k, Value: v})
}
_, err = c.Doc(id).Update(ctx, updates)
if err != nil {
return errors.Wrapf(err, "error updating %s record with id %s", col, id)
}
return nil
}
type Identifiable interface {
GetID() string
}
func BatchSet[T Identifiable](ctx context.Context, client *firestore.Client, col string, items ...T) error {
if client == nil {
return errors.New("nil client")
}
if col == "" {
return errors.New("nil collection name")
}
batchSize := len(items)
if batchSize == 0 {
return nil
}
if batchSize > MaxBatchSize {
return errors.Errorf("batch size %d exceeds max batch size %d", batchSize, MaxBatchSize)
}
c, err := GetCollection(client, col)
if err != nil {
return errors.Wrap(err, "error getting collection")
}
b := client.Batch()
for _, item := range items {
b.Set(c.Doc(item.GetID()), item)
}
if _, err := b.Commit(ctx); err != nil {
return errors.Wrapf(err, "error batch setting %d records on %s", batchSize, c.ID)
}
return nil
}