WriteBatch+WriteEncodable.swift 4.2 KB

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