@@ -198,23 +198,34 @@ where
198198 async ( feature = "async" )
199199) ]
200200pub trait ManyOps < T : DataObject > {
201+ /// Save all unsaved relation changes to the backend.
202+ ///
201203 /// Used by macro-generated code. You do not need to call this directly.
204+ ///
205+ /// This will insert added values first, and then remove the removed values.
206+ /// Use inside a transaction to provide atomicity.
202207 async fn save ( & mut self , conn : & impl ConnectionMethods ) -> Result < ( ) > ;
203208
204- /// Delete all references from the database, and any unsaved additions.
209+ /// Delete all references from the backend, and any unsaved additions.
210+ ///
211+ /// This operation is atomic.
205212 async fn delete ( & mut self , conn : & impl ConnectionMethods ) -> Result < ( ) > ;
206213
207- /// Loads the values referred to by this many relationship from the
208- /// database if necessary and returns a reference to them.
214+ /// Overwrite the references in the backend, and clears unsaved additions.
215+ ///
216+ /// This will call `delete()` first, and then insert the items scheduled to be added.
217+ /// Use inside a transaction to provide atomicity.
218+ async fn set ( & mut self , conn : & impl ConnectionMethods , values : Vec < T > ) -> Result < ( ) > ;
219+
220+ /// Loads the values referred to by this many relationship from the backend if necessary.
209221 async fn load < ' a > (
210222 & ' a self ,
211223 conn : & impl ConnectionMethods ,
212224 ) -> Result < impl Iterator < Item = & ' a T > >
213225 where
214226 T : ' a ;
215227
216- /// Loads and orders the values referred to by this many relationship from a
217- /// database if necessary and returns a reference to them.
228+ /// Loads and orders the values referred to by this many relationship from the backend if necessary.
218229 async fn load_ordered < ' a > (
219230 & ' a self ,
220231 conn : & impl ConnectionMethods ,
@@ -274,6 +285,18 @@ impl<T: DataObject> ManyOps<T> for Many<T> {
274285 Ok ( ( ) )
275286 }
276287
288+ async fn set ( & mut self , conn : & impl ConnectionMethods , values : Vec < T > ) -> Result < ( ) > {
289+ ManyOps :: delete ( self , conn) . await ?;
290+
291+ for value in & values {
292+ self . add ( value) ?;
293+ }
294+ ManyOps :: save ( self , conn) . await ?;
295+
296+ self . all_values = OnceLock :: from ( values) ;
297+ Ok ( ( ) )
298+ }
299+
277300 async fn load < ' a > (
278301 & ' a self ,
279302 conn : & impl ConnectionMethods ,
0 commit comments