Skip to content

Commit 3a43fe7

Browse files
committed
[Feat] Implement isDocumentExists in FirestoreService
1 parent a8b0f8c commit 3a43fe7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/SHFirestoreService/FirestoreService.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,23 @@ extension FirestoreService: FirestoreTransactional {
388388
}.eraseToAnyPublisher()
389389
}
390390
}
391+
392+
// MARK: - FirestoreDocumentSupportable
393+
extension FirestoreService: FirestoreDocumentSupportable {
394+
/// Determine whether a document exists
395+
///
396+
/// Notes:
397+
/// 1. Do not need to specify FirestoreMethod's specific type when using this function.
398+
/// 2. Set up requestType of FirestoreAccessible in endpoint to access firestore's a specific document before call this function.
399+
public func isDocumentExists(endpoint: any FirestoreEndopintable) -> AnyPublisher<Bool, FirestoreServiceError> {
400+
guard let documentRef = endpoint.reference.asDocumentRef else {
401+
return Fail(error: FirestoreServiceError.documentNotFound).eraseToAnyPublisher()
402+
}
403+
return documentRef
404+
.getDocument()
405+
.convertFirestoreServiceError()
406+
.map { snapshot -> Bool in return snapshot.exists }.eraseToAnyPublisher()
407+
.eraseToAnyPublisher()
408+
}
409+
}
391410
#endif

0 commit comments

Comments
 (0)