You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

162 lines
3.5 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package internal
  15. // TODO(deklerk) can this file and directory be deleted, or is it being used for documentation somewhere?
  16. import (
  17. "context"
  18. "fmt"
  19. firestore "cloud.google.com/go/firestore"
  20. "google.golang.org/api/iterator"
  21. )
  22. // State represents a state.
  23. //[ structDef
  24. type State struct {
  25. Capital string `firestore:"capital"`
  26. Population float64 `firestore:"pop"` // in millions
  27. }
  28. //]
  29. func f1() {
  30. //[ NewClient
  31. ctx := context.Background()
  32. client, err := firestore.NewClient(ctx, "projectID")
  33. if err != nil {
  34. // TODO: Handle error.
  35. }
  36. //]
  37. //[ refs
  38. states := client.Collection("States")
  39. ny := states.Doc("NewYork")
  40. // Or, in a single call:
  41. ny = client.Doc("States/NewYork")
  42. //]
  43. //[ docref.Get
  44. docsnap, err := ny.Get(ctx)
  45. if err != nil {
  46. // TODO: Handle error.
  47. }
  48. dataMap := docsnap.Data()
  49. fmt.Println(dataMap)
  50. //]
  51. //[ DataTo
  52. var nyData State
  53. if err := docsnap.DataTo(&nyData); err != nil {
  54. // TODO: Handle error.
  55. }
  56. //]
  57. //[ GetAll
  58. docsnaps, err := client.GetAll(ctx, []*firestore.DocumentRef{
  59. states.Doc("Wisconsin"), states.Doc("Ohio"),
  60. })
  61. if err != nil {
  62. // TODO: Handle error.
  63. }
  64. for _, ds := range docsnaps {
  65. _ = ds // TODO: Use ds.
  66. }
  67. //[ docref.Create
  68. wr, err := ny.Create(ctx, State{
  69. Capital: "Albany",
  70. Population: 19.8,
  71. })
  72. if err != nil {
  73. // TODO: Handle error.
  74. }
  75. fmt.Println(wr)
  76. //]
  77. //[ docref.Set
  78. ca := states.Doc("California")
  79. _, err = ca.Set(ctx, State{
  80. Capital: "Sacramento",
  81. Population: 39.14,
  82. })
  83. //]
  84. //[ docref.Update
  85. _, err = ca.Update(ctx, []firestore.Update{{Path: "capital", Value: "Sacramento"}})
  86. //]
  87. //[ docref.Delete
  88. _, err = ny.Delete(ctx)
  89. //]
  90. //[ LUT-precond
  91. docsnap, err = ca.Get(ctx)
  92. if err != nil {
  93. // TODO: Handle error.
  94. }
  95. _, err = ca.Update(ctx,
  96. []firestore.Update{{Path: "capital", Value: "Sacramento"}},
  97. firestore.LastUpdateTime(docsnap.UpdateTime))
  98. //]
  99. //[ WriteBatch
  100. writeResults, err := client.Batch().
  101. Create(ny, State{Capital: "Albany"}).
  102. Update(ca, []firestore.Update{{Path: "capital", Value: "Sacramento"}}).
  103. Delete(client.Doc("States/WestDakota")).
  104. Commit(ctx)
  105. //]
  106. _ = writeResults
  107. //[ Query
  108. q := states.Where("pop", ">", 10).OrderBy("pop", firestore.Desc)
  109. //]
  110. //[ Documents
  111. iter := q.Documents(ctx)
  112. for {
  113. doc, err := iter.Next()
  114. if err == iterator.Done {
  115. break
  116. }
  117. if err != nil {
  118. // TODO: Handle error.
  119. }
  120. fmt.Println(doc.Data())
  121. }
  122. //]
  123. //[ CollQuery
  124. iter = client.Collection("States").Documents(ctx)
  125. //]
  126. }
  127. func txn() {
  128. var ctx context.Context
  129. var client *firestore.Client
  130. //[ Transaction
  131. ny := client.Doc("States/NewYork")
  132. err := client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
  133. doc, err := tx.Get(ny) // tx.Get, NOT ny.Get!
  134. if err != nil {
  135. return err
  136. }
  137. pop, err := doc.DataAt("pop")
  138. if err != nil {
  139. return err
  140. }
  141. return tx.Update(ny, []firestore.Update{{Path: "pop", Value: pop.(float64) + 0.2}})
  142. })
  143. if err != nil {
  144. // TODO: Handle error.
  145. }
  146. //]
  147. }