forked from compnerd/swift-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement runTransaction and batch support (#28)
Adds: - `Transaction`, `TransactionOptions` and `WriteBatch` types - `VoidFuture` as workaround for `Future<void>` - `TransactionWeakReference` as a wrapper for `Transaction` since `Transaction` cannot be exposed directly to Swift given that it is a non-copyable type.
- Loading branch information
Showing
8 changed files
with
358 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
|
||
import CxxShim | ||
import Foundation | ||
|
||
public typealias Transaction = swift_firebase.swift_cxx_shims.firebase.firestore.TransactionWeakReference | ||
|
||
extension Transaction { | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference) -> Transaction { | ||
setData(data, forDocument: document, merge: false) | ||
} | ||
|
||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, merge: Bool) -> Transaction { | ||
assert(is_valid()) | ||
self.Set(document, FirestoreDataConverter.firestoreValue(document: data), merge ? .Merge() : .init()) | ||
return self | ||
} | ||
|
||
/* TODO: implement | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, mergeFields: [Any]) -> Transaction { | ||
} | ||
*/ | ||
|
||
public mutating func updateData(_ fields: [String : Any], forDocument document: DocumentReference) -> Transaction { | ||
assert(is_valid()) | ||
self.Update(document, FirestoreDataConverter.firestoreValue(document: fields)) | ||
return self | ||
} | ||
|
||
public mutating func deleteDocument(_ document: DocumentReference) -> Transaction { | ||
assert(is_valid()) | ||
Delete(document) | ||
return self | ||
} | ||
|
||
public mutating func getDocument(_ document: DocumentReference) throws -> DocumentSnapshot { | ||
assert(is_valid()) | ||
|
||
var error = firebase.firestore.kErrorNone | ||
var errorMessage = std.string() | ||
|
||
let snapshot = Get(document, &error, &errorMessage) | ||
|
||
if error != firebase.firestore.kErrorNone { | ||
throw NSError.firestore(error, errorMessage: String(errorMessage)) | ||
} | ||
|
||
return snapshot | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
|
||
public typealias TransactionOptions = firebase.firestore.TransactionOptions | ||
|
||
extension TransactionOptions { | ||
public var maxAttempts: Int { | ||
get { | ||
Int(max_attempts()) | ||
} | ||
set { | ||
set_max_attempts(Int32(newValue)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
@_spi(FirebaseInternal) | ||
import FirebaseCore | ||
|
||
import CxxShim | ||
import Foundation | ||
|
||
public typealias WriteBatch = firebase.firestore.WriteBatch | ||
|
||
extension WriteBatch { | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference) -> WriteBatch { | ||
setData(data, forDocument: document, merge: false) | ||
} | ||
|
||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, merge: Bool) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_set( | ||
self, document, FirestoreDataConverter.firestoreValue(document: data), merge ? .Merge() : .init() | ||
) | ||
return self | ||
} | ||
|
||
/* TODO: implement | ||
public mutating func setData(_ data: [String : Any], forDocument document: DocumentReference, mergeFields: [Any]) -> WriteBatch { | ||
} | ||
*/ | ||
|
||
public mutating func updateData(_ fields: [String : Any], forDocument document: DocumentReference) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_update( | ||
self, document, FirestoreDataConverter.firestoreValue(document: fields) | ||
) | ||
return self | ||
} | ||
|
||
public mutating func deleteDocument(_ document: DocumentReference) -> WriteBatch { | ||
_ = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_delete( | ||
self, document | ||
) | ||
return self | ||
} | ||
|
||
public mutating func commit(completion: @escaping ((Error?) -> Void) = { _ in }) { | ||
let future = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_commit(self) | ||
future.setCompletion({ | ||
let (_, error) = future.resultAndError | ||
DispatchQueue.main.async { | ||
completion(error) | ||
} | ||
}) | ||
} | ||
|
||
public mutating func commit() async throws { | ||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in | ||
let future = swift_firebase.swift_cxx_shims.firebase.firestore.write_batch_commit(self) | ||
future.setCompletion({ | ||
let (_, error) = future.resultAndError | ||
if let error { | ||
continuation.resume(throwing: error) | ||
} else { | ||
continuation.resume() | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.