Replies: 3 comments 2 replies
-
Thank you for your suggestion. leptos is really cool! I am interested in making client-server interchange seamless. Hence, I'd suggest, we need to use a JSON tokenizer somewhere and implement the parser our own. The benefit is, since ActiveModel does not nest, the implementation is much simpler and potentially faster (as there will be no recursion). Edit: the special case is if there is a JSON field value, we'd need to delegate the parsing to serde. |
Beta Was this translation helpful? Give feedback.
-
Serde doesn't force us to use null/undefined, there are other ways to serialize enums, e.g. using a |
Beta Was this translation helpful? Give feedback.
-
I've written a quick and dirty proof of concept for this, which seems to work It basically matches a new value in the let deserialize_serialize_tokens = if derive_deserialize_serialize {
quote!(, Deserialize, Serialize)
} else{
quote!()
};
Ok(quote!(
#[doc = " Generated by sea-orm-macros"]
#[derive(Clone, Debug, PartialEq #deserialize_serialize_tokens)]
pub struct ActiveModel {
#(
#[doc = " Generated by sea-orm-macros"]
pub #field: sea_orm::ActiveValue<#ty>
),*
}
[...] An example of it's usage is like this: #[derive(Clone, Debug, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "item_event", derive_active_model_deserialize_serialize)]
pub struct Model {
// Only None before insertion, as DB sets
#[sea_orm(primary_key)]
pub id: i32,
pub user_id: i32,
pub item_id: i32,
pub qr_code: String,
pub name: String,
pub picture: Vec<u8>,
pub removed: bool,
pub event_at: DateTime<Utc>
} Just a starting point, but seems to run okay. |
Beta Was this translation helpful? Give feedback.
-
I would like
serde::{Serialize, Deserialize}
to be implemented onActiveModel
and not just onModel
as it is today.Motivation
I'm writing a leptos web app that uses Rust on both the server and in the browser via wasm, both server and client code written in the same code base. Communication between the server and the client happens via serde-serialized structs. The web app has several pages that show table rows from the database, allow the user to edit the data, and click "save" to update the database.
What this means in the background is that when the user opens the page, the browser sends a request to the server, which serializes a seq_orm
Model
and sends it back to the client. The client deserializes it back into the sameModel
struct and displays its content. If the user clicks the "save" button, the client assembles a newModel
object, sends it to the server, the server converts theModel
object into anActiveModel
(usingmodel.into().reset_all()
), and updates the database row usingActiveModel::update()
.However, because the server doesn't know which fields the client actually changed, it has to use
.reset_all()
and write all fields to the row. It would be much nicer if, instead of passingModel
between the server and the client, we were passing anActiveModel
that would remember which fields the client actually changed, and then can directly send thatActiveModel
back to the server and the server only updates the modified fields.There's mainly two reasons for this
Proposed Solutions
Add
serde::{Serialize,Deserialize}
implementations toActiveModel
.Current Workarounds
Model
between the server and the client, but this isn't great because it requires writing the full row each time.ActiveModel
already implements exactly what I need.ActiveModel::set_from_json
is another way, using my own json objects. But this also means I have to implement change tracking manually instead of just using the ability of the leptos framework to pass serializable Rust structs seamlessly between the server and the browser.Note also that I found other issues discussing this previously but they were mostly ok with the "implement change tracking manually" approach because they had to do that anyways. They didn't use web frameworks that would have allowed them to seamlessly reuse
ActiveModel
on the client, so they had to use custom json anyways andActiveModel::set_from_json
was good enough. This is not the case for me as described above.Beta Was this translation helpful? Give feedback.
All reactions