Skip to content

Commit

Permalink
Merge pull request #28 from abdolence/feature/fluent-transactions-sup…
Browse files Browse the repository at this point in the history
…port

Delete/Update Fluent API supports transactions now
  • Loading branch information
abdolence authored Oct 26, 2022
2 parents fa56c7c + 6215a2d commit befdbe9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
55 changes: 35 additions & 20 deletions examples/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,60 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
};

// Remove if it already exist
db.delete_by_id(TEST_COLLECTION_NAME, &my_struct.some_id)
db.fluent()
.delete()
.from(TEST_COLLECTION_NAME)
.document_id(&my_struct.some_id)
.execute()
.await?;

// Let's insert some data
db.create_obj(TEST_COLLECTION_NAME, &my_struct.some_id, &my_struct)
db.fluent()
.insert()
.into(TEST_COLLECTION_NAME)
.document_id(&my_struct.some_id)
.object(&my_struct)
.execute()
.await?;
}

println!("Transaction update/delete on collection");

let mut transaction = db.begin_transaction().await?;

transaction.update_object(
TEST_COLLECTION_NAME,
"test-0",
&MyTestStructure {
db.fluent()
.update()
.fields(paths!(MyTestStructure::{
some_string
}))
.in_col(TEST_COLLECTION_NAME)
.document_id("test-0")
.object(&MyTestStructure {
some_id: format!("test-0"),
some_string: "UpdatedTest".to_string(),
},
Some(paths!(MyTestStructure::{
some_string
})),
)?;
})
.add_to_transaction(&mut transaction)?;

transaction.delete_by_id(TEST_COLLECTION_NAME, "test-5")?;
db.fluent()
.delete()
.from(TEST_COLLECTION_NAME)
.document_id("test-5")
.add_to_transaction(&mut transaction)?;

transaction.commit().await?;

println!("Listing objects as a stream with updated test-0 and removed test-5");
// Query as a stream our data
let mut objs_stream: BoxStream<MyTestStructure> = db
.stream_list_obj(
FirestoreListDocParams::new(TEST_COLLECTION_NAME.into()).with_order_by(vec![
FirestoreQueryOrder::new(
path!(MyTestStructure::some_id),
FirestoreQueryDirection::Descending,
),
]),
)
.fluent()
.select()
.from(TEST_COLLECTION_NAME)
.order_by([(
path!(MyTestStructure::some_id),
FirestoreQueryDirection::Descending,
)])
.obj()
.stream_query()
.await?;

while let Some(object) = objs_stream.next().await {
Expand Down
18 changes: 17 additions & 1 deletion src/fluent_api/delete_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FirestoreDeleteSupport, FirestoreResult};
use crate::{FirestoreDeleteSupport, FirestoreResult, FirestoreTransaction};

#[derive(Clone, Debug)]
pub struct FirestoreDeleteInitialBuilder<'a, D>
Expand Down Expand Up @@ -127,4 +127,20 @@ where
.await
}
}

#[inline]
pub fn add_to_transaction<'t>(
self,
transaction: &'a mut FirestoreTransaction<'t>,
) -> FirestoreResult<&'a mut FirestoreTransaction<'t>> {
if let Some(parent) = self.parent {
transaction.delete_by_id_at(
parent.as_str(),
self.collection_id.as_str(),
self.document_id,
)
} else {
transaction.delete_by_id(self.collection_id.as_str(), self.document_id)
}
}
}
25 changes: 24 additions & 1 deletion src/fluent_api/update_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FirestoreResult, FirestoreUpdateSupport};
use crate::{FirestoreResult, FirestoreTransaction, FirestoreUpdateSupport};
use gcloud_sdk::google::firestore::v1::Document;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -303,4 +303,27 @@ where
.await
}
}

#[inline]
pub fn add_to_transaction<'t>(
self,
transaction: &'a mut FirestoreTransaction<'t>,
) -> FirestoreResult<&'a mut FirestoreTransaction<'t>> {
if let Some(parent) = self.parent {
transaction.update_object_at(
parent.as_str(),
self.collection_id.as_str(),
self.document_id,
self.object,
self.update_only_fields,
)
} else {
transaction.update_object(
self.collection_id.as_str(),
self.document_id,
self.object,
self.update_only_fields,
)
}
}
}

0 comments on commit befdbe9

Please sign in to comment.