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

Commit

Permalink
📡 EasyFirestore.Updating struct
Browse files Browse the repository at this point in the history
  • Loading branch information
benlmyers committed Mar 11, 2022
1 parent a423c81 commit 881cdd2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ extension Document {
- parameter increment: The amount to increment by.
- parameter completion: The completion handler.
*/
public mutating func increment<T>(_ path: WritableKeyPath<Self, T?>, by increment: T, completion: @escaping (Error?) -> Void = { _ in }) where T: AdditiveArithmetic {
let val = self[keyPath: path]
guard var val = val else { return }
public mutating func increment<T>(_ path: WritableKeyPath<Self, T>, by increment: T, completion: @escaping (Error?) -> Void = { _ in }) where T: AdditiveArithmetic {
var val = self[keyPath: path]
if let intIncrement = increment as? Int {
EasyFirestore.Updating.increment(path, by: intIncrement, in: self, completion: completion)
val.add(increment)
Expand Down
8 changes: 4 additions & 4 deletions Sources/EasyFirebase/Services/Firestore/Retrieval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension EasyFirestore {
completion(cachedDocument)
return
} else {
get(id, collection: colName(of: T.self), type: type, completion: completion)
`get`(id, collection: colName(of: T.self), type: type, completion: completion)
}
}

Expand All @@ -49,7 +49,7 @@ extension EasyFirestore {
Singletons retrieved from Firestore are retrieved from the `Singleton` collection.
*/
public static func get<T>(singleton: SingletonName, ofType type: T.Type, completion: @escaping (T?) -> Void) where T: Singleton {
get(singleton, collection: "Singleton", type: type, completion: completion)
`get`(singleton, collection: "Singleton", type: type, completion: completion)
}

/**
Expand All @@ -68,7 +68,7 @@ extension EasyFirestore {
let chunks = ids.chunk(size: 10)
var results: [T] = []
for chunk in chunks {
get(chunk: chunk, ofType: type, useCache: useCache) { arr in
`get`(chunk: chunk, ofType: type, useCache: useCache) { arr in
results <= arr
onFetch(results)
}
Expand All @@ -90,7 +90,7 @@ extension EasyFirestore {
onFetch([])
return
}
get(ids: ids, ofType: U.self, onFetch: onFetch)
`get`(ids: ids, ofType: U.self, onFetch: onFetch)
}
}

Expand Down
23 changes: 22 additions & 1 deletion Sources/EasyFirebase/Services/Firestore/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension EasyFirestore {
/**
Sets the object in Firestore, then assigns it to a parent document's field list of `DocumentID`s.
- parameter document: The document to store in Fires
- parameter document: The document to store in Firestore.
- parameter child: The child document (only used to get an ID).
- parameter path: The path of the parent document's field containing the list of `DocumentID`s.
- parameter parent: The parent document containing the list of `DocumentID`s.
Expand All @@ -86,6 +86,27 @@ extension EasyFirestore {
}
}

/**
Sets a (default) document in Firestore only if the document does not exist.
This method is used to create new documents in Firestore without being destructive. For instance, if user objects have other document types unique to them, you may want to create these new documents under the condition that they don't already exist (to prevent user data loss).
- parameter document: The document to set in Firestore.
- parameter id: The ID of the document to check in Firestore. If set to `nil`, the ID of the document being set will be used to check.
*/
public static func setIfNone<T>(_ document: T, checking id: DocumentID? = nil, completion: @escaping (Error?) -> Void = { _ in }) where T: Document {
var checkID = document.id
if let id = id {
checkID = id
}
db.collection(String(describing: T.self)).document(checkID).getDocument { result, error in
guard error == nil else { return }
guard let result = result else { return }
guard !result.exists else { return }
`set`(document, completion: completion)
}
}

// MARK: - Private Static Methods

private static func set<T>(_ model: T, collection: CollectionName, id: String, completion: @escaping (Error?) -> Void) where T: Model {
Expand Down
2 changes: 1 addition & 1 deletion 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, 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

0 comments on commit 881cdd2

Please sign in to comment.