diff --git a/Sources/EasyFirebase/Services/Firestore/Protocols/Document.swift b/Sources/EasyFirebase/Services/Firestore/Protocols/Document.swift index be4989f..d3d3358 100644 --- a/Sources/EasyFirebase/Services/Firestore/Protocols/Document.swift +++ b/Sources/EasyFirebase/Services/Firestore/Protocols/Document.swift @@ -129,9 +129,8 @@ extension Document { - parameter increment: The amount to increment by. - parameter completion: The completion handler. */ - public mutating func increment(_ path: WritableKeyPath, 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(_ path: WritableKeyPath, 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) diff --git a/Sources/EasyFirebase/Services/Firestore/Retrieval.swift b/Sources/EasyFirebase/Services/Firestore/Retrieval.swift index b812425..61227ab 100644 --- a/Sources/EasyFirebase/Services/Firestore/Retrieval.swift +++ b/Sources/EasyFirebase/Services/Firestore/Retrieval.swift @@ -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) } } @@ -49,7 +49,7 @@ extension EasyFirestore { Singletons retrieved from Firestore are retrieved from the `Singleton` collection. */ public static func get(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) } /** @@ -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) } @@ -90,7 +90,7 @@ extension EasyFirestore { onFetch([]) return } - get(ids: ids, ofType: U.self, onFetch: onFetch) + `get`(ids: ids, ofType: U.self, onFetch: onFetch) } } diff --git a/Sources/EasyFirebase/Services/Firestore/Storage.swift b/Sources/EasyFirebase/Services/Firestore/Storage.swift index 3a509a7..055b395 100644 --- a/Sources/EasyFirebase/Services/Firestore/Storage.swift +++ b/Sources/EasyFirebase/Services/Firestore/Storage.swift @@ -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. @@ -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(_ 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(_ model: T, collection: CollectionName, id: String, completion: @escaping (Error?) -> Void) where T: Model { diff --git a/Sources/EasyFirebase/Services/Firestore/Updating.swift b/Sources/EasyFirebase/Services/Firestore/Updating.swift index bfd20d8..adce74f 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, 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) }