Replies: 1 comment 3 replies
|
Hi, yes, this is possible in three ways. I would recommend the first but the others are worth considering as well:
#[derive(DbType)]
struct Name {
first: String,
last: String,
}
#[derive(DbElement)]
struct Person {
#[agdb(flatten)]
name: Name,
age: u64,
}This will create an element with keys: #[derive(DbType)]
struct Name {
#[agdb(rename = "first_name")]
first: String,
#[agdb(rename = "last_name")]
last: String,
}Then when searching you use those fields as normal, e.g. QueryBuilder::select().element::<Person>() .search().from(1).where_().key("last_name").value("Doe").query();
#[derive(Clone, DbValue, DbSerialize)]
struct Name {
first: String,
last: String,
}
#[derive(DbElement)]
struct Person {
name: Name,
age: u64,
}This will create a single value by serializing the QueryBuilder::select().element::<Person>() .search().from(1).where_().key("name").value(Comparison::Contains("Doe".as_bytes().into())).query();
The issue with the binary value is that it might not be as nice for non-ASCII names or interact well with conditions such as impl From<Name> for DbValue {
fn from(name: Name) -> Self {
format!("{} {}", name2.first, name2.last).into()
}
}
impl TryFrom<DbValue> for Name {
type Error = DbError;
fn try_from(value: DbValue) -> Result<Self, Self::Error> {
let value_str = value.string()?;
let parts: Vec<&str> = value_str.split_whitespace().collect();
if parts.len() != 2 {
return Err(DbError::serialization(
agdb::DbErrorType::NotEnoughData,
"Invalid format for Name. Expected 'first last'.",
));
}
Ok(Name {
first: parts[0].to_string(),
last: parts[1].to_string(),
})
}
}This will create a single field "name" and in it will be concatenated "first last". That will allow you to search with QueryBuilder::select().element::<Person>() .search().from(1).where_().key("name").value(Comparison::Contains("Doe".into())).query(); |
Uh oh!
There was an error while loading. Please reload this page.
Does agdb support nested queries, for example:
where I want to query the person's first and last name?
All reactions