DynamoDB library for Rust.
You can see more examples here
raiden uses aws-sdk-dynamodb or rusoto_dynamodb as internal client.
use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
#[tokio::main]
async fn main() {
// Simply, specify the region.
let client = User::client(config::Region::from_static("us-east-1"));
// You can also specify aws-sdk-dynamodb client.
let client = {
let sdk_config = aws_config::defaults(aws_config::BehaviorVersion::latest())
.region(raiden::config::Region::from_static("us-east-1"))
.load()
.await;
let sdk_client = raiden::Client::new(&sdk_config);
User::client_with(sdk_client)
};
// Run operations...
}use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
#[tokio::main]
async fn main() {
// Simply, specify the region.
let client = User::client(Region::UsEast1);
// You can also specify rusoto_core client.
let client = User::client_with(Client::shared(), Region::UsEast1);
// Run operations...
}use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
#[tokio::main]
async fn main() {
let client = User::client(config::Region::from_static("us-east-1"))
.table_prefix("prefix-")
.table_suffix("-suffix");
// Print `prefix-user-suffix`
println!("{}", client.table_name());
}NOTE: Default retry strategy differs between aws-sdk and rusoto ( or rusoto_rustls )
aws-sdk... Not retry in raiden by default. Because you can configure retry strategy usingaws_config. Or you can configure your own strategy like next example.rusotoorrusoto_rustls... Enabled retrying in raiden by default. See detail here.
use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
// Force retry 3 times.
struct MyRetryStrategy;
impl RetryStrategy for MyRetryStrategy {
fn should_retry(&self, _error: &RaidenError) -> bool {
true
}
fn policy(&self) -> Policy {
Policy::Limit(3)
}
}
#[tokio::main]
async fn main() {
let client = User::client(config::Region::from_static("us-east-1"))
.with_retries(Box::new(MyRetryStrategy));
// Run operations...
}use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
#[tokio::main]
async fn main() {
let client = /* generate client */;
let _res = client.get("user_primary_key").run().await;
}use raiden::*;
#[derive(Raiden)]
#[raiden(table_name = "user")]
pub struct User {
#[raiden(partition_key)]
id: String,
name: String,
}
#[tokio::main]
async fn main() {
let client = /* generate client */;
let input = User::put_item_builder()
.id("foo".to_owned())
.name("bokuweb".to_owned())
.build();
let _res = client.put(&input).run().await;
}use raiden::*;
#[derive(Raiden, Debug, PartialEq)]
pub struct User {
#[raiden(partition_key)]
id: String,
#[raiden(sort_key)]
year: usize,
}
#[tokio::main]
async fn main() {
let client = /* generate client */;
let keys: Vec<(&str, usize)> = vec![("Alice", 1992), ("Bob", 1976), ("Charlie", 2002)];
let res = client.batch_get(keys).run().await;
}raiden supports making span for Tracing ( span name is dynamodb::action with table name and api name in field ).
To activate this feature, you need to specify tracing feature in your Cargo.toml. And your crate needs tracing .
# Example
[dependencies]
raiden = {
tag = "0.0.76",
git = "https://github.com/raiden-rs/raiden-dynamo.git",
features = [ "tracing"]
}
tracing = "0.1"- Rust (1.76.0+)
- Deno (1.13.2+)
- GNU Make
- Docker Engine
make test
NOTE: Don't recommend to use cargo test because our test suite doesn't support running tests in parallel. Use cargo test -- --test-threads=1 instead of it.
make dynamo
AWS_ACCESS_KEY_ID=dummy AWS_SECRET_ACCESS_KEY=dummy cargo run --example EXAMPLE_NAME
dynamodb-admin is useful to check data in DynamoDB Local.
npx dynamodb-admin
Then open http://localhost:8001 in browser.
- BatchGetItem
- BatchWriteItem
- DeleteItem
- GetItem
- PutItem
- Query
- Scan
- TransactGetItems
- TransactWriteItems
- UpdateItem
Here is a list of unsupported features/behaviors in the actual implementation. We have a plan to resolve these issues in a future release.
This project is available under the terms of either the Apache 2.0 license or the MIT license.
