-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
123 lines (107 loc) · 3.04 KB
/
firebase.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
package firebase
import (
"context"
"fmt"
"os"
"sync"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"rest.com.tw/tinymud/src/RestGo.MUD.Core/Config"
"rest.com.tw/tinymud/src/RestGo.Util/utility"
)
var (
client *firestore.Client
once sync.Once
)
var firebaseendpoint = ""
func IsRunningInTestMode() bool {
return firebaseendpoint != ""
}
// GetClient uses sync.Once to ensure that the firestore client is only initialized once
func GetClient() *firestore.Client {
once.Do(func() {
// Initialize the client
firebaseCredentialFile := Config.ServiceConfig.Firebase.CredentialFile
firebaseProjectID := Config.ServiceConfig.Firebase.ProjectID
ctx := context.Background()
optCredential := option.WithCredentialsFile(firebaseCredentialFile)
var err error
if firebaseendpoint != "" {
os.Setenv("FIRESTORE_EMULATOR_HOST", firebaseendpoint)
}
client, err = firestore.NewClient(ctx, firebaseProjectID, optCredential)
if err != nil {
panic(err)
}
})
return client
}
// UpdateOrCreate updates a document with the given id in the specified collection, or creates a new one if it doesn't exist
func UpdateOrCreate(collection string, id string, data interface{}, flag ...bool) (err error) {
c := GetClient()
m, err := utility.ConvertStructToMap(data)
if err != nil {
fmt.Println(err.Error())
} else {
if len(flag) > 0 && flag[0] {
_, err = c.Collection(collection).Doc(id).Set(context.Background(), m, firestore.MergeAll) //完整覆寫
} else {
_, err = c.Collection(collection).Doc(id).Set(context.Background(), m) //合併文件
}
}
return
}
// Delete deletes a document with the given id in the specified collection
func Delete(collection string, id string) error {
c := GetClient()
_, err := c.Collection(collection).Doc(id).Delete(context.Background())
return err
}
// GetAll returns all documents in the specified collection
func GetAll(collection string) ([]map[string]interface{}, error) {
c := GetClient()
iter := c.Collection(collection).Documents(context.Background())
var result []map[string]interface{}
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
result = append(result, doc.Data())
}
return result, nil
}
func GetByKeyValue(collection string, key string, value string) ([]map[string]interface{}, error) {
c := GetClient()
iter := c.Collection(collection).Where(key, "==", value).Documents(context.Background())
var result []map[string]interface{}
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
result = append(result, doc.Data())
}
return result, nil
}
func GetByID(collection string, ID string) (map[string]interface{}, error) {
c := GetClient()
doc, err := c.Collection(collection).Doc(ID).Get(context.Background())
if err != nil {
return nil, err
}
return doc.Data(), nil
}
func init() {
b, err := os.ReadFile("./../../FirebaseEmulator.setting")
if err == nil { //有找到設定檔
firebaseendpoint = string(b)
}
}