Transaction+WriteEncodable.swift 4.2 KB

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