mirror of
https://github.com/Phundrak/georm.git
synced 2025-12-13 16:31:51 +01:00
feat: add defaultable field support with companion struct generation
Introduces support for `#[georm(defaultable)]` attribute on entity fields. When fields are marked as defaultable, generates companion `<Entity>Default` structs where defaultable fields become `Option<T>`, enabling easier entity creation when some fields have database defaults or are auto-generated. Key features: - Generates `<Entity>Default` structs with optional defaultable fields - Implements `Defaultable<Id, Entity>` trait with async `create` method - Validates that `Option<T>` fields cannot be marked as defaultable - Preserves field visibility in generated companion structs - Only generates companion struct when defaultable fields are present
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use georm::Georm;
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(
|
||||
table = "biographies",
|
||||
one_to_one = [{
|
||||
@@ -13,7 +13,7 @@ pub struct Biography {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(table = "authors")]
|
||||
pub struct Author {
|
||||
#[georm(id)]
|
||||
@@ -35,7 +35,7 @@ impl Ord for Author {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Georm, PartialEq, Eq, Default)]
|
||||
#[georm(
|
||||
table = "books",
|
||||
one_to_many = [
|
||||
@@ -68,7 +68,7 @@ impl Ord for Book {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq)]
|
||||
#[derive(Debug, Georm, PartialEq, Eq)]
|
||||
#[georm(table = "reviews")]
|
||||
pub struct Review {
|
||||
#[georm(id)]
|
||||
@@ -78,7 +78,7 @@ pub struct Review {
|
||||
pub review: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq)]
|
||||
#[derive(Debug, Georm, PartialEq, Eq)]
|
||||
#[georm(
|
||||
table = "genres",
|
||||
many_to_many = [{
|
||||
|
||||
Reference in New Issue
Block a user