ReferenceableObject.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2023 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseFirestore
  17. import CryptoKit
  18. import OSLog
  19. /// A protocol that denotes an object that can be "stored by reference" in Firestore.
  20. /// Structs that implement this and are contained in other Structs, are stored as a reference
  21. /// instead of stored inline if the FirestoreObjectReference propertyWrapper is used to annotate them.
  22. ///
  23. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  24. public protocol ReferenceableObject: Codable, Identifiable, Hashable, Equatable {
  25. // The Firestore collection where objects of this type are stored.
  26. // If no value is specified, it defaults to type name of the object
  27. static func parentCollection() -> String
  28. var id: String? { get set }
  29. var path: String? { get }
  30. static func objectPath(objectId: String) -> String
  31. }
  32. // default implementations
  33. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  34. public extension ReferenceableObject {
  35. static func parentCollection() -> String {
  36. let t = type(of: self)
  37. return String(describing: t)
  38. }
  39. func hash(into hasher: inout Hasher) {
  40. hasher.combine(path)
  41. }
  42. static func == (lhs: Self, rhs: Self) -> Bool {
  43. if let lpath = lhs.path,
  44. let rpath = rhs.path {
  45. return lpath == rpath
  46. } else {
  47. return false
  48. }
  49. }
  50. var path: String? {
  51. guard let id else {
  52. return nil
  53. }
  54. return Self.objectPath(objectId: id)
  55. }
  56. // Helper function that creates a path for the object
  57. static func objectPath(objectId: String) -> String {
  58. let collection = Self.parentCollection()
  59. if collection.hasSuffix("/") {
  60. return collection + objectId
  61. } else {
  62. return collection + "/" + objectId
  63. }
  64. }
  65. }
  66. // MARK: Contained Object Reference
  67. /// Struct used to store a reference to a ReferenceableObject. When a container struct is
  68. /// encoded, any FirestoreObjectReference property wrappers are encoded as ObjectReference objects and the referenced
  69. /// objects are stored (if needed) in their intended location instead of inline.
  70. ///
  71. /// For example:
  72. /// When storing UserProfile, which contains an Employer referenceable object, Employer object is stored
  73. /// in the Employer collection and a reference to Employer object is stored within the UserProfile encoded document.
  74. ///
  75. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  76. public struct ObjectReference<T: ReferenceableObject>: Codable {
  77. /// documentId of referenced object
  78. public var objectId: String
  79. /// collection where the referenced object is stored
  80. public var collection: String
  81. /// The referenced object. By default, it is not loaded. Apps can load this calling the loadReferencedObject() function.
  82. public var referencedObject: T?
  83. enum CodingKeys: String, CodingKey {
  84. case objectId
  85. case collection
  86. }
  87. /// Loads the referenced object from the db and assigns it to the referencedObject property
  88. public mutating func loadReferencedObject() async throws {
  89. let obj: T? = try await ReferenceableObjectManager.instance.getObject(objectId: objectId)
  90. referencedObject = obj
  91. }
  92. }
  93. // MARK: Logger
  94. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  95. extension FirestoreLogger {
  96. static var objectReference = FirestoreLogger(
  97. category: "objectReference"
  98. )
  99. }