pub trait SaveRepositoryTransaction<M>: SaveRepository<M> + TransactionRepository<M>{
// Provided method
fn save_in_transaction<'a>(
&'a self,
model: M,
) -> impl Future<Output = Result<M, Error>> + Send + 'a
where M: 'a { ... }
}Expand description
Extension trait for Save operations with transactions.
This trait provides convenience methods for using transactions with repositories
that implement SaveRepository. It’s automatically implemented for any type that
implements both SaveRepository<M> and TransactionRepository<M>.
Provided Methods§
Sourcefn save_in_transaction<'a>(
&'a self,
model: M,
) -> impl Future<Output = Result<M, Error>> + Send + 'awhere
M: 'a,
fn save_in_transaction<'a>(
&'a self,
model: M,
) -> impl Future<Output = Result<M, Error>> + Send + 'awhere
M: 'a,
Saves a model in a transactions, ensuring atomicity.
This method:
- Creates a transactions using
with_transaction - Calls
save_with_executorwith the transactions - Returns the model on successful save
§Parameters
model: The model to save
§Returns
A future that resolves to:
Ok(M): The saved model on successErr(crate::Error): The error if saving failed
§Example
let saved_model = repo.save_in_transaction(model).await?;Examples found in repository?
examples/basic_repo.rs (line 197)
175async fn main() {
176 install_default_drivers();
177
178 initialize_db_pool(
179 PoolOptions::new()
180 .max_connections(21)
181 .min_connections(5)
182 .idle_timeout(Duration::from_secs(60 * 10))
183 .max_lifetime(Duration::from_secs(60 * 60 * 24))
184 .acquire_timeout(Duration::from_secs(20))
185 .connect(&DATABASE_URL)
186 .await
187 .expect("Failed to connect to database"),
188 );
189
190 let user = User {
191 id: 1,
192 name: String::new(),
193 };
194
195 USER_REPO.save_ref(&user).await.unwrap();
196
197 USER_REPO.save_in_transaction(user.clone()).await.unwrap();
198
199 USER_REPO
200 .save_with_context(user.clone(), UserContext::System)
201 .await
202 .unwrap();
203
204 USER_REPO.with_transaction(action).await.unwrap();
205
206 USER_REPO
207 .delete_by_values_in_transaction("id", [1, 2, 3, 11, 22])
208 .await
209 .unwrap();
210}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.