-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirestore.go
121 lines (88 loc) · 2.81 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
package firestore
import (
"context"
"log"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"github.com/fernandezvara/certsfor/db/store"
"github.com/fernandezvara/rest"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func init() {
store.Register("firestore", &Driver{})
}
// Driver initializes a Firestore and returns it
type Driver struct {
}
// Open creates the firestore struct, configures and returns it
func (f Driver) Open(ctx context.Context, connection string) (store.Store, error) {
var (
store Firestore
opt option.ClientOption
app *firebase.App
err error
)
opt = option.WithCredentialsFile(connection)
app, err = firebase.NewApp(ctx, &firebase.Config{}, opt)
if err != nil {
log.Fatalln(err)
}
store.client, err = app.Firestore(ctx)
if err != nil {
return nil, err
}
return store, err
}
// Firestore is the storage driver to manage data for the Firebase Firestore
//
// collection : CA Identifier that is used to split different datasets on the same storage
// id : ID of the item to retrieve
// value : pointer to fill/set with the information
type Firestore struct {
client *firestore.Client
}
// Get retrieves a value from the storage and unmarshals it to the required type
func (f Firestore) Get(ctx context.Context, collection, id string, value interface{}) (err error) {
var dsnap *firestore.DocumentSnapshot
dsnap, err = f.client.Collection(collection).Doc(id).Get(ctx)
if grpc.Code(err) == codes.NotFound {
return rest.ErrNotFound
}
if err != nil {
return
}
dsnap.DataTo(value)
return
}
// GetAll retrieves all the items for the required dataset
func (f Firestore) GetAll(ctx context.Context, collection string) (values []map[string]interface{}, err error) {
var docs []*firestore.DocumentSnapshot
docs, err = f.client.Collection(collection).Where("ca", "==", false).OrderBy("cn", firestore.Asc).Documents(ctx).GetAll()
if err != nil {
return
}
for _, doc := range docs {
values = append(values, doc.Data())
}
return
}
// Set inserts/updates a item in the dataset
func (f Firestore) Set(ctx context.Context, collection, id string, value interface{}) (err error) {
_, err = f.client.Collection(collection).Doc(id).Set(ctx, value)
return
}
// Delete removes the required ID on the dataset
func (f Firestore) Delete(ctx context.Context, collection, id string) (ok bool, err error) {
//var result *firestore.WriteResult
_, err = f.client.Collection(collection).Doc(id).Delete(ctx)
return
}
// Ping returns a non-nil error if the Store is not healthy or if the
// connection to the persistence is compromised.
func (f Firestore) Ping(ctx context.Context) error { return nil }
// Close releases the resources associated with the Store.
func (f Firestore) Close() error {
return f.client.Close()
}