-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main.go
151 lines (137 loc) · 4.87 KB
/
main.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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Command firestore_snippets contains runnable snippet code for Cloud Spanner.
package main
import (
"context"
"fmt"
"log"
"os"
"cloud.google.com/go/firestore"
)
// [START fs_class_definition]
// City represents a city.
type City struct {
Name string `firestore:"name,omitempty"`
State string `firestore:"state,omitempty"`
Country string `firestore:"country,omitempty"`
Capital bool `firestore:"capital,omitempty"`
Population int64 `firestore:"population,omitempty"`
Regions []string `firestore:"regions,omitempty"`
}
// [END fs_class_definition]
func main() {
ctx := context.Background()
projectID := os.Getenv("GCLOUD_PROJECT")
if projectID == "" {
log.Fatalf("Set Firebase project ID via GCLOUD_PROJECT env variable.")
}
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Cannot create client: %v", err)
}
defer client.Close()
if err := prepareQuery(ctx, client); err != nil {
log.Fatalf("Cannot prepare query docs: %v", err)
}
if err := addDocAsMap(ctx, client); err != nil {
log.Fatalf("Cannot add document as map: %v", err)
}
if err := addDocDataTypes(ctx, client); err != nil {
log.Fatalf("Cannot add document with more data types: %v", err)
}
if err := addDocAsEntity(ctx, client); err != nil {
log.Fatalf("Cannot add document as entity: %v", err)
}
if err := addDocWithID(ctx, client); err != nil {
log.Fatalf("Cannot add doc with id: %v", err)
}
if err := addDocWithoutID(ctx, client); err != nil {
log.Fatalf("Cannot add doc without id: %v", err)
}
if err := addDocAfterAutoGeneratedID(ctx, client); err != nil {
log.Fatalf("Cannot add document after generating ID: %v", err)
}
if err := updateDoc(ctx, client); err != nil {
log.Fatalf("Cannot update doc: %v", err)
}
if err := updateDocCreateIfMissing(ctx, client); err != nil {
log.Fatalf("Cannot update doc, creating if missing: %v", err)
}
if err := updateDocMultiple(ctx, client); err != nil {
log.Fatalf("Cannot update multiple docs: %v", err)
}
if err := updateDocNested(ctx, client); err != nil {
log.Fatalf("Cannot update nested doc: %v", err)
}
if err := deleteDoc(ctx, client); err != nil {
log.Fatalf("Cannot delete doc: %v", err)
}
if err := deleteField(ctx, client); err != nil {
log.Fatalf("Cannot delete document field: %v", err)
}
if err := runSimpleTransaction(ctx, client); err != nil {
log.Fatalf("Cannot run simple job in transaction: %v", err)
}
if err := infoTransaction(ctx, client); err != nil {
log.Fatalf("Cannot return info in transaction: %v", err)
}
if err := batchWrite(ctx, client); err != nil {
log.Fatalf("Cannot write in a batch: %v", err)
}
if err := prepareRetrieve(ctx, client); err != nil {
log.Fatalf("Cannot prepare for retrieve samples: %v", err)
}
if err := paginateCursor(ctx, client); err != nil {
log.Fatalf("Cannot paginate cursor: %v", err)
}
doc, err := docAsMap(ctx, client)
if err != nil {
log.Fatalf("Cannot get doc as map: %v", err)
}
fmt.Printf("Retrieved doc as map: %v\n", doc)
city, err := docAsEntity(ctx, client)
if err != nil {
log.Fatalf("Cannot get doc as entity: %v", err)
}
fmt.Printf("Retrieved doc as entity: %v\n", city)
if err := multipleDocs(ctx, client); err != nil {
log.Fatalf("Cannot retrieve capital cities: %v", err)
}
if err := allDocs(ctx, client); err != nil {
log.Fatalf("Cannot retrieve all docs: %v", err)
}
if err := getCollections(ctx, client); err != nil {
log.Fatalf("Cannot get subcollections for document: %v", err)
}
if err := createInQuery(ctx, client); err != nil {
log.Fatalf("Cannot get query results using in: %v", err)
}
if err := createInQueryWithArray(ctx, client); err != nil {
log.Fatalf("Cannot get query results using in with array: %v", err)
}
if err := createArrayContainsQuery(ctx, client); err != nil {
log.Fatalf("Cannot get query results using array-contains: %v", err)
}
if err := createArrayContainsAnyQuery(ctx, client); err != nil {
log.Fatalf("Cannot get query results using array-contains-any: %v", err)
}
if err := createStartAtDocSnapshotQuery(ctx, client); err != nil {
log.Fatalf("Cannot get query results using document snapshot: %v", err)
}
if err := deleteCollection(ctx, client, client.Collection("cities"), 2); err != nil {
log.Fatalf("Cannot delete collectionL %v", err)
}
}
// TODO(jbd): Add tests.