Crate serde_dynamo
source ·Expand description
DynamoDB is an AWS database that stores key/value and document data.
serde_dynamo provides a way to serialize and deserialize between data stored in these items and strongly-typed Rust data structures.
§The full power of serde
serde_dynamo supports the full power of serde.
Most uses of DynamoDB will involve simple structs mapping keys to values in type-safe ways.
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
pub struct UserId(String);
#[derive(Serialize, Deserialize)]
pub struct User {
id: UserId,
name: String,
age: u8,
#[serde(with = "serde_bytes")]
public_key: Vec<u8>
}
More advanced usage – including flattening, adjacently tagged enums, and untagged enums – is fully supported.
#[derive(Serialize, Deserialize)]
struct Message {
id: String,
#[serde(flatten)]
message_type: MessageType,
sent: DateTime<Utc>,
}
/// What type of message this is.
///
/// By the power of Rust enums and serde serializating, we can guarantee that we _either_ have
/// an email with all of its required fields, _or_ an SMS with all of its required fields.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "message_type", content = "message_payload")]
enum MessageType {
Email(Email),
Sms(Sms),
}
#[derive(Serialize, Deserialize)]
struct Email {
email: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
subject: String,
body: String,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
struct Sms {
phone_number: String,
body: String,
}
let input = r#"
{
"id": "HWCqBFBG2Gl4",
"message_type": "sms",
"message_payload": {
"phone_number": "5551234567",
"body": "Good morning!"
},
"sent": "1985-04-21T11:12:13Z"
}
"#;
let message: Message = serde_json::from_str(input)?;
let item = to_item(message)?;
§aws-sdk support
serde_dynamo works well with aws-sdk-dynamodb.
Add the following to your dependencies.
[dependencies]
serde_dynamo = { version = "4", features = ["aws-sdk-dynamodb+1"] }
See aws_sdk_dynamodb_1
for examples and more information. See
aws_sdk_dynamodbstreams_1
for DynamoDb streams support.
§aws_lambda_events support
aws_lambda_events, starting with version 0.8, uses serde_dynamo directly, so no feature flags need to be enabled.
[dependencies]
serde_dynamo = { version = "4" }
§rusoto support
serde_dynamo works well with rusoto_dynamodb.
Add the following to your dependencies.
[dependencies]
serde_dynamo = { version = "4", features = ["rusoto_dynamodb+0.48"] }
See rusoto_dynamodb_0_48
for examples and more information.
§JSON
DynamoDB’s items share strong similarities with JSON, and it is very common to store JSON data in DynamoDB either directly or as a subfield.
To support this, serde_dynamo supports serializing JSON just like any other Rust data structure.
#[derive(Serialize, Deserialize)]
struct IncludesJson {
id: String,
data: serde_json::Value,
}
In addition, serde_dynamo also maps strongly-typed data structures nearly identically as serde_json. This means that, in almost all cases, serializing to JSON first and then to an DynamoDb item will result in the exact same representation as serializing directly to a DynamoDb item. (The caveat here is for byte data, which loses fidelity because JSON doesn’t support byte data natively, but DynamoDB does.)
let user = User {
name: "Arthur Dent".to_string(),
age: 42,
};
// Serialize directly from the data structure to an item
let direct_item = to_item(user.clone())?;
// Serialize indirectly through JSON
let json = serde_json::to_value(user.clone())?;
let indirect_item = to_item(json)?;
// The result should be the same!
assert_eq!(direct_item, indirect_item);
§Features
serde_dynamo is a stable library ready to use in production. Because of that, it’s major version is above 1.0.
This creates problems when supporting dynamodb libraries that have version numbers less than 1.0.
To avoid doing a major version bump for every release of aws-sdk-dynamodb
and
aws_lambda_events
, serde_dynamo uses features to opt into the correct version of the
dynamodb library.
See the modules section for all possible features. Feature names are largely
guessable: the library name, a plus, and the library version (with underscores instead of dots,
because crates.io doesn’t support feature names with dots). For example, support for
aws-sdk-dynamodb
version 0.13
is enabled with the feature aws-sdk-dynamodb+0_13
.
§Converting to and from DynamoDB JSON
In most cases, libraries already exist to handle the raw DynamoDB JSON and convert it into an item. For example, aws-sdk-dynamodb deals with the raw JSON if you’re making API calls, and aws_lambda_events deals with the raw JSON if you’re writing lambdas that react on DynamoDB change streams.
However, in very rare cases, you may need to convert the DynamoDB JSON yourself. In those cases, both Item and AttributeValue implement serde::Serialize and serde::Deserialize.
let input = r#"{
"Id":{
"N":"103"
},
"Title":{
"S":"Book 103 Title"
},
"Authors":{
"SS":[
"Author1",
"Author2"
]
},
"InPublication":{
"BOOL":false
}
}"#;
let item: Item = serde_json::from_str(input)
.expect("expected to deserialize DynamoDB JSON format");
assert_eq!(
item.get("Id").unwrap(),
&AttributeValue::N(String::from("103")),
);
Modules§
- aws_lambda_events_0_6
aws_lambda_events+0_6
Support for aws_lambda_events version 0.6 - aws_lambda_events_0_7
aws_lambda_events+0_7
Support for aws_lambda_events version 0.7 - aws_sdk_dynamodb_0_7
aws-sdk-dynamodb+0_7
Support for aws-sdk-dynamodb version 0.7 - aws_sdk_dynamodb_0_8
aws-sdk-dynamodb+0_8
Support for aws-sdk-dynamodb version 0.8 - aws_sdk_dynamodb_0_9
aws-sdk-dynamodb+0_9
Support for aws-sdk-dynamodb version 0.9 - aws_sdk_dynamodb_0_10
aws-sdk-dynamodb+0_10
Support for aws-sdk-dynamodb version 0.10 - aws_sdk_dynamodb_0_11
aws-sdk-dynamodb+0_11
Support for aws-sdk-dynamodb version 0.11 - aws_sdk_dynamodb_0_12
aws-sdk-dynamodb+0_12
Support for aws-sdk-dynamodb version 0.12 - aws_sdk_dynamodb_0_13
aws-sdk-dynamodb+0_13
Support for aws-sdk-dynamodb version 0.13 - aws_sdk_dynamodb_0_14
aws-sdk-dynamodb+0_14
Support for aws-sdk-dynamodb version 0.14 - aws_sdk_dynamodb_0_15
aws-sdk-dynamodb+0_15
Support for aws-sdk-dynamodb version 0.15 - aws_sdk_dynamodb_0_16
aws-sdk-dynamodb+0_16
Support for aws-sdk-dynamodb version 0.16 - aws_sdk_dynamodb_0_17
aws-sdk-dynamodb+0_17
Support for aws-sdk-dynamodb version 0.17 - aws_sdk_dynamodb_0_18
aws-sdk-dynamodb+0_18
Support for aws-sdk-dynamodb version 0.18 - aws_sdk_dynamodb_0_19
aws-sdk-dynamodb+0_19
Support for aws-sdk-dynamodb version 0.19 - aws_sdk_dynamodb_0_21
aws-sdk-dynamodb+0_21
Support for aws-sdk-dynamodb version 0.21 - aws_sdk_dynamodb_0_22
aws-sdk-dynamodb+0_22
Support for aws-sdk-dynamodb version 0.22 - aws_sdk_dynamodb_0_23
aws-sdk-dynamodb+0_23
Support for aws-sdk-dynamodb version 0.23 - aws_sdk_dynamodb_0_24
aws-sdk-dynamodb+0_24
Support for aws-sdk-dynamodb version 0.24 - aws_sdk_dynamodb_0_25
aws-sdk-dynamodb+0_25
Support for aws-sdk-dynamodb version 0.25 - aws_sdk_dynamodb_0_26
aws-sdk-dynamodb+0_26
Support for aws-sdk-dynamodb version 0.26 - aws_sdk_dynamodb_0_27
aws-sdk-dynamodb+0_27
Support for aws-sdk-dynamodb version 0.27 - aws_sdk_dynamodb_0_28
aws-sdk-dynamodb+0_28
Support for aws-sdk-dynamodb version 0.28 - aws_sdk_dynamodb_0_29
aws-sdk-dynamodb+0_29
Support for aws-sdk-dynamodb version 0.29 - aws_sdk_dynamodb_0_30
aws-sdk-dynamodb+0_30
Support for aws-sdk-dynamodb version 0.30 - aws_sdk_dynamodb_0_31
aws-sdk-dynamodb+0_31
Support for aws-sdk-dynamodb version 0.31 - aws_sdk_dynamodb_0_32
aws-sdk-dynamodb+0_32
Support for aws-sdk-dynamodb version 0.32 - aws_sdk_dynamodb_0_33
aws-sdk-dynamodb+0_33
Support for aws-sdk-dynamodb version 0.33 - aws_sdk_dynamodb_0_34
aws-sdk-dynamodb+0_34
Support for aws-sdk-dynamodb version 0.34 - aws_sdk_dynamodb_0_35
aws-sdk-dynamodb+0_35
Support for aws-sdk-dynamodb version 0.35 - aws_sdk_dynamodb_0_36
aws-sdk-dynamodb+0_36
Support for aws-sdk-dynamodb version 0.36 - aws_sdk_dynamodb_0_37
aws-sdk-dynamodb+0_37
Support for aws-sdk-dynamodb version 0.37 - aws_sdk_dynamodb_0_38
aws-sdk-dynamodb+0_38
Support for aws-sdk-dynamodb version 0.38 - aws_sdk_dynamodb_0_39
aws-sdk-dynamodb+0_39
Support for aws-sdk-dynamodb version 0.39 - aws_sdk_dynamodb_1
aws-sdk-dynamodb+1
Support for aws-sdk-dynamodb version 1 - aws_sdk_dynamodbstreams_0_8
aws-sdk-dynamodbstreams+0_8
Support for aws-sdk-dynamodbstreams version 0.8 - aws_sdk_dynamodbstreams_0_9
aws-sdk-dynamodbstreams+0_9
Support for aws-sdk-dynamodbstreams version 0.9 - aws_sdk_dynamodbstreams_0_10
aws-sdk-dynamodbstreams+0_10
Support for aws-sdk-dynamodbstreams version 0.10 - aws_sdk_dynamodbstreams_0_11
aws-sdk-dynamodbstreams+0_11
Support for aws-sdk-dynamodbstreams version 0.11 - aws_sdk_dynamodbstreams_0_12
aws-sdk-dynamodbstreams+0_12
Support for aws-sdk-dynamodbstreams version 0.12 - aws_sdk_dynamodbstreams_0_13
aws-sdk-dynamodbstreams+0_13
Support for aws-sdk-dynamodbstreams version 0.13 - aws_sdk_dynamodbstreams_0_14
aws-sdk-dynamodbstreams+0_14
Support for aws-sdk-dynamodbstreams version 0.14 - aws_sdk_dynamodbstreams_0_15
aws-sdk-dynamodbstreams+0_15
Support for aws-sdk-dynamodbstreams version 0.15 - aws_sdk_dynamodbstreams_0_16
aws-sdk-dynamodbstreams+0_16
Support for aws-sdk-dynamodbstreams version 0.16 - aws_sdk_dynamodbstreams_0_17
aws-sdk-dynamodbstreams+0_17
Support for aws-sdk-dynamodbstreams version 0.17 - aws_sdk_dynamodbstreams_0_18
aws-sdk-dynamodbstreams+0_18
Support for aws-sdk-dynamodbstreams version 0.18 - aws_sdk_dynamodbstreams_0_19
aws-sdk-dynamodbstreams+0_19
Support for aws-sdk-dynamodbstreams version 0.19 - aws_sdk_dynamodbstreams_0_21
aws-sdk-dynamodbstreams+0_21
Support for aws-sdk-dynamodbstreams version 0.21 - aws_sdk_dynamodbstreams_0_22
aws-sdk-dynamodbstreams+0_22
Support for aws-sdk-dynamodbstreams version 0.22 - aws_sdk_dynamodbstreams_0_23
aws-sdk-dynamodbstreams+0_23
Support for aws-sdk-dynamodbstreams version 0.23 - aws_sdk_dynamodbstreams_0_24
aws-sdk-dynamodbstreams+0_24
Support for aws-sdk-dynamodbstreams version 0.24 - aws_sdk_dynamodbstreams_0_25
aws-sdk-dynamodbstreams+0_25
Support for aws-sdk-dynamodbstreams version 0.25 - aws_sdk_dynamodbstreams_0_26
aws-sdk-dynamodbstreams+0_26
Support for aws-sdk-dynamodbstreams version 0.26 - aws_sdk_dynamodbstreams_0_27
aws-sdk-dynamodbstreams+0_27
Support for aws-sdk-dynamodbstreams version 0.27 - aws_sdk_dynamodbstreams_0_28
aws-sdk-dynamodbstreams+0_28
Support for aws-sdk-dynamodbstreams version 0.28 - aws_sdk_dynamodbstreams_0_29
aws-sdk-dynamodbstreams+0_29
Support for aws-sdk-dynamodbstreams version 0.29 - aws_sdk_dynamodbstreams_0_30
aws-sdk-dynamodbstreams+0_30
Support for aws-sdk-dynamodbstreams version 0.30 - aws_sdk_dynamodbstreams_0_31
aws-sdk-dynamodbstreams+0_31
Support for aws-sdk-dynamodbstreams version 0.31 - aws_sdk_dynamodbstreams_0_32
aws-sdk-dynamodbstreams+0_32
Support for aws-sdk-dynamodbstreams version 0.32 - aws_sdk_dynamodbstreams_0_33
aws-sdk-dynamodbstreams+0_33
Support for aws-sdk-dynamodbstreams version 0.33 - aws_sdk_dynamodbstreams_0_34
aws-sdk-dynamodbstreams+0_34
Support for aws-sdk-dynamodbstreams version 0.34 - aws_sdk_dynamodbstreams_0_35
aws-sdk-dynamodbstreams+0_35
Support for aws-sdk-dynamodbstreams version 0.35 - aws_sdk_dynamodbstreams_0_36
aws-sdk-dynamodbstreams+0_36
Support for aws-sdk-dynamodbstreams version 0.36 - aws_sdk_dynamodbstreams_0_37
aws-sdk-dynamodbstreams+0_37
Support for aws-sdk-dynamodbstreams version 0.37 - aws_sdk_dynamodbstreams_0_38
aws-sdk-dynamodbstreams+0_38
Support for aws-sdk-dynamodbstreams version 0.38 - aws_sdk_dynamodbstreams_0_39
aws-sdk-dynamodbstreams+0_39
Support for aws-sdk-dynamodbstreams version 0.39 - aws_sdk_dynamodbstreams_1
aws-sdk-dynamodbstreams+1
Support for aws-sdk-dynamodbstreams version 1 - Serializer codec for serializing a list of binaries as a set
- Serializer codec for serializing a list of numbers as a set
- rusoto_dynamodb_0_46
rusoto_dynamodb+0_46
Support for rusoto_dynamodb version 0.46 - rusoto_dynamodb_0_47
rusoto_dynamodb+0_47
Support for rusoto_dynamodb version 0.47 - rusoto_dynamodb_0_48
rusoto_dynamodb+0_48
Support for rusoto_dynamodb version 0.48 - rusoto_dynamodbstreams_0_46
rusoto_dynamodbstreams+0_46
Support for rusoto_dynamodbstreams version 0.46 - rusoto_dynamodbstreams_0_47
rusoto_dynamodbstreams+0_47
Support for rusoto_dynamodbstreams version 0.47 - rusoto_dynamodbstreams_0_48
rusoto_dynamodbstreams+0_48
Support for rusoto_dynamodbstreams version 0.48 - Serializer codec for serializing a list of strings as a set
Structs§
- A structure that deserializes
AttributeValue
s into Rust values. - This type represents all possible errors that can occur when serializing or deserializing DynamoDB data.
- An item that comes from DynamoDb.
- Multiple items that come from DynamoDb.
- A structure for serializing Rust values into
AttributeValue
s.
Enums§
- The value for an attribute that comes from DynamoDb.
Functions§
- Interpret an
AttributeValue
as an instance of typeT
. - Interpret an
Item
as an instance of typeT
. - Interpret a
Items
as aVec<T>
. - Convert a
T
into anAttributeValue
. - Convert a
T
into anItem
.
Type Aliases§
- Alias for a
Result
with the error typeserde_dynamo::Error