From 5d4cbf5a1695641f4b70a848a5ef7548b932c24c Mon Sep 17 00:00:00 2001 From: Y N Date: Thu, 6 Apr 2023 10:13:28 +0900 Subject: [PATCH] Add new_with_client to WriteTx (#218) --- .../transact_write_with_http_client.rs | 43 +++++++++++++++++++ raiden/src/ops/transact_write.rs | 8 ++++ 2 files changed, 51 insertions(+) create mode 100644 raiden/examples/transact_write_with_http_client.rs diff --git a/raiden/examples/transact_write_with_http_client.rs b/raiden/examples/transact_write_with_http_client.rs new file mode 100644 index 00000000..d1947dca --- /dev/null +++ b/raiden/examples/transact_write_with_http_client.rs @@ -0,0 +1,43 @@ +use raiden::*; + +#[derive(Raiden)] +#[raiden(table_name = "user")] +pub struct User { + #[raiden(partition_key)] + pub id: String, + pub name: String, +} + +fn main() { + let rt = tokio::runtime::Runtime::new().unwrap(); + async fn example() { + let dispatcher = + raiden::request::HttpClient::new().expect("failed to create request dispatcher"); + let credentials_provider = raiden::credential::DefaultCredentialsProvider::new() + .expect("failed to create credentials provider"); + let core_client = raiden::Client::new_with(credentials_provider, dispatcher); + + let tx = ::raiden::WriteTx::new_with_client( + core_client, + Region::Custom { + endpoint: "http://localhost:8000".into(), + name: "ap-northeast-1".into(), + }, + ); + let cond = User::condition().attr_not_exists(User::id()); + let input = User::put_item_builder() + .id("testId".to_owned()) + .name("bokuweb".to_owned()) + .build(); + let input2 = User::put_item_builder() + .id("testId2".to_owned()) + .name("bokuweb".to_owned()) + .build(); + tx.put(User::put(input).condition(cond)) + .put(User::put(input2)) + .run() + .await + .unwrap(); + } + rt.block_on(example()); +} diff --git a/raiden/src/ops/transact_write.rs b/raiden/src/ops/transact_write.rs index d58bb1fe..03a5b889 100644 --- a/raiden/src/ops/transact_write.rs +++ b/raiden/src/ops/transact_write.rs @@ -14,6 +14,14 @@ impl WriteTx { retry_condition: crate::RetryCondition::new(), } } + pub fn new_with_client(client: crate::Client, region: crate::Region) -> Self { + let client = crate::DynamoDbClient::new_with_client(client, region); + Self { + items: vec![], + client, + retry_condition: crate::RetryCondition::new(), + } + } pub fn with_retries(mut self, s: Box) -> Self { self.retry_condition.strategy = s;