WriteBatch+WriteEncodable.swift 4.3 KB

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