-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.go
173 lines (135 loc) · 3.31 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
package aefire
type AEError string
func (e AEError) Error() string {
return string(e)
}
const (
DocumentNotFound = AEError("DocumentNotFound")
)
/*func NewBatch() *firestore.WriteBatch {
return FStore.Batch()
}
func Doc(path string) *firestore.DocumentRef {
return FStore.Doc(path)
}
func Snap(path string) *firestore.DocumentSnapshot {
snap, err := Doc(path).Get(context.Background())
if status.Code(err) == codes.NotFound {
return nil
} else {
return snap
}
}
func Col(path string) *firestore.CollectionRef {
return FStore.Collection(path)
}
func SnapExists(snap *firestore.DocumentSnapshot) bool {
return snap != nil && snap.Exists()
}
func SnapTo(snap *firestore.DocumentSnapshot, to interface{}) error {
if !SnapExists(snap) {
return DocumentNotFound
}
return snap.DataTo(to)
}
func PathTo(path string, to interface{}) error {
snap := Snap(path)
if !SnapExists(snap) {
return DocumentNotFound
}
return snap.DataTo(to)
}
func QueryStringField(q firestore.Query, field string) (fieldValues map[string]string) {
iter := q.Documents(context.Background())
for {
snap, err := iter.Next()
if err == iterator.Done {
break
}
value, err := snap.DataAt(field)
if str, ok := value.(string); ok {
fieldValues[snap.Ref.ID] = str
}
}
return
}
func BatchMerge(b *firestore.WriteBatch, doc *firestore.DocumentRef, pairs ...interface{}) *firestore.WriteBatch {
b.Set(doc, MapOf(pairs...), firestore.MergeAll)
return b
}
func BatchMergePath(b *firestore.WriteBatch, path string, pairs ...interface{}) *firestore.WriteBatch {
b.Set(Doc(path), MapOf(pairs...), firestore.MergeAll)
return b
}
func BatchMergeObj(b *firestore.WriteBatch, path string, o interface{}) *firestore.WriteBatch {
b.Set(Doc(path), ToMap(o), firestore.MergeAll)
return b
}
func DocMerge(doc *firestore.DocumentRef, pairs ...interface{}) error {
_, err := doc.Set(context.Background(), MapOf(pairs...), firestore.MergeAll)
return err
}
func PathMerge(path string, pairs ...interface{}) error {
return DocMerge(FStore.Doc(path), pairs...)
}
func DocMergeObj(doc *firestore.DocumentRef, o interface{}) error {
_, err := doc.Set(context.Background(), ToMap(o), firestore.MergeAll)
return err
}
func DeleteQueryResults(q firestore.Query, b *firestore.WriteBatch) error {
snaps, err := q.Documents(context.Background()).GetAll()
if err != nil {
return err
}
newBatch := false
if b == nil {
b = NewBatch()
newBatch = true
}
for _, snap := range snaps {
b.Delete(snap.Ref)
}
if !newBatch {
return nil
} else {
_, err = b.Commit(context.Background())
return err
}
}
func UpdateQueryResults(q firestore.Query, b *firestore.WriteBatch, updates ...interface{}) error {
snaps, err := q.Documents(context.Background()).GetAll()
if err != nil {
return err
}
newBatch := false
if b == nil {
b = NewBatch()
newBatch = true
}
for _, snap := range snaps {
BatchMerge(b, snap.Ref, updates...)
}
if !newBatch {
return nil
} else {
_, err = b.Commit(context.Background())
return err
}
}
func UpdatesOf(pairs ...interface{}) []firestore.Update {
if len(pairs)%2 != 0 {
panic("MapOf: key-value pair cannot be odd")
}
u := []firestore.Update{}
for i, kv := range pairs {
if i%2 == 1 {
k := pairs[i-1].(string)
u = append(u, firestore.Update{
Path: k,
Value: kv,
})
}
}
return u
}
*/