Trait dynomite::Item[][src]

pub trait Item: IntoAttributes + FromAttributes {
    fn key(&self) -> Attributes;
}
Expand description

A type which can be converted to and from a set of String keys and AttributeValues.

Examples

Below is an example of doing this manually for demonstration.

use dynomite::{
    dynamodb::AttributeValue, Attribute, AttributeError, Attributes, FromAttributes,
    IntoAttributes, Item,
};
use std::{collections::HashMap, convert::TryFrom};

#[derive(PartialEq, Debug, Clone)]
struct Person {
    id: String,
}

impl Item for Person {
    fn key(&self) -> Attributes {
        let mut attrs = HashMap::new();
        attrs.insert("id".into(), "123".to_string().into_attr());
        attrs
    }
}

impl FromAttributes for Person {
    fn from_attrs(attrs: &mut Attributes) -> Result<Self, AttributeError> {
        Ok(Self {
            id: attrs
                .remove("id")
                .and_then(|val| val.s)
                .ok_or_else(|| AttributeError::MissingField { name: "id".into() })?,
        })
    }
}

impl IntoAttributes for Person {
    fn into_attrs(
        self,
        attrs: &mut Attributes,
    ) {
        attrs.insert("id".into(), "123".to_string().into_attr());
    }
}

// Unfortunately `dynomite` is not able to provide a blanket impl for std::convert traits
// due to orphan rules, but they are generated via the `dynomite_derive` attributes

impl TryFrom<Attributes> for Person {
    type Error = AttributeError;

    fn try_from(mut attrs: Attributes) -> Result<Person, AttributeError> {
        Person::from_attrs(&mut attrs)
    }
}

impl From<Person> for Attributes {
    fn from(person: Person) -> Attributes {
        let mut map = HashMap::new();
        person.into_attrs(&mut map);
        map
    }
}

let person = Person { id: "123".into() };
let attrs: Attributes = person.clone().into();
assert_eq!(Ok(person), Person::try_from(attrs))

You can get this all for free automatically using #[derive(Item)] on your structs. This is the recommended approach.

use dynomite::Item;
#[derive(Item)]
struct Book {
    #[dynomite(partition_key)]
    id: String,
}

Renaming fields

In some cases you may be dealing with a DynamoDB table whose fields are named using conventions that do not align with Rust’s conventions. You can leverage the rename attribute to map Rust’s fields back to its source name explicitly

use dynomite::Item;

#[derive(Item)]
struct Book {
    #[dynomite(partition_key)]
    id: String,
    #[dynomite(rename = "notConventional")]
    not_conventional: String,
}

Accommodating sparse data

In some cases you may be dealing with a DynamoDB table whose fields are absent for some records. This is different than fields whose records have NULL attribute type values. In these cases you can use the default field attribute to communicate that the std::default::Default::default() value for the fields type will be used in the absence of data.

use dynomite::Item;

#[derive(Item)]
struct Book {
    #[dynomite(partition_key)]
    id: String,
    #[dynomite(default)]
    summary: Option<String>,
}

Item attribute projections

DynamoDB Items are a set of attributes with a uniquely identifying partition key. At times, you may wish to project over these attributes into a type that does not include a partition_key. For that specific purpose, instead of deriving an Item type you’ll want to derive Attributes

use dynomite::Attributes;

#[derive(Attributes)]
struct BookProjection {
  author: String,
  #[dynomite(default)]
  summary: Option<String>
}

Required methods

Returns the set of attributes which make up this item’s primary key

This is often used in item look ups

Implementors