Transaction+WriteEncodable.swift 4.1 KB

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