rocket_db_pools::diesel::prelude

Trait SaveChangesDsl

Source
pub trait SaveChangesDsl<Conn> {
    // Provided method
    fn save_changes<'life0, 'async_trait, T>(
        self,
        connection: &'life0 mut Conn,
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: Sized + Identifiable + Send + 'async_trait,
             Conn: UpdateAndFetchResults<Self, T>,
             T: 'async_trait { ... }
}
Expand description

Sugar for types which implement both AsChangeset and Identifiable

On backends which support the RETURNING keyword, foo.save_changes(&conn) is equivalent to update(&foo).set(&foo).get_result(&conn). On other backends, two queries will be executed.

§Example

use diesel_async::{SaveChangesDsl, AsyncConnection};

#[derive(Queryable, Debug, PartialEq)]
struct Animal {
   id: i32,
   species: String,
   legs: i32,
   name: Option<String>,
}

#[derive(AsChangeset, Identifiable)]
#[diesel(table_name = animals)]
struct AnimalForm<'a> {
    id: i32,
    name: &'a str,
}

let form = AnimalForm { id: 2, name: "Super scary" };
let changed_animal = form.save_changes(connection).await?;
let expected_animal = Animal {
    id: 2,
    species: String::from("spider"),
    legs: 8,
    name: Some(String::from("Super scary")),
};
assert_eq!(expected_animal, changed_animal);

Provided Methods§

Source

fn save_changes<'life0, 'async_trait, T>( self, connection: &'life0 mut Conn, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: Sized + Identifiable + Send + 'async_trait, Conn: UpdateAndFetchResults<Self, T>, T: 'async_trait,

See the trait documentation

Implementors§

Source§

impl<T, Conn> SaveChangesDsl<Conn> for T
where T: Copy + AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,