Skip to content

Commit

Permalink
📕 Firestore Updating documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
benlmyers committed Aug 29, 2022
1 parent 999f7a5 commit 697dfce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
52 changes: 26 additions & 26 deletions Sources/EasyFirebase/Services/Firestore/Updating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension EasyFirestore {
- parameter document: The document with the updated field.
- parameter completion: The completion handler.
*/
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 {
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 {
let collectionName = String(describing: T.self)
db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.increment(Int64(increase))], completion: completion)
}
Expand Down Expand Up @@ -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<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)
}
// /**
// 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 697dfce

Please sign in to comment.