Replies: 2 comments 2 replies
-
Hi, let me first explain how it currently works. The default implementation of the Now there are two issues:
Consider: let node = Node { attributes: vec![Attribute { name: "path".into(), value: "/some/path".into() }], ..default()}; There already is Even without the above issues resolved you can currently do this to store your struct in the db. I am omitting the other fields focusing on the struct Attribute {
key: String,
value: String,
}
struct Node {
db_id: Option<DbId>,
// path: NodePath,
// ntype: NodeType,
// nphys: NodePhysicality,
// created_time: SysTime,
// modified_time: SysTime,
attributes: Vec<Attribute>,
}
impl DbUserValue for Node {
fn db_id(&self) -> Option<QueryId> {
self.db_id.map(QueryId::Id)
}
fn db_keys() -> Vec<DbValue> {
vec!["attributes".into()]
}
fn from_db_element(element: &DbElement) -> std::result::Result<Self, DbError> {
let attributes = element.values[0]
.value
.vec_string()?
.iter()
.map(|a| {
if let Some(kv) = a.split_once(':') {
Ok(Attribute {
key: kv.0.to_string(),
value: kv.1.to_string(),
})
} else {
Err(DbError::from("bad value"))
}
})
.collect::<Result<Vec<Attribute>, DbError>>()?;
Ok(Self {
db_id: Some(element.id),
attributes,
})
}
fn to_db_values(&self) -> Vec<agdb::DbKeyValue> {
vec![(
"attributes",
self.attributes
.iter()
.map(|a| format!("{}:{}", a.key, a.value))
.collect::<Vec<String>>(),
)
.into()]
}
} |
Beta Was this translation helpful? Give feedback.
-
@teodosin Hi, both issues I opened for this are now fixed and they should simplify supporting your use case. With #1139 if you do not specify any keys the db will return all keys. With #1138 you can now use custom types in vectors as long as they implement Please let me know if you issues with the implementation of if you have other stuff you would wish to be supported in |
Beta Was this translation helpful? Give feedback.
-
Hey! I have a question regarding a manual implementation of DbUserValue. Made this as a discussion instead of an issue because I'm not certain if this is an actual problem with your library and not just a misunderstanding on my part. Hoping to clear this up.
I've got a Node struct with the following definition:
Where Attribute is just a struct with a name and value, just like a DbKeyValue. The vector of attributes is a new addition before which there were no issues with using the UserValue macro. Since I couldn't find a way to
impl From<Vec<DbKeyValue>> for Vec<Attribute>
(if there is a way, do let me know), I tried to manually implement the trait to get all my attributes transferred, and here's where my confusion lies.The trait definition is odd to me:
Why does db_keys not have access to self? Am I misunderstanding its purpose? I have no way of providing the function with the keys from the attributes vector.
Beta Was this translation helpful? Give feedback.
All reactions