Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

146 linhas
4.2 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. // DO NOT EDIT doc.go. Modify internal/doc.template, then run make -C internal.
  15. /*
  16. Package firestore provides a client for reading and writing to a Cloud Firestore
  17. database.
  18. See https://cloud.google.com/firestore/docs for an introduction
  19. to Cloud Firestore and additional help on using the Firestore API.
  20. Note: you can't use both Cloud Firestore and Cloud Datastore in the same
  21. project.
  22. Creating a Client
  23. To start working with this package, create a client with a project ID:
  24. [NewClient]
  25. CollectionRefs and DocumentRefs
  26. In Firestore, documents are sets of key-value pairs, and collections are groups of
  27. documents. A Firestore database consists of a hierarchy of alternating collections
  28. and documents, referred to by slash-separated paths like
  29. "States/California/Cities/SanFrancisco".
  30. This client is built around references to collections and documents. CollectionRefs
  31. and DocumentRefs are lightweight values that refer to the corresponding database
  32. entities. Creating a ref does not involve any network traffic.
  33. [refs]
  34. Reading
  35. Use DocumentRef.Get to read a document. The result is a DocumentSnapshot.
  36. Call its Data method to obtain the entire document contents as a map.
  37. [docref.Get]
  38. You can also obtain a single field with DataAt, or extract the data into a struct
  39. with DataTo. With the type definition
  40. [structDef]
  41. we can extract the document's data into a value of type State:
  42. [DataTo]
  43. Note that this client supports struct tags beginning with "firestore:" that work like
  44. the tags of the encoding/json package, letting you rename fields, ignore them, or
  45. omit their values when empty.
  46. To retrieve multiple documents from their references in a single call, use
  47. Client.GetAll.
  48. [GetAll]
  49. Writing
  50. For writing individual documents, use the methods on DocumentReference.
  51. Create creates a new document.
  52. [docref.Create]
  53. The first return value is a WriteResult, which contains the time
  54. at which the document was updated.
  55. Create fails if the document exists. Another method, Set, either replaces an existing
  56. document or creates a new one.
  57. [docref.Set]
  58. To update some fields of an existing document, use Update. It takes a list of
  59. paths to update and their corresponding values.
  60. [docref.Update]
  61. Use DocumentRef.Delete to delete a document.
  62. [docref.Delete]
  63. Preconditions
  64. You can condition Deletes or Updates on when a document was last changed. Specify
  65. these preconditions as an option to a Delete or Update method. The check and the
  66. write happen atomically with a single RPC.
  67. [LUT-precond]
  68. Here we update a doc only if it hasn't changed since we read it.
  69. You could also do this with a transaction.
  70. To perform multiple writes at once, use a WriteBatch. Its methods chain
  71. for convenience.
  72. WriteBatch.Commit sends the collected writes to the server, where they happen
  73. atomically.
  74. [WriteBatch]
  75. Queries
  76. You can use SQL to select documents from a collection. Begin with the collection, and
  77. build up a query using Select, Where and other methods of Query.
  78. [Query]
  79. Call the Query's Documents method to get an iterator, and use it like
  80. the other Google Cloud Client iterators.
  81. [Documents]
  82. To get all the documents in a collection, you can use the collection itself
  83. as a query.
  84. [CollQuery]
  85. Transactions
  86. Use a transaction to execute reads and writes atomically. All reads must happen
  87. before any writes. Transaction creation, commit, rollback and retry are handled for
  88. you by the Client.RunTransaction method; just provide a function and use the
  89. read and write methods of the Transaction passed to it.
  90. [Transaction]
  91. Authentication
  92. See examples of authorization and authentication at
  93. https://godoc.org/cloud.google.com/go#pkg-examples.
  94. */
  95. package firestore