feat: implement server-side conversions
The ValueType enum that we've created can now be converted directly into an AWS DynamoDB AttributeValue type, meaning that storage on the server does not require the application to store serialized maps of data. Benefits of this: queries using secondary indexes, simpler filtering, less storage required per database item, faster parsing. We can also start to move away from the ExpandableInfo struct, as we really just use the HashMap inside it.
This commit is contained in:
1 parent
247d0dfa73
commit
97ad9ceee7
1 file changed
+28
@@ -113,3 +113,31 @@ impl Display for ValueType {
|
||||
write!(f, "{}", str)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
use aws_sdk_dynamodb::types::AttributeValue;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl From<&AttributeValue> for ValueType {
|
||||
fn from(attr: &AttributeValue) -> Self {
|
||||
match attr {
|
||||
AttributeValue::S(s) => ValueType::String(Some(s.clone())),
|
||||
AttributeValue::N(n) => ValueType::Number(Some(n.clone())),
|
||||
AttributeValue::L(l) => ValueType::List(Some(l.iter().map(|v| v.into()).collect())),
|
||||
AttributeValue::M(m) => ValueType::Map(Some(m.iter().map(|(k, v)| (k.clone(), v.into())).collect())),
|
||||
_ => ValueType::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl Into<AttributeValue> for ValueType {
|
||||
fn into(self) -> AttributeValue {
|
||||
match self {
|
||||
ValueType::String(s) => AttributeValue::S(s.unwrap_or_default()),
|
||||
ValueType::Number(n) => AttributeValue::N(n.unwrap_or_default()),
|
||||
ValueType::List(l) => AttributeValue::L(l.unwrap_or_default().into_iter().map(|v| v.into()).collect()),
|
||||
ValueType::Map(m) => AttributeValue::M(m.unwrap_or_default().into_iter().map(|(k, v)| (k, v.into())).collect()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user