I have the following simple scenario for a REST API and I'm not sure how should I do it idiomatically in Rust. So basically there's a get˙function that returns a variable type X from the database. This get function currently receives a sqlx::MysqlPool, creates a transaction, runs the queries to get the data, commits, returns it, done. So far so good.
I have multiple endpoints, but for this example let's say there's an update function, which should update the record in the database and return the object afterwards.
My problem is that I'm not sure how I can do this properly.
I could just do the
updatewith one transaction, and reuse thegetfunction afterwards, but that would be two separate transactions and so it's theoretically possible that between those transactions an other client updates the same record as well. Also it doesn't seem optimal.Somehow share the same transaction between those functions, but in that case I would need to start the transaction outside of any of these functions, which somehow doesn't feel right.
Simply duplicate most of the
getfunction's code in theupdatefunction, but that doesn't seem to be a good idea, specially if I need to do it in 4-5 functions.
How do people get around this issue?