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

Commit a423c81

Browse files
committed
🪜 Ability to increment field values in Firestore
1 parent a9f9835 commit a423c81

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Ben Myers on 3/10/22.
6+
//
7+
8+
import Foundation
9+
10+
internal extension AdditiveArithmetic {
11+
mutating func add(_ val: Self) {
12+
self += val
13+
}
14+
}

Sources/EasyFirebase/Services/Firestore/Protocols/Document.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ extension Document {
122122
EasyFirestore.Storage.set(value, to: path, in: self, completion: completion)
123123
}
124124

125+
/**
126+
Increments a specific field remotely in Firestore.
127+
128+
- parameter path: The path to the field to update.
129+
- parameter increment: The amount to increment by.
130+
- parameter completion: The completion handler.
131+
*/
132+
public mutating func increment<T>(_ path: WritableKeyPath<Self, T?>, by increment: T, completion: @escaping (Error?) -> Void = { _ in }) where T: AdditiveArithmetic {
133+
let val = self[keyPath: path]
134+
guard var val = val else { return }
135+
if let intIncrement = increment as? Int {
136+
EasyFirestore.Updating.increment(path, by: intIncrement, in: self, completion: completion)
137+
val.add(increment)
138+
self[keyPath: path] = val
139+
} else {
140+
fatalError("[EasyFirebase] You can't increment mismatching values! Check the types of values you are providing to increment.")
141+
}
142+
}
143+
125144
/**
126145
Gets the most up-to-date value from a specified path.
127146

@@ -177,3 +196,4 @@ extension Document {
177196
EasyFirestore.Linking.unassign(self, from: path, in: parent, completion: completion)
178197
}
179198
}
199+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Updating.swift
3+
//
4+
//
5+
// Created by Ben Myers on 3/10/22.
6+
//
7+
8+
import Foundation
9+
import Firebase
10+
import FirebaseFirestore
11+
import FirebaseFirestoreSwift
12+
13+
@available(iOS 13.0, *)
14+
extension EasyFirestore {
15+
16+
/**
17+
A service used for storing objects in Firestore.
18+
*/
19+
public struct Updating {
20+
21+
// MARK: - Public Static Methods
22+
23+
/**
24+
Increments a value for a particular field in Firestore.
25+
26+
- parameter path: The path to the document's field to update.
27+
- parameter increase: The amount to increase.
28+
- parameter document: The document with the updated field.
29+
- parameter completion: The completion handler.
30+
*/
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 {
32+
let collectionName = String(describing: T.self)
33+
db.collection(collectionName).document(document.id).updateData([path.string: FieldValue.increment(Int64(increase))], completion: completion)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)