Hello
I have the following - heavily simplified - database-structure:
These are all separate tables in my database.
The question I have now: Is it better in my Rust-code to include the relations in my structs. Or keep them separate?
Option 1: Include relations in my structs.
pub struct Restaurant {
id: u64,
name: String,
description: String,
dishes: std::vec::Vec<Dish>, // RELATION TO DISH IS INCLUDED IN THIS PARENT STRUCT.
}
pub struct Dish {
id: u64,
name: String,
ingredients: String,
dish_variations: std::vec::Vec<DishVariation>, // RELATION TO DISHVARIATON IS INCLUDED IN THIS PARENT STRUCT.
dish_extras: std::vec::Vec<DishExtra>, // RELATION TO DISHEXTRA IS INCLUDED IN THIS PARENT STRUCT.
}
pub struct DishVariation {
id: u64,
name: String,
price: f64,
}
pub struct DishExtra {
id: u64,
name: String,
price: f64,
}
In this case I could in my Askama templates access all the data of a restaurant simple by using restaurant.name
, restaurant.dish[0].name
,...
Option 2: Separate relations in my structs.
pub struct Restaurant {
id: u64,
name: String,
description: String,
}
pub struct Dish {
id: u64,
name: String,
ingredients: String,
}
pub struct DishVariation {
id: u64,
name: String,
price: f64,
}
pub struct DishExtra {
id: u64,
name: String,
price: f64,
}
Is this option the structs are identical to the database-model. If I for example want to access the dishes a Restaurant has, I'd use a custom function like get_dishes_of_restaurant(id: u64) -> std::vec::Vec<Dish> {...}
.
What is the best option?