AWScala
AWScala copied to clipboard
dynamodbv2.Item doesn't support getting attributes by key
All DynamoDB get operations (get, query, scan, etc...) return Item objects. While the AWS API returns a Map, the Item object turns it into a simple Seq(name, AttributeValue). That means that to get the value of a specific attribute (which I assume is a common case you have to either turn it back into a Map or iterate. It would be much more useful to retain the attribute names and be able to do, for example, item("attribute").
Here's my workaround for that:
import awscala._, dynamodbv2._
class EnhancedDynamoItem(item: Item) {
def attributesMap: Map[String, AttributeValue] =
item.attributes
.foldLeft(Map.empty[String, AttributeValue])((m, a) =>
m ++ Map(a.name -> a.value))
}
object ToEnhancedDynamoOps {
implicit def toEnhancedItem(item: Item): EnhancedDynamoItem =
new EnhancedDynamoItem(item)
}