Transaction+WriteEncodable.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2019 Google
  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 Foundation
  17. #if SWIFT_PACKAGE
  18. @_exported import FirebaseFirestoreInternalWrapper
  19. #else
  20. @_exported import FirebaseFirestoreInternal
  21. #endif // SWIFT_PACKAGE
  22. public extension Transaction {
  23. /// Encodes an instance of `Encodable` and overwrites the encoded data
  24. /// to the document referred by `doc`. If no document exists,
  25. /// it is created. If a document already exists, it is overwritten.
  26. ///
  27. /// See `Firestore.Encoder` for more details about the encoding process.
  28. ///
  29. /// - Parameters:
  30. /// - value: a instance of `Encoded` to be encoded to a document.
  31. /// - encoder: The encoder instance to use to run the encoding.
  32. /// - doc: The document to create/overwrite the encoded data to.
  33. /// - Returns: This instance of `Transaction`. Used for chaining method calls.
  34. @discardableResult
  35. func setData<T: Encodable>(from value: T,
  36. forDocument doc: DocumentReference,
  37. encoder: Firestore.Encoder = Firestore
  38. .Encoder()) throws -> Transaction {
  39. let encoded = try encoder.encode(value)
  40. setData(encoded, forDocument: doc)
  41. return self
  42. }
  43. /// Encodes an instance of `Encodable` and overwrites the encoded data
  44. /// to the document referred by `doc`. If no document exists,
  45. /// it is created. If a document already exists, it is overwritten. If you pass
  46. /// merge:true, the provided `Encodable` will be merged into any existing document.
  47. ///
  48. /// See `Firestore.Encoder` for more details about the encoding process.
  49. ///
  50. /// - Parameters:
  51. /// - value: An instance of `Encodable` to be encoded to a document.
  52. /// - doc: The document to create/overwrite the encoded data to.
  53. /// - merge: Whether to merge the provided `Encodable` into any existing
  54. /// document.
  55. /// - encoder: The encoder instance to use to run the encoding.
  56. /// - Returns: This instance of `Transaction`. Used for chaining method calls.
  57. @discardableResult
  58. func setData<T: Encodable>(from value: T,
  59. forDocument doc: DocumentReference,
  60. merge: Bool,
  61. encoder: Firestore.Encoder = Firestore
  62. .Encoder()) throws -> Transaction {
  63. let encoded = try encoder.encode(value)
  64. setData(encoded, forDocument: doc, merge: merge)
  65. return self
  66. }
  67. /// Encodes an instance of `Encodable` and writes the encoded data to the document referred
  68. /// by `doc` by only replacing the fields specified under `mergeFields`.
  69. /// Any field that is not specified in mergeFields is ignored and remains untouched. If the
  70. /// document doesn’t yet exist, this method creates it and then sets the data.
  71. ///
  72. /// It is an error to include a field in `mergeFields` that does not have a corresponding
  73. /// field in the `Encodable`.
  74. ///
  75. /// See `Firestore.Encoder` for more details about the encoding process.
  76. ///
  77. /// - Parameters:
  78. /// - value: An instance of `Encodable` to be encoded to a document.
  79. /// - doc: The document to create/overwrite the encoded data to.
  80. /// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
  81. /// merge. Fields can contain dots to reference nested fields within the
  82. /// document.
  83. /// - encoder: The encoder instance to use to run the encoding.
  84. /// - Returns: This instance of `Transaction`. Used for chaining method calls.
  85. @discardableResult
  86. func setData<T: Encodable>(from value: T,
  87. forDocument doc: DocumentReference,
  88. mergeFields: [Any],
  89. encoder: Firestore.Encoder = Firestore
  90. .Encoder()) throws -> Transaction {
  91. let encoded = try encoder.encode(value)
  92. setData(encoded, forDocument: doc, mergeFields: mergeFields)
  93. return self
  94. }
  95. }