Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit 697dfce

Browse files
committed
📕 Firestore Updating documentation
1 parent 999f7a5 commit 697dfce

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ EasyFirebase is a Swift wrapper for all things Firebase. Save hours from impleme
3333
- Document storage [](https://github.com/Flowductive/easy-firebase#document-storage)
3434
- Document removal
3535
- Document retrieval [](https://github.com/Flowductive/easy-firebase#document-retrieval)
36+
- ✨ NEW: Document updating [](https://github.com/Flowductive/easy-firebase#document-updating)
3637
- Update listeners [](https://github.com/Flowductive/easy-firebase#update-listeners)
3738
- Built-in cacheing [](https://github.com/Flowductive/easy-firebase#built-in-cacheing)
3839
- Easy linking [](https://github.com/Flowductive/easy-firebase#easy-linking)
@@ -132,6 +133,21 @@ EasyFirestore.Retrieval.get(id: myCarID, ofType: Car.self) { car in
132133
}
133134
```
134135

136+
### Document Updating
137+
138+
Update fields remotely in Firestore without performing any reads.
139+
140+
```swift
141+
// Append element to array
142+
EasyFirestore.Updating.append(\.ingredients, with: "Cheese", in: pizzaDocument) { error in ... }
143+
144+
// Increment field
145+
EasyFirestore.Updating.increment(\.pepperoniCount, by: 5, in: pizzaDocument)
146+
147+
// Remove elements from array
148+
EasyFirestore.Updating.remove(\.ingredients, taking: ["Garlic", "Anchovies", "Pineapple"], from: pizzaDocument)
149+
```
150+
135151
### Update Listeners
136152

137153
Grab documents and update the local instance when changed in Firestore:

Sources/EasyFirebase/Services/Firestore/Updating.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension EasyFirestore {
2828
- parameter document: The document with the updated field.
2929
- parameter completion: The completion handler.
3030
*/
31-
public static func increment<T, U>(_ path: KeyPath<T, U>, by increase: Int, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: AdditiveArithmetic {
31+
public static func increment<T, U>(_ path: KeyPath<T, U>, by increase: Int = 1, in document: T, completion: @escaping (Error?) -> Void = { _ in }) where T: Document, U: AdditiveArithmetic {
3232
let collectionName = String(describing: T.self)
3333
db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.increment(Int64(increase))], completion: completion)
3434
}
@@ -83,30 +83,30 @@ extension EasyFirestore {
8383
db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.arrayRemove(items)], completion: completion)
8484
}
8585

86-
/**
87-
Adds a key-value pair to a dictionary in a field in Firestore.
88-
89-
- parameter pair: The pair to add to the dictionary value.
90-
- parameter path: The path to the document's dictionary field to update.
91-
- parameter document: The document to modify.
92-
- parameter completion: The completion handler.
93-
*/
94-
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 {
95-
let collectionName = String(describing: T.self)
96-
db.collection(collectionName).document(document.id).setData([path.string: [pair.0: pair.1]], merge: true, completion: completion)
97-
}
98-
99-
/**
100-
Adds key-value pairs to a dictionary in a field in Firestore.
101-
102-
- parameter pairs: The pairs to add to the dictionary value.
103-
- parameter path: The path to the document's dictionary field to update.
104-
- parameter document: The document to modify.
105-
- parameter completion: The completion handler.
106-
*/
107-
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 {
108-
let collectionName = String(describing: T.self)
109-
db.collection(collectionName).document(document.id).setData(dict, merge: true, completion: completion)
110-
}
86+
// /**
87+
// Adds a key-value pair to a dictionary in a field in Firestore.
88+
//
89+
// - parameter pair: The pair to add to the dictionary value.
90+
// - parameter path: The path to the document's dictionary field to update.
91+
// - parameter document: The document to modify.
92+
// - parameter completion: The completion handler.
93+
// */
94+
// 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 {
95+
// let collectionName = String(describing: T.self)
96+
// db.collection(collectionName).document(document.id).setData([path.string: [pair.0: pair.1]], merge: true, completion: completion)
97+
// }
98+
//
99+
// /**
100+
// Adds key-value pairs to a dictionary in a field in Firestore.
101+
//
102+
// - parameter pairs: The pairs to add to the dictionary value.
103+
// - parameter path: The path to the document's dictionary field to update.
104+
// - parameter document: The document to modify.
105+
// - parameter completion: The completion handler.
106+
// */
107+
// 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 {
108+
// let collectionName = String(describing: T.self)
109+
// db.collection(collectionName).document(document.id).setData(dict, merge: true, completion: completion)
110+
// }
111111
}
112112
}

0 commit comments

Comments
 (0)