weldsorm / welds

BSD 3-Clause "New" or "Revised" License
119 stars 6 forks source link

Add a `replace_inner` method to `DbState` #36

Closed RustyNova016 closed 1 month ago

RustyNova016 commented 1 month ago

It would be great to have a method that replace the content of a DbState struct to another (as long as the PK is the same). This would allow easy overwrite of rows without mapping each fields to the other, potentially forgetting some in the process.

For example:

let mut old_data = Foo::find_by_id(&client, 1).await?;  // Returns Foo { id: 1, val: 33 }
let new_data = Foo { id: 1, val: 42 }

old_data.replace_inner(new_data) // Set the value to Foo { id: 1, val: 42 }, and DbStatus to Edited.
old_data.save(&client).await?; // Just save it now!