collref.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 firestore
  15. import (
  16. "context"
  17. "math/rand"
  18. "os"
  19. "sync"
  20. "time"
  21. )
  22. // A CollectionRef is a reference to Firestore collection.
  23. type CollectionRef struct {
  24. c *Client
  25. // The full resource path of the collection's parent. Typically Parent.Path,
  26. // or c.path if Parent is nil. May be different if this CollectionRef was
  27. // created from a stored reference to a different project/DB.
  28. //
  29. // For example, "projects/P/databases/D/documents/coll-1/doc-1".
  30. parentPath string
  31. // The shorter resource path of the collection. A collection "coll-2" in
  32. // document "doc-1" in collection "coll-1" would be: "coll-1/doc-1/coll-2".
  33. selfPath string
  34. // Parent is the document of which this collection is a part. It is
  35. // nil for top-level collections.
  36. Parent *DocumentRef
  37. // The full resource path of the collection: "projects/P/databases/D/documents..."
  38. Path string
  39. // ID is the collection identifier.
  40. ID string
  41. // Use the methods of Query on a CollectionRef to create and run queries.
  42. Query
  43. }
  44. func newTopLevelCollRef(c *Client, dbPath, id string) *CollectionRef {
  45. return &CollectionRef{
  46. c: c,
  47. ID: id,
  48. parentPath: dbPath,
  49. selfPath: id,
  50. Path: dbPath + "/documents/" + id,
  51. Query: Query{
  52. c: c,
  53. collectionID: id,
  54. path: dbPath + "/documents/" + id,
  55. parentPath: dbPath,
  56. },
  57. }
  58. }
  59. func newCollRefWithParent(c *Client, parent *DocumentRef, id string) *CollectionRef {
  60. selfPath := parent.shortPath + "/" + id
  61. return &CollectionRef{
  62. c: c,
  63. Parent: parent,
  64. ID: id,
  65. parentPath: parent.Path,
  66. selfPath: selfPath,
  67. Path: parent.Path + "/" + id,
  68. Query: Query{
  69. c: c,
  70. collectionID: id,
  71. path: parent.Path + "/" + id,
  72. parentPath: parent.Path,
  73. },
  74. }
  75. }
  76. // Doc returns a DocumentRef that refers to the document in the collection with the
  77. // given identifier.
  78. func (c *CollectionRef) Doc(id string) *DocumentRef {
  79. if c == nil {
  80. return nil
  81. }
  82. return newDocRef(c, id)
  83. }
  84. // NewDoc returns a DocumentRef with a uniquely generated ID.
  85. func (c *CollectionRef) NewDoc() *DocumentRef {
  86. return c.Doc(uniqueID())
  87. }
  88. // Add generates a DocumentRef with a unique ID. It then creates the document
  89. // with the given data, which can be a map[string]interface{}, a struct or a
  90. // pointer to a struct.
  91. //
  92. // Add returns an error in the unlikely event that a document with the same ID
  93. // already exists.
  94. func (c *CollectionRef) Add(ctx context.Context, data interface{}) (*DocumentRef, *WriteResult, error) {
  95. d := c.NewDoc()
  96. wr, err := d.Create(ctx, data)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. return d, wr, nil
  101. }
  102. // DocumentRefs returns references to all the documents in the collection, including
  103. // missing documents. A missing document is a document that does not exist but has
  104. // sub-documents.
  105. func (c *CollectionRef) DocumentRefs(ctx context.Context) *DocumentRefIterator {
  106. return newDocumentRefIterator(ctx, c, nil)
  107. }
  108. const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  109. var (
  110. rngMu sync.Mutex
  111. rng = rand.New(rand.NewSource(time.Now().UnixNano() ^ int64(os.Getpid())))
  112. )
  113. func uniqueID() string {
  114. var b [20]byte
  115. rngMu.Lock()
  116. for i := 0; i < len(b); i++ {
  117. b[i] = alphanum[rng.Intn(len(alphanum))]
  118. }
  119. rngMu.Unlock()
  120. return string(b[:])
  121. }