Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add derive DeriveIntoActiveModel and IntoActiveValue trait #240

Merged
merged 4 commits into from
Oct 13, 2021
Merged

Add derive DeriveIntoActiveModel and IntoActiveValue trait #240

merged 4 commits into from
Oct 13, 2021

Conversation

tqwewe
Copy link
Contributor

@tqwewe tqwewe commented Oct 11, 2021

Creates a new derive macro DeriveIntoActiveModel for implementing IntoActiveModel on structs. This is useful for creating your own struct with only partial fields of a model, for example an insert struct, or update struct.

// Define regular model as usual
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
pub struct Model {
    pub id: Uuid,
    pub created_at: DateTimeWithTimeZone,
    pub updated_at: DateTimeWithTimeZone,
    pub email: String,
    pub password: String,
    pub full_name: Option<String>,
    pub phone: Option<String>,
}

// Create a new struct with some fields omitted
#[derive(DeriveIntoActiveModel)]
pub struct NewUser {
    // id, created_at and updated_at are omitted from this struct, and will always be `ActiveValue::unset`
    pub email: String,
    pub password: String,
    pub full_name: String, // Full name is usually optional, but it can be required here
    pub phone: Option<String>, // Option implements `IntoActiveValue`, and when `None` will be `unset`
}

#[derive(DeriveIntoActiveModel)]
pub struct UpdateUser {
    pub phone: Option<Option<String>>, // Option<Option<T>> allows for Some(None) to update the column to be NULL
}

fn main() {
    let new_user = NewUser {
        email: "[email protected]".to_string(),
        password: "test".to_string(),
        full_name: "John Doe".to_string(),
        phone: None,
    };
    
    let active_model = new_user.into_active_model();
    // active_model can now be used in queries...
}

Additionally, an attribute active_model is available in case your active model struct has a custom name.

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = MyActiveModel)]
pub struct NewProduct {
    // ...
}

@tyt2y3 tyt2y3 merged commit d5e95b0 into SeaQL:master Oct 13, 2021
@tyt2y3
Copy link
Member

tyt2y3 commented Oct 13, 2021

Is it better if the active_model attribute is a string?
I discovered it is not possible to have :: in the value

Can't compile

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = my_mod::MyActiveModel)]
pub struct NewProduct {
    // ...
}

Should work

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = "my_mod::MyActiveModel")]
pub struct NewProduct {
    // ...
}

@tqwewe
Copy link
Contributor Author

tqwewe commented Oct 14, 2021

Is it better if the active_model attribute is a string?
I discovered it is not possible to have :: in the value

Can't compile

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = my_mod::MyActiveModel)]
pub struct NewProduct {
    // ...
}

Should work

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = "my_mod::MyActiveModel")]
pub struct NewProduct {
    // ...
}

Oh I wasn't aware that wouldn't work. I guess it's better to use a string then. I can create another PR to change this if you'd like?

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 14, 2021

Oh I wasn't aware that wouldn't work. I guess it's better to use a string then. I can create another PR to change this if you'd like?

Sure, that'd be appreciated!
(And it is more in line with DeriveRelation)

@billy1624 billy1624 mentioned this pull request Oct 15, 2021
8 tasks
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Oct 18, 2021
tyt2y3 pushed a commit to SeaQL/seaql.github.io that referenced this pull request Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants