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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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
//
// http://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.
package snippets
// [START authenticate_db_imports]
import (
"context"
"fmt"
"log"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/db"
"google.golang.org/api/option"
)
// [END authenticate_db_imports]
func authenticateWithAdminPrivileges() {
// [START authenticate_with_admin_privileges]
ctx := context.Background()
conf := &firebase.Config{
DatabaseURL: "https://databaseName.firebaseio.com",
}
// Fetch the service account key JSON file contents
opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
// Initialize the app with a service account, granting admin privileges
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
// As an admin, the app has access to read and write all data, regradless of Security Rules
ref := client.NewRef("restricted_access/secret_document")
var data map[string]interface{}
if err := ref.Get(ctx, &data); err != nil {
log.Fatalln("Error reading from database:", err)
}
fmt.Println(data)
// [END authenticate_with_admin_privileges]
}
func authenticateWithLimitedPrivileges() {
// [START authenticate_with_limited_privileges]
ctx := context.Background()
// Initialize the app with a custom auth variable, limiting the server's access
ao := map[string]interface{}{"uid": "my-service-worker"}
conf := &firebase.Config{
DatabaseURL: "https://databaseName.firebaseio.com",
AuthOverride: &ao,
}
// Fetch the service account key JSON file contents
opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
// The app only has access as defined in the Security Rules
ref := client.NewRef("/some_resource")
var data map[string]interface{}
if err := ref.Get(ctx, &data); err != nil {
log.Fatalln("Error reading from database:", err)
}
fmt.Println(data)
// [END authenticate_with_limited_privileges]
}
func authenticateWithGuestPrivileges() {
// [START authenticate_with_guest_privileges]
ctx := context.Background()
// Initialize the app with a nil auth variable, limiting the server's access
var nilMap map[string]interface{}
conf := &firebase.Config{
DatabaseURL: "https://databaseName.firebaseio.com",
AuthOverride: &nilMap,
}
// Fetch the service account key JSON file contents
opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
// The app only has access to public data as defined in the Security Rules
ref := client.NewRef("/some_resource")
var data map[string]interface{}
if err := ref.Get(ctx, &data); err != nil {
log.Fatalln("Error reading from database:", err)
}
fmt.Println(data)
// [END authenticate_with_guest_privileges]
}
func getReference(ctx context.Context, app *firebase.App) {
// [START get_reference]
// Create a database client from App.
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
// Get a database reference to our blog.
ref := client.NewRef("server/saving-data/fireblog")
// [END get_reference]
fmt.Println(ref.Path)
}
// [START user_type]
// User is a json-serializable type.
type User struct {
DateOfBirth string `json:"date_of_birth,omitempty"`
FullName string `json:"full_name,omitempty"`
Nickname string `json:"nickname,omitempty"`
}
// [END user_type]
func setValue(ctx context.Context, ref *db.Ref) {
// [START set_value]
usersRef := ref.Child("users")
err := usersRef.Set(ctx, map[string]*User{
"alanisawesome": {
DateOfBirth: "June 23, 1912",
FullName: "Alan Turing",
},
"gracehop": {
DateOfBirth: "December 9, 1906",
FullName: "Grace Hopper",
},
})
if err != nil {
log.Fatalln("Error setting value:", err)
}
// [END set_value]
}
func setChildValue(ctx context.Context, usersRef *db.Ref) {
// [START set_child_value]
if err := usersRef.Child("alanisawesome").Set(ctx, &User{
DateOfBirth: "June 23, 1912",
FullName: "Alan Turing",
}); err != nil {
log.Fatalln("Error setting value:", err)
}
if err := usersRef.Child("gracehop").Set(ctx, &User{
DateOfBirth: "December 9, 1906",
FullName: "Grace Hopper",
}); err != nil {
log.Fatalln("Error setting value:", err)
}
// [END set_child_value]
}
func updateChild(ctx context.Context, usersRef *db.Ref) {
// [START update_child]
hopperRef := usersRef.Child("gracehop")
if err := hopperRef.Update(ctx, map[string]interface{}{
"nickname": "Amazing Grace",
}); err != nil {
log.Fatalln("Error updating child:", err)
}
// [END update_child]
}
func updateChildren(ctx context.Context, usersRef *db.Ref) {
// [START update_children]
if err := usersRef.Update(ctx, map[string]interface{}{
"alanisawesome/nickname": "Alan The Machine",
"gracehop/nickname": "Amazing Grace",
}); err != nil {
log.Fatalln("Error updating children:", err)
}
// [END update_children]
}
func overwriteValue(ctx context.Context, usersRef *db.Ref) {
// [START overwrite_value]
if err := usersRef.Update(ctx, map[string]interface{}{
"alanisawesome": &User{Nickname: "Alan The Machine"},
"gracehop": &User{Nickname: "Amazing Grace"},
}); err != nil {
log.Fatalln("Error updating children:", err)
}
// [END overwrite_value]
}
// [START post_type]
// Post is a json-serializable type.
type Post struct {
Author string `json:"author,omitempty"`
Title string `json:"title,omitempty"`
}
// [END post_type]
func pushValue(ctx context.Context, ref *db.Ref) {
// [START push_value]
postsRef := ref.Child("posts")
newPostRef, err := postsRef.Push(ctx, nil)
if err != nil {
log.Fatalln("Error pushing child node:", err)
}
if err := newPostRef.Set(ctx, &Post{
Author: "gracehop",
Title: "Announcing COBOL, a New Programming Language",
}); err != nil {
log.Fatalln("Error setting value:", err)
}
// We can also chain the two calls together
if _, err := postsRef.Push(ctx, &Post{
Author: "alanisawesome",
Title: "The Turing Machine",
}); err != nil {
log.Fatalln("Error pushing child node:", err)
}
// [END push_value]
}
func pushAndSetValue(ctx context.Context, postsRef *db.Ref) {
// [START push_and_set_value]
if _, err := postsRef.Push(ctx, &Post{
Author: "gracehop",
Title: "Announcing COBOL, a New Programming Language",
}); err != nil {
log.Fatalln("Error pushing child node:", err)
}
// [END push_and_set_value]
}
func pushKey(ctx context.Context, postsRef *db.Ref) {
// [START push_key]
// Generate a reference to a new location and add some data using Push()
newPostRef, err := postsRef.Push(ctx, nil)
if err != nil {
log.Fatalln("Error pushing child node:", err)
}
// Get the unique key generated by Push()
postID := newPostRef.Key
// [END push_key]
fmt.Println(postID)
}
func transaction(ctx context.Context, client *db.Client) {
// [START transaction]
fn := func(t db.TransactionNode) (interface{}, error) {
var currentValue int
if err := t.Unmarshal(¤tValue); err != nil {
return nil, err
}
return currentValue + 1, nil
}
ref := client.NewRef("server/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes")
if err := ref.Transaction(ctx, fn); err != nil {
log.Fatalln("Transaction failed to commit:", err)
}
// [END transaction]
}
func readValue(ctx context.Context, app *firebase.App) {
// [START read_value]
// Create a database client from App.
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
// Get a database reference to our posts
ref := client.NewRef("server/saving-data/fireblog/posts")
// Read the data at the posts reference (this is a blocking operation)
var post Post
if err := ref.Get(ctx, &post); err != nil {
log.Fatalln("Error reading value:", err)
}
// [END read_value]
fmt.Println(ref.Path)
}
// [START dinosaur_type]
// Dinosaur is a json-serializable type.
type Dinosaur struct {
Height int `json:"height"`
Width int `json:"width"`
}
// [END dinosaur_type]
func orderByChild(ctx context.Context, client *db.Client) {
// [START order_by_child]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("height").GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
var d Dinosaur
if err := r.Unmarshal(&d); err != nil {
log.Fatalln("Error unmarshaling result:", err)
}
fmt.Printf("%s was %d meteres tall", r.Key(), d.Height)
}
// [END order_by_child]
}
func orderByNestedChild(ctx context.Context, client *db.Client) {
// [START order_by_nested_child]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("dimensions/height").GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
var d Dinosaur
if err := r.Unmarshal(&d); err != nil {
log.Fatalln("Error unmarshaling result:", err)
}
fmt.Printf("%s was %d meteres tall", r.Key(), d.Height)
}
// [END order_by_nested_child]
}
func orderByKey(ctx context.Context, client *db.Client) {
// [START order_by_key]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByKey().GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
snapshot := make([]Dinosaur, len(results))
for i, r := range results {
var d Dinosaur
if err := r.Unmarshal(&d); err != nil {
log.Fatalln("Error unmarshaling result:", err)
}
snapshot[i] = d
}
fmt.Println(snapshot)
// [END order_by_key]
}
func orderByValue(ctx context.Context, client *db.Client) {
// [START order_by_value]
ref := client.NewRef("scores")
results, err := ref.OrderByValue().GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
var score int
if err := r.Unmarshal(&score); err != nil {
log.Fatalln("Error unmarshaling result:", err)
}
fmt.Printf("The %s dinosaur's score is %d\n", r.Key(), score)
}
// [END order_by_value]
}
func limitToLast(ctx context.Context, client *db.Client) {
// [START limit_query_1]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("weight").LimitToLast(2).GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END limit_query_1]
}
func limitToFirst(ctx context.Context, client *db.Client) {
// [START limit_query_2]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("height").LimitToFirst(2).GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END limit_query_2]
}
func limitWithValueOrder(ctx context.Context, client *db.Client) {
// [START limit_query_3]
ref := client.NewRef("scores")
results, err := ref.OrderByValue().LimitToLast(3).GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
var score int
if err := r.Unmarshal(&score); err != nil {
log.Fatalln("Error unmarshaling result:", err)
}
fmt.Printf("The %s dinosaur's score is %d\n", r.Key(), score)
}
// [END limit_query_3]
}
func startAt(ctx context.Context, client *db.Client) {
// [START range_query_1]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("height").StartAt(3).GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END range_query_1]
}
func endAt(ctx context.Context, client *db.Client) {
// [START range_query_2]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByKey().EndAt("pterodactyl").GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END range_query_2]
}
func startAndEndAt(ctx context.Context, client *db.Client) {
// [START range_query_3]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByKey().StartAt("b").EndAt("b\uf8ff").GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END range_query_3]
}
func equalTo(ctx context.Context, client *db.Client) {
// [START range_query_4]
ref := client.NewRef("dinosaurs")
results, err := ref.OrderByChild("height").EqualTo(25).GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
for _, r := range results {
fmt.Println(r.Key())
}
// [END range_query_4]
}
func complexQuery(ctx context.Context, client *db.Client) {
// [START complex_query]
ref := client.NewRef("dinosaurs")
var favDinoHeight int
if err := ref.Child("stegosaurus").Child("height").Get(ctx, &favDinoHeight); err != nil {
log.Fatalln("Error querying database:", err)
}
query := ref.OrderByChild("height").EndAt(favDinoHeight).LimitToLast(2)
results, err := query.GetOrdered(ctx)
if err != nil {
log.Fatalln("Error querying database:", err)
}
if len(results) == 2 {
// Data is ordered by increasing height, so we want the first entry.
// Second entry is stegosarus.
fmt.Printf("The dinosaur just shorter than the stegosaurus is %s\n", results[0].Key())
} else {
fmt.Println("The stegosaurus is the shortest dino")
}
// [END complex_query]
}
|