@@ -371,6 +371,44 @@ extension FirestoreService: FirestoreQueryable {
371371 }
372372 } . eraseToAnyPublisher ( )
373373 }
374+ }
375+
376+ // MARK: - FirestoreTransactional
377+ extension FirestoreService : FirestoreTransactional {
378+ /// Notes:
379+ /// 1. Executes a transaction and emits FiresotreServiceError if execution fails.
380+ public func performTransaction(
381+ _ updateBlock: @escaping ( Transaction ) throws -> Any ?
382+ ) -> AnyPublisher < Any ? , any Error > {
383+ return Firestore . firestore ( )
384+ . runTransaction ( updateBlock)
385+ . subscribeAndReceive ( on: backgroundQueue)
386+ . mapError { error in
387+ return FirestoreServiceError . failedTransaction ( error)
388+ } . eraseToAnyPublisher ( )
389+ }
390+ }
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+ /// 3. When a document path is provided from requestType, this function determines whether the document exists by querying the snapshot.
400+ public func isDocumentExists(
401+ endpoint: any FirestoreEndopintable
402+ ) -> AnyPublisher < Bool , FirestoreServiceError > {
403+ guard let documentRef = endpoint. reference. asDocumentRef else {
404+ return Fail ( error: FirestoreServiceError . documentNotFound) . eraseToAnyPublisher ( )
405+ }
406+ return documentRef
407+ . getDocument ( )
408+ . convertFirestoreServiceError ( )
409+ . map { snapshot -> Bool in return snapshot. exists } . eraseToAnyPublisher ( )
410+ . eraseToAnyPublisher ( )
411+ }
374412
375413 /// Checks if a document is duplicated or not from the endpoint requestType is a collectionRef using firestore's query.
376414 ///
@@ -400,7 +438,7 @@ extension FirestoreService: FirestoreQueryable {
400438 /// // TODO: - You can check whether the query has any matching documents in this downstream
401439 /// }
402440 /// ```
403- public func isFieldDuplicated (
441+ public func isDocumentExists (
404442 endpoint: any FirestoreEndopintable ,
405443 makeQuery: @escaping FirestoreQueryHandler
406444 ) -> AnyPublisher < Bool , FirestoreServiceError > {
@@ -428,39 +466,4 @@ extension FirestoreService: FirestoreQueryable {
428466 } . eraseToAnyPublisher ( )
429467 }
430468}
431-
432- // MARK: - FirestoreTransactional
433- extension FirestoreService : FirestoreTransactional {
434- /// Notes:
435- /// 1. Executes a transaction and emits FiresotreServiceError if execution fails.
436- public func performTransaction(
437- _ updateBlock: @escaping ( Transaction ) throws -> Any ?
438- ) -> AnyPublisher < Any ? , any Error > {
439- return Firestore . firestore ( )
440- . runTransaction ( updateBlock)
441- . subscribeAndReceive ( on: backgroundQueue)
442- . mapError { error in
443- return FirestoreServiceError . failedTransaction ( error)
444- } . eraseToAnyPublisher ( )
445- }
446- }
447-
448- // MARK: - FirestoreDocumentSupportable
449- extension FirestoreService : FirestoreDocumentSupportable {
450- /// Determine whether a document exists
451- ///
452- /// Notes:
453- /// 1. Do not need to specify FirestoreMethod's specific type when using this function.
454- /// 2. Set up requestType of FirestoreAccessible in endpoint to access firestore's a specific document before call this function.
455- public func isDocumentExists( endpoint: any FirestoreEndopintable ) -> AnyPublisher < Bool , FirestoreServiceError > {
456- guard let documentRef = endpoint. reference. asDocumentRef else {
457- return Fail ( error: FirestoreServiceError . documentNotFound) . eraseToAnyPublisher ( )
458- }
459- return documentRef
460- . getDocument ( )
461- . convertFirestoreServiceError ( )
462- . map { snapshot -> Bool in return snapshot. exists } . eraseToAnyPublisher ( )
463- . eraseToAnyPublisher ( )
464- }
465- }
466469#endif
0 commit comments