Skip to content

Commit

Permalink
馃摍 Dictionary updating methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benlmyers committed Aug 27, 2022
1 parent b0c490b commit b079b75
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/EasyFirebase/Services/Firestore/Updating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,31 @@ extension EasyFirestore {
let collectionName = String(describing: T.self)
db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.arrayRemove(items)], completion: completion)
}

/**
Adds a key-value pair to a dictionary in a field in Firestore.
- parameter pair: The pair to add to the dictionary value.
- parameter path: The path to the document's dictionary field to update.
- parameter document: The document to modify.
- parameter completion: The completion handler.
*/
public static func add<T, U>(pair: (String, U), to path: KeyPath<T, Dictionary<String, U>>, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: Codable {
let collectionName = String(describing: T.self)
db.collection(collectionName).document(document.id).setData([path.string: [pair.0: pair.1]], merge: true, completion: completion)
}

/**
Adds key-value pairs to a dictionary in a field in Firestore.
- parameter pairs: The pairs to add to the dictionary value.
- parameter path: The path to the document's dictionary field to update.
- parameter document: The document to modify.
- parameter completion: The completion handler.
*/
public static func add<T, U>(pairs dict: [String: U], to path: KeyPath<T, Dictionary<String, U>>, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: Codable {
let collectionName = String(describing: T.self)
db.collection(collectionName).document(document.id).setData(dict, merge: true, completion: completion)
}
}
}

0 comments on commit b079b75

Please sign in to comment.