NyxCode / ormx

bringing orm-like features to sqlx
MIT License
287 stars 32 forks source link

Derive Serialize and Deserialize for the generated insert structs #28

Closed Elieroz closed 5 days ago

Elieroz commented 2 years ago

I added a new feature called serde to derive serde's Serialize and Deserialize traits for the generated insert structs when enabled.

This way, these structs can be easily used in web applications built with frameworks or libraries that require the structs that represent a request's body to be serializable to and deserializable from a JSON document.

Punie commented 2 years ago

I find the idea interesting but I'm concerned about the fact that activating the serde feature will implement Serialize and Deserialize indiscrimately for all Insert structs.

I think there should be additional attributes to opt-in to this, eg (naming for these attributes is bad but I only thought about it for 30sec 😅):

#[derive(Debug, ormx::Table)]
#[ormx(table = "foo", id = id, insertable, insertable_derive_deserialize, insertable_derive_serialize)]
struct Foo {
    id: i32,
    bar: String,
}
Elieroz commented 2 years ago

Oh, I just assumed this would always be desirable. Whoops!

I'll follow your suggestion!

NyxCode commented 5 days ago

This can be achieved with

#[ormx(table = "foo", id = id, insertable = #[derive(Serialize) InsertFoo)]