-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
firestore.go
359 lines (317 loc) · 8.88 KB
/
firestore.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Package firestore implements dstore.Documents backed by firestore.
package firestore
import (
"context"
"encoding/json"
"net/url"
"strings"
"cloud.google.com/go/firestore"
"github.com/keys-pub/keys"
"github.com/keys-pub/keys/dstore"
"github.com/keys-pub/keys/dstore/events"
"github.com/pkg/errors"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ dstore.Documents = &Firestore{}
var _ events.Events = &Firestore{}
// Firestore is a DocumentStore implemented on Google Cloud Firestore.
type Firestore struct {
uri string
client *firestore.Client
}
// New creates a Firestore
func New(uri string, opts ...option.ClientOption) (*Firestore, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
if u.Scheme != "firestore" {
return nil, errors.Errorf("invalid scheme, should be like firestore://projectid")
}
projectID := u.Host
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID, opts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to create firestore client")
}
fs := &Firestore{
uri: uri,
client: client,
}
return fs, nil
}
// URI ...
func (f *Firestore) URI() string {
return f.uri
}
// Create document.
//
// Paths can be nested as long as they are even length components.
// For example,
//
// collection1/key1 (OK)
// collection1/key1/collection2/key2 (OK)
// collection1 (INVALID)
// collection1/key1/collection2 (INVALID)
//
func (f *Firestore) Create(ctx context.Context, path string, values map[string]interface{}) error {
fn := func() error { return f.create(ctx, path, values) }
return keys.RetryE(fn)
}
// Set document.
// Will create or set, overwriting any existing data.
//
// Paths can be nested as long as they are even length components.
// For example,
//
// collection1/key1 (OK)
// collection1/key1/collection2/key2 (OK)
// collection1 (INVALID)
// collection1/key1/collection2 (INVALID)
//
func (f *Firestore) Set(ctx context.Context, path string, values map[string]interface{}, opt ...dstore.SetOption) error {
opts := dstore.NewSetOptions(opt...)
fn := func() error { return f.set(ctx, path, values, opts) }
return keys.RetryE(fn)
}
func normalizePath(p string) string {
path := dstore.Path(p)
path = strings.TrimPrefix(path, "/")
return path
}
func (f *Firestore) create(ctx context.Context, path string, values map[string]interface{}) error {
logger.Infof(ctx, "Create (Firestore) %s", path)
path, err := checkPath(path)
if err != nil {
return err
}
doc := f.client.Doc(normalizePath(path))
if _, err := doc.Create(ctx, values); err != nil {
st, ok := status.FromError(err)
if !ok {
return errors.Wrapf(processError(err), "failed to create firestore value")
}
switch st.Code() {
case codes.AlreadyExists:
return dstore.NewErrPathExists(path)
default:
return errors.Wrapf(processError(err), "failed to create firestore document")
}
}
return nil
}
func checkPath(path string) (string, error) {
path = dstore.Path(path)
if len(dstore.PathComponents(path))%2 != 0 {
return "", errors.Errorf("invalid path %s", path)
}
if path == "/" {
return "", errors.Errorf("invalid path /")
}
return path, nil
}
func (f *Firestore) set(ctx context.Context, path string, values map[string]interface{}, opts dstore.SetOptions) error {
logger.Infof(ctx, "Set (Firestore) %s", path)
path, err := checkPath(path)
if err != nil {
return err
}
fopts := []firestore.SetOption{}
if opts.MergeAll {
fopts = append(fopts, firestore.MergeAll)
}
doc := f.client.Doc(normalizePath(path))
if _, err := doc.Set(ctx, values, fopts...); err != nil {
return errors.Wrapf(processError(err), "failed to set firestore document")
}
return nil
}
// GetAll paths.
func (f *Firestore) GetAll(ctx context.Context, paths []string) ([]*dstore.Document, error) {
refs := make([]*firestore.DocumentRef, 0, len(paths))
for _, p := range paths {
p = normalizePath(p)
ref := f.client.Doc(p)
refs = append(refs, ref)
}
res, err := f.client.GetAll(ctx, refs)
if err != nil {
return nil, err
}
out := make([]*dstore.Document, 0, len(res))
for _, doc := range res {
if !doc.Exists() {
continue
}
pc := dstore.PathComponents(doc.Ref.Path)
// TODO: Is there an easier way to get the path?
path := dstore.Path(pc[5:])
newDoc := dstore.NewDocument(path).With(doc.Data())
newDoc.CreatedAt = doc.CreateTime
newDoc.UpdatedAt = doc.UpdateTime
out = append(out, newDoc)
}
return out, nil
}
// Get ...
func (f *Firestore) Get(ctx context.Context, path string) (*dstore.Document, error) {
logger.Infof(ctx, "Get (Firestore) %s", path)
path, err := checkPath(path)
if err != nil {
return nil, err
}
doc, err := f.get(ctx, path)
if err != nil {
return nil, err
}
if doc == nil {
return nil, nil
}
logger.Debugf(ctx, "Create time %s", doc.CreateTime)
logger.Debugf(ctx, "Update time %s", doc.UpdateTime)
out := dstore.NewDocument(path).With(doc.Data())
out.CreatedAt = doc.CreateTime
out.UpdatedAt = doc.UpdateTime
return out, nil
}
// Exists returns true if path exists.
func (f *Firestore) Exists(ctx context.Context, path string) (bool, error) {
doc, err := f.get(ctx, path)
if err != nil {
return false, err
}
return doc != nil, nil
}
// Load path into value.
func (f *Firestore) Load(ctx context.Context, path string, v interface{}) (bool, error) {
return dstore.Load(ctx, f, path, v)
}
func (f *Firestore) get(ctx context.Context, path string) (*firestore.DocumentSnapshot, error) {
path = normalizePath(path)
doc, err := f.client.Doc(path).Get(ctx)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, nil
}
return nil, err
}
return doc, nil
}
func (f *Firestore) txGet(tx *firestore.Transaction, path string) (*firestore.DocumentSnapshot, error) {
path = normalizePath(path)
doc := f.client.Doc(path)
res, err := tx.Get(doc)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, nil
}
return nil, err
}
return res, nil
}
// DocumentIterator ...
func (f *Firestore) DocumentIterator(ctx context.Context, parent string, opt ...dstore.Option) (dstore.Iterator, error) {
opts := dstore.NewOptions(opt...)
// TODO: Handle context Done()
path := normalizePath(parent)
if path == "" {
return nil, errors.Errorf("list root not supported")
}
logger.Infof(ctx, "Query (firestore) %q (%+v)...", path, opts)
col := f.client.Collection(path)
if col == nil {
return &docsIterator{parent: path}, nil
}
q := col.Offset(0)
if opts.Prefix != "" {
q = q.Where(firestore.DocumentID, ">=", col.Doc(opts.Prefix))
}
if opts.Where != nil {
q = q.Where(opts.Where.Name, opts.Where.Op, opts.Where.Value)
}
// if opts.OrderBy != "" {
// q = col.OrderBy(opts.OrderBy, firestore.Asc)
// }
// if opts.StartAt != "" {
// q = q.StartAt(opts.StartAt)
// }
if opts.Index > 0 {
q = q.Offset(opts.Index)
}
if opts.Limit > 0 {
q = q.Limit(opts.Limit)
}
iter := q.Documents(ctx)
return &docsIterator{iter: iter, parent: path, prefix: opts.Prefix, pathOnly: opts.NoData}, nil
}
// Documents not implemented on Firestore, use DocumentIterator.
func (f *Firestore) Documents(ctx context.Context, parent string, opt ...dstore.Option) ([]*dstore.Document, error) {
return nil, errors.Errorf("unsupported: use DocumentIterator instead")
}
// processError tries to unmarshal Firebase JSON error, if it fails it returns
// what was passed in.
func processError(ferr error) error {
if strings.HasPrefix(ferr.Error(), "{") {
var jsonErr struct{ Error string }
if err := json.Unmarshal([]byte(ferr.Error()), &jsonErr); err == nil {
if jsonErr.Error != "" {
return errors.Errorf("firestore error: %s", jsonErr.Error)
}
}
}
return ferr
}
// Collections ...
func (f *Firestore) Collections(ctx context.Context, parent string) ([]*dstore.Collection, error) {
if dstore.Path(parent) != "/" {
return nil, errors.Errorf("only root collections supported")
}
iter := f.client.Collections(ctx)
cols := []*dstore.Collection{}
for {
col, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
cols = append(cols, &dstore.Collection{Path: dstore.Path(col.ID)})
}
return cols, nil
}
// Delete ...
func (f *Firestore) Delete(ctx context.Context, path string) (bool, error) {
return f.delete(ctx, path)
}
func (f *Firestore) delete(ctx context.Context, path string) (bool, error) {
path = normalizePath(path)
if len(dstore.PathComponents(path))%2 != 0 {
return false, errors.Errorf("invalid path %s", path)
}
exists, err := f.Exists(ctx, path)
if err != nil {
return false, err
}
if !exists {
return false, nil
}
doc := f.client.Doc(path)
_, err = doc.Delete(ctx)
if err != nil {
return false, err
}
return true, nil
}
// DeleteAll ...
func (f *Firestore) DeleteAll(ctx context.Context, paths []string) error {
for _, p := range paths {
if _, err := f.delete(ctx, p); err != nil {
return err
}
}
return nil
}