Which rust embedded db supports nested structures?

Say if I had this code:

struct Customer
{
    id: u16,
    name: Name,
    age: u8,
    address: Address,
    is_active: bool,
}

struct Address
{
    street_no: u8,
    address: String,
    city: String,
    country: String,
}

struct Name
{
    first: String,
    last: String,
}

fn main()
{
    let customer = Customer{id: 10, name: Name{first: "John".to_string(), last: "Doe".to_string()},
    age: 32, is_active: false, address: Address
    {street_no: 1, address: "23 bakers street".to_string(), city: "New York".to_string(),
    country: "USA".to_string(),}
    };
}

I want a database that supports nested structures, has a seaorm like syntax and can query stuff that has got nested fields e.g. the first name or the street number.

What if I used serde/postcard instead, would this be ok in regards to performance, particularly if customer was a vector and contained thousands of records.

It is my opinion that if you have structured data you should use a relational database. So here it probably means SQLite if you want to embed it. You'll always be able to fallback to unstructured data by using a json column with the json functions and operators.

Yes it does mean learning some SQL. But libraries like sqlx make it really easy to map a SQL query to an arbitrary Rust struct.

1 Like

Is this like serializing it to json and deserializing it if I used it with Rust?

I know SQL and can use it, but I don't like typing every command as a string, you know big issue with this is it will compile even if there is a syntax error, plus no auto completion etc, that is why I wanted more of a seaorm like syntax.

According to chage ai, apparently seaorm does support nested structures though I do not know if this is correct or not this is what it gave me:

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "users")]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
    pub address: Address,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Address {
    pub street: String,
    pub city: String,
    pub state: String,
    pub zip: String,
}


let users = User::find()
    .filter(address.street.eq("123 Main St"))
    .all(db)
    .await?;

don't know this is valid or not.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.