-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofirebase.go
202 lines (168 loc) · 4.42 KB
/
gofirebase.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
package gofirebase
import (
"cloud.google.com/go/firestore"
"cloud.google.com/go/storage"
"context"
"google.golang.org/api/iterator"
firebase "firebase.google.com/go/v4"
"fmt"
"github.com/mchirico/go-firebase/pkg/bucket"
"google.golang.org/api/option"
"log"
"sync"
)
// Firebase struct
type FB struct {
sync.Mutex
Credentials string
App *firebase.App
StorageBucket string
Bucket *bucket.Bucket
// Private
bucketHandle *storage.BucketHandle
err error
}
func (fb *FB) WriteMap(ctx context.Context, doc map[string]interface{}, collection string, Doc string) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
_, err = client.Collection(collection).Doc(Doc).Set(ctx, doc)
if err != nil {
log.Fatalf("Failed adding record: %v", err)
}
defer client.Close()
}
func (fb *FB) WriteMapCol2Doc2(ctx context.Context, doc map[string]interface{},
collection string, Doc string, collection2, Doc2 string) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
_, err = client.Collection(collection).Doc(Doc).Collection(collection2).Doc(Doc2).Set(ctx, doc)
if err != nil {
log.Fatalf("Failed adding record: %v", err)
}
defer client.Close()
}
func (fb *FB) ReadMap(ctx context.Context, path string, Doc string) (*firestore.DocumentSnapshot,
error) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
defer client.Close()
dsnap, err := client.Collection(path).Doc(Doc).Get(ctx)
if err != nil {
return dsnap, err
}
return dsnap, err
}
func (fb *FB) ReadCol(ctx context.Context, path string) ([]map[string]interface{}, error) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
if err != nil {
return []map[string]interface{}{}, err
}
defer client.Close()
iter := client.Collection(path).Documents(ctx)
resultFind := []map[string]interface{}{}
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return resultFind, err
}
m := map[string]interface{}{}
for k, v := range doc.Data() {
fmt.Printf("K: %v\n", k)
m[k] = v
}
resultFind = append(resultFind, m)
}
return resultFind, nil
}
func (fb *FB) ReadMapCol2Doc2(ctx context.Context, path, Doc, col2, Doc2 string) (*firestore.DocumentSnapshot,
error) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
defer client.Close()
dsnap, err := client.Collection(path).Doc(Doc).Collection(col2).Doc(Doc2).Get(ctx)
if err != nil {
return dsnap, err
}
return dsnap, err
}
func (fb *FB) Find(ctx context.Context, collection, path, op string, value interface{}) (map[string]interface{}, error) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
// You need to close
defer client.Close()
if err != nil {
return map[string]interface{}{}, err
}
// query := client.Collection(collection).Where("state", "==", "CA")
iter := client.Collection(collection).Where(path, op, value).Documents(ctx)
resultFind := map[string]interface{}{}
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return map[string]interface{}{}, err
}
for k, v := range doc.Data() {
resultFind[k] = v
}
}
return resultFind, nil
}
func (fb *FB) FindCol2Doc2(ctx context.Context, collection, Doc, col2, path, op string, value interface{}) (map[string]interface{}, error) {
fb.Lock()
defer fb.Unlock()
client, err := fb.App.Firestore(ctx)
// You need to close
defer client.Close()
if err != nil {
return map[string]interface{}{}, err
}
// query := client.Collection(collection).Where("state", "==", "CA")
iter := client.Collection(collection).Doc(Doc).Collection(col2).Where(path, op, value).Documents(ctx)
resultFind := map[string]interface{}{}
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return map[string]interface{}{}, err
}
fmt.Println(doc.Data())
for k, v := range doc.Data() {
resultFind[k] = v
}
}
return resultFind, nil
}
func (fb *FB) CreateApp(ctx context.Context) (*firebase.App, error) {
fb.Lock()
defer fb.Unlock()
opt := option.WithCredentialsFile(fb.Credentials)
storageClient, err := storage.NewClient(ctx, opt)
fb.Bucket = bucket.FBInitBucket(storageClient, fb.StorageBucket)
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
return nil, fmt.Errorf("error initializing app: %v", err)
}
fb.App = app
return app, nil
}