-
Notifications
You must be signed in to change notification settings - Fork 10
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
Update with a variable set of fields using MaybeUnset #35
Comments
In Charybdis we make use of Lets say we have model user: #[charybdis_model(
table_name = users,
partition_keys = [id],
clustering_keys = [],
)]
#[derive(Serialize, Deserialize, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: Uuid,
pub username: Text,
pub email: Text,
pub password: Text,
pub first_name: Text,
pub last_name: Text,
pub bio: Option<Text>,
#[serde(default = "chrono::Utc::now")]
pub created_at: Timestamp,
#[serde(default = "chrono::Utc::now")]
pub updated_at: Timestamp,
} and we want to update partial_user!(UpdateBioUser, id, bio, updated_at); Now we have
If you use actix web you can define action like this: #[put("/bio")]
pub async fn update_bio(data: RequestData, client_session: Session, mut user: web::Json<UpdateBioUser>) -> Response {
user.auth_update(&data).await?;
user.update_cb(&data).execute(data.db_session()).await?;
// or if you don't use callbacks
// user.update().execute(data.db_session().await?;
Ok(HttpResponse::Ok().json(user))
} so you can utilize partial model Note that |
@GoranBrkuljan I am familiar with |
Here is my full server function (Leptos with actix_web). If an argument is None, its corresponding column in Scylla remains untouched.
|
Well, we don't support that ATM. In our platform we have endpoint for each set of fields that we want to update at once, and we know them in advance. That is why I recommended partial models. Here you would like to have single action for many possible updates on the same model without knowing in advance what set of fields are going to be updated. We could maybe introduce Other option is to create helper macro, like we have for find find_user!("username = ?", (username,)).execute(db).await?; So if implemented we could do: update_user!(
"first_name = ?, last_name = ?",
(
first_name.map_or(Unset, |x| Set(x)),
last_name.map_or(Unset, |x| Set(x)),
)
)
.execute(db)
.await?; Let me know if something like this would be helpful. |
I guess a macro like that ( A better approach in my view is implement a macro
This would also be really cool if implemeted: |
Here's how I am doing it with the native driver:
Is there a Charybdis alternative to this?
The text was updated successfully, but these errors were encountered: