From 697dfcef5c60e785251a705d6a91c10b335714a6 Mon Sep 17 00:00:00 2001 From: Ben Myers Date: Sun, 28 Aug 2022 23:37:09 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=95=20Firestore=20Updating=20documenta?= =?UTF-8?q?tion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++ .../Services/Firestore/Updating.swift | 52 +++++++++---------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 77f38f3..e6494b6 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ EasyFirebase is a Swift wrapper for all things Firebase. Save hours from impleme - Document storage [→](https://github.com/Flowductive/easy-firebase#document-storage) - Document removal - Document retrieval [→](https://github.com/Flowductive/easy-firebase#document-retrieval) + - ✨ NEW: Document updating [→](https://github.com/Flowductive/easy-firebase#document-updating) - Update listeners [→](https://github.com/Flowductive/easy-firebase#update-listeners) - Built-in cacheing [→](https://github.com/Flowductive/easy-firebase#built-in-cacheing) - Easy linking [→](https://github.com/Flowductive/easy-firebase#easy-linking) @@ -132,6 +133,21 @@ EasyFirestore.Retrieval.get(id: myCarID, ofType: Car.self) { car in } ``` +### Document Updating + +Update fields remotely in Firestore without performing any reads. + +```swift +// Append element to array +EasyFirestore.Updating.append(\.ingredients, with: "Cheese", in: pizzaDocument) { error in ... } + +// Increment field +EasyFirestore.Updating.increment(\.pepperoniCount, by: 5, in: pizzaDocument) + +// Remove elements from array +EasyFirestore.Updating.remove(\.ingredients, taking: ["Garlic", "Anchovies", "Pineapple"], from: pizzaDocument) +``` + ### Update Listeners Grab documents and update the local instance when changed in Firestore: diff --git a/Sources/EasyFirebase/Services/Firestore/Updating.swift b/Sources/EasyFirebase/Services/Firestore/Updating.swift index 0f2bb06..bfabc05 100644 --- a/Sources/EasyFirebase/Services/Firestore/Updating.swift +++ b/Sources/EasyFirebase/Services/Firestore/Updating.swift @@ -28,7 +28,7 @@ extension EasyFirestore { - parameter document: The document with the updated field. - parameter completion: The completion handler. */ - public static func increment(_ path: KeyPath, by increase: Int, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: AdditiveArithmetic { + public static func increment(_ path: KeyPath, by increase: Int = 1, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: AdditiveArithmetic { let collectionName = String(describing: T.self) db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.increment(Int64(increase))], completion: completion) } @@ -83,30 +83,30 @@ extension EasyFirestore { 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(pair: (String, U), to path: KeyPath>, 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(pairs dict: [String: U], to path: KeyPath>, 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) - } +// /** +// 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(pair: (String, U), to path: KeyPath>, 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(pairs dict: [String: U], to path: KeyPath>, 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) +// } } }