This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfirestoreCollection.go
65 lines (56 loc) · 1.7 KB
/
firestoreCollection.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
package gcp
import (
"context"
"cloud.google.com/go/firestore"
"github.com/puppetlabs/wash/plugin"
)
type firestoreCollection struct {
plugin.EntryBase
client *firestore.Client
path string
}
func newFirestoreCollection(client *firestore.Client, parent string, collRef *firestore.CollectionRef) *firestoreCollection {
return &firestoreCollection{
EntryBase: plugin.NewEntry(collRef.ID),
client: client,
path: firestorePath(parent, collRef.ID),
}
}
func (coll *firestoreCollection) List(ctx context.Context) ([]plugin.Entry, error) {
docs, err := coll.client.Collection(coll.path).Documents(ctx).GetAll()
if err != nil {
return nil, err
}
entries := make([]plugin.Entry, len(docs))
for ix, doc := range docs {
entries[ix] = newFirestoreDocument(coll.client, coll.path, doc)
}
return entries, nil
}
func (coll *firestoreCollection) Delete(ctx context.Context) (bool, error) {
// According to https://stackoverflow.com/a/47861164, deleting a collection
// means deleting its documents.
batch := coll.client.Batch()
docs, err := coll.client.Collection(coll.path).DocumentRefs(ctx).GetAll()
if err != nil {
return false, err
}
for _, doc := range docs {
batch.Delete(doc)
}
_, err = batch.Commit(ctx)
return true, err
}
func (coll *firestoreCollection) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(coll, "collection").
SetDescription(firestoreCollectionDescription)
}
func (coll *firestoreCollection) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{
(&firestoreDocument{}).Schema(),
}
}
const firestoreCollectionDescription = `
This is a Firestore collection. See the 'firestore' directory's docs for
more details on why we have this kind of entry.
`