Open jsgalarraga opened 4 months ago
Any predictions for this merge? We are really looking forward to this feature
Hi @evandrobubiak, sorry for allowing this to stale, I must've missed the original PR email.
I'll review this now.
Also, would you mind writing a few tests? It doesn't need to be comprehensive, but at least covering the basics, essentially the samples you wrote for the documentation.
Oh, sure! Will also work on the tests
This PR introduces Firestore transactions to the package.
Why
Transactions are a powerful feature to have in the backend. This is an effort to have a more complete feature set for a dart backend.
It has been requested by users in #63 and #126.
Inspiration
The work here has been inspired by:
Transaction
class that exposes the methods that can be performed.Implementation notes
Transaction
class, does not extend the existingReference
even though it needs some of its methods because aTransaction
does not have a predefinedpath
. The_fullPath
and_encodeMap
methods have been adapted.mutations
in aTransaction
have to be exposed for theFirestoreGateway
to have access to them and run the transactioncommit
. To safely do this, it is kept as anUnmodifiableListView
to prevent the end user from modifying it.GrpcError
withStatusCode.aborted
is thrown. In this PR, the error handling is implemented to retry the transaction up to 5 times by default (as the other firestore SDKs do).document
attribute inFirestoreGateway
had the route to the/documents
path, which is needed for most common operations in the database. However, for the transactions, the raw database route is needed, therefore this has been updated to have bothdatabase
pointing to the root anddocumentDatabase
which adds the/documents
path.Examples
To ensure transactions are working and keeping the database consistent with all the write operations performed in parallel, the following test has been run: Increase a value multiple times both with and without transactions. As we can see, when running with transactions, the numbers are increased correctly 3 times, but the number is only increased by 1 when ran without transactions.
https://github.com/jsgalarraga/firedart/assets/52668514/559a8fa7-62ec-4a46-a41e-fbcb565392ce
With transactions
Without transactions