feat: one-to-many relation helper

Allow one to specify that a field of a model is a foreign key.
It will generate a bunch of helper methods to query related entities
from one entity.
This commit is contained in:
Matthieu Bessat 2025-11-11 17:10:47 +01:00
parent 32ef1f7b33
commit 5f45671b74
25 changed files with 764 additions and 140 deletions

View file

@ -10,12 +10,54 @@ struct Model {
fields: Vec<Field>
}
#[derive(Debug)]
impl Model {
// pub fn concrete_fields(&self) -> Vec<Field> {
// self.fields.iter().map(|f| {
// if f.is_foreign_ref {
// Field {
// name: f.name.clone(),
// rust_type: "String".into(),
// is_nullable: f.is_nullable.clone(),
// is_unique: f.is_unique.clone(),
// is_primary: false,
// is_foreign_ref: false
// }
// } else {
// f.clone()
// }
// }).collect()
// }
}
#[derive(Debug, Clone)]
#[fully_pub]
struct ForeignRefParams {
/// eg. "tokens"
reverse_relation_name: String,
/// eg. "user"
target_resource_name: String,
// /// eg. "users"
// target_resource_name_plural: String
}
#[derive(Debug, Clone)]
#[fully_pub]
enum FieldForeignMode {
ForeignRef(ForeignRefParams),
NotRef
}
#[derive(Debug, Clone)]
#[fully_pub]
struct Field {
name: String,
rust_type: String,
is_nullable: bool,
is_unique: bool,
is_primary: bool
is_primary: bool,
foreign_mode: FieldForeignMode,
// is_foreign_ref: bool,
// reverse_relation_name: Option<String>
}