Revert save to return Self

This commit is contained in:
Chris Tsang 2021-12-25 22:55:39 +08:00
parent cf685fed26
commit d5c9c65079
9 changed files with 51 additions and 51 deletions

View File

@ -46,14 +46,14 @@ pub async fn save_active_model(db: &DbConn) -> Result<(), DbErr> {
name: Set("Banana".to_owned()), name: Set("Banana".to_owned()),
..Default::default() ..Default::default()
}; };
let mut banana: fruit::ActiveModel = banana.save(db).await?.into_active_model(); let mut banana: fruit::ActiveModel = banana.save(db).await?;
println!(); println!();
println!("Inserted: {:?}\n", banana); println!("Inserted: {:?}\n", banana);
banana.name = Set("Banana Mongo".to_owned()); banana.name = Set("Banana Mongo".to_owned());
let banana: fruit::ActiveModel = banana.save(db).await?.into_active_model(); let banana: fruit::ActiveModel = banana.save(db).await?;
println!(); println!();
println!("Updated: {:?}\n", banana); println!("Updated: {:?}\n", banana);

View File

@ -381,28 +381,28 @@ pub trait ActiveModelTrait: Clone + Debug {
Self::after_save(model, false) Self::after_save(model, false)
} }
/// Insert the model if primary key is not_set, update otherwise. /// Insert the model if primary key is `NotSet`, update otherwise.
/// Only works if the entity has auto increment primary key. /// Only works if the entity has auto increment primary key.
async fn save<'a, C>(self, db: &'a C) -> Result<<Self::Entity as EntityTrait>::Model, DbErr> async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>, <Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a, Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>, C: ConnectionTrait<'a>,
{ {
let am = self;
let mut is_update = true; let mut is_update = true;
for key in <Self::Entity as EntityTrait>::PrimaryKey::iter() { for key in <Self::Entity as EntityTrait>::PrimaryKey::iter() {
let col = key.into_column(); let col = key.into_column();
if am.is_not_set(col) { if self.is_not_set(col) {
is_update = false; is_update = false;
break; break;
} }
} }
if !is_update { let res = if !is_update {
am.insert(db).await self.insert(db).await
} else { } else {
am.update(db).await self.update(db).await
} }?;
Ok(res.into_active_model())
} }
/// Delete an active model by its primary key /// Delete an active model by its primary key

View File

@ -207,7 +207,7 @@
//! }; //! };
//! //!
//! // create, because primary key `id` is `NotSet` //! // create, because primary key `id` is `NotSet`
//! let mut banana = banana.save(db).await?.into_active_model(); //! let mut banana = banana.save(db).await?;
//! //!
//! banana.name = Set("Banana Mongo".to_owned()); //! banana.name = Set("Banana Mongo".to_owned());
//! //!

View File

@ -45,7 +45,7 @@ async fn crud_cake(db: &DbConn) -> Result<(), DbErr> {
..Default::default() ..Default::default()
}; };
let mut apple = apple.save(db).await?.into_active_model(); let mut apple = apple.save(db).await?;
println!(); println!();
println!("Inserted: {:?}", apple); println!("Inserted: {:?}", apple);

View File

@ -108,7 +108,7 @@ pub async fn test_update_deleted_customer(db: &DbConn) {
init_n_customers + 1 init_n_customers + 1
); );
let customer_id = customer.id; let customer_id = customer.id.clone().unwrap();
let _ = customer.delete(db).await; let _ = customer.delete(db).await;
assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers); assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers);

View File

@ -43,7 +43,7 @@ pub async fn find_one_with_result() {
let result = Bakery::find().one(&ctx.db).await.unwrap().unwrap(); let result = Bakery::find().one(&ctx.db).await.unwrap().unwrap();
assert_eq!(result.id, bakery.id); assert_eq!(result.id, bakery.id.unwrap());
ctx.delete().await; ctx.delete().await;
} }
@ -83,13 +83,13 @@ pub async fn find_by_id_with_result() {
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
let result = Bakery::find_by_id(bakery.id.clone()) let result = Bakery::find_by_id(bakery.id.clone().unwrap())
.one(&ctx.db) .one(&ctx.db)
.await .await
.unwrap() .unwrap()
.unwrap(); .unwrap();
assert_eq!(result.id, bakery.id); assert_eq!(result.id, bakery.id.unwrap());
ctx.delete().await; ctx.delete().await;
} }

View File

@ -24,7 +24,7 @@ pub async fn left_join() {
profit_margin: Set(10.4), profit_margin: Set(10.4),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
@ -38,7 +38,7 @@ pub async fn left_join() {
bakery_id: Set(Some(bakery.id.clone())), bakery_id: Set(Some(bakery.id.clone())),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert baker"); .expect("could not insert baker");
@ -48,7 +48,7 @@ pub async fn left_join() {
bakery_id: Set(None), bakery_id: Set(None),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert baker"); .expect("could not insert baker");
@ -103,7 +103,7 @@ pub async fn right_join() {
profit_margin: Set(10.4), profit_margin: Set(10.4),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
@ -111,7 +111,7 @@ pub async fn right_join() {
name: Set("Kate".to_owned()), name: Set("Kate".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -119,7 +119,7 @@ pub async fn right_join() {
name: Set("Jim".to_owned()), name: Set("Jim".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -131,7 +131,7 @@ pub async fn right_join() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -189,7 +189,7 @@ pub async fn inner_join() {
profit_margin: Set(10.4), profit_margin: Set(10.4),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
@ -197,7 +197,7 @@ pub async fn inner_join() {
name: Set("Kate".to_owned()), name: Set("Kate".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -205,7 +205,7 @@ pub async fn inner_join() {
name: Set("Jim".to_owned()), name: Set("Jim".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -217,7 +217,7 @@ pub async fn inner_join() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -229,7 +229,7 @@ pub async fn inner_join() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -279,7 +279,7 @@ pub async fn group_by() {
profit_margin: Set(10.4), profit_margin: Set(10.4),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
@ -287,7 +287,7 @@ pub async fn group_by() {
name: Set("Kate".to_owned()), name: Set("Kate".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -299,7 +299,7 @@ pub async fn group_by() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -311,7 +311,7 @@ pub async fn group_by() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -374,7 +374,7 @@ pub async fn having() {
profit_margin: Set(10.4), profit_margin: Set(10.4),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert bakery"); .expect("could not insert bakery");
@ -382,7 +382,7 @@ pub async fn having() {
name: Set("Kate".to_owned()), name: Set("Kate".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -394,7 +394,7 @@ pub async fn having() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -406,7 +406,7 @@ pub async fn having() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -414,7 +414,7 @@ pub async fn having() {
name: Set("Bob".to_owned()), name: Set("Bob".to_owned()),
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert customer"); .expect("could not insert customer");
@ -426,7 +426,7 @@ pub async fn having() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");
@ -438,7 +438,7 @@ pub async fn having() {
..Default::default() ..Default::default()
} }
.save(&ctx.db) .insert(&ctx.db)
.await .await
.expect("could not insert order"); .expect("could not insert order");

View File

@ -42,7 +42,7 @@ async fn seed_data(db: &DatabaseConnection) {
let baker_1 = baker::ActiveModel { let baker_1 = baker::ActiveModel {
name: Set("Baker 1".to_owned()), name: Set("Baker 1".to_owned()),
contact_details: Set(serde_json::json!({})), contact_details: Set(serde_json::json!({})),
bakery_id: Set(Some(bakery.id.clone())), bakery_id: Set(Some(bakery.id.clone().unwrap())),
..Default::default() ..Default::default()
} }
.save(db) .save(db)
@ -52,7 +52,7 @@ async fn seed_data(db: &DatabaseConnection) {
let _baker_2 = baker::ActiveModel { let _baker_2 = baker::ActiveModel {
name: Set("Baker 2".to_owned()), name: Set("Baker 2".to_owned()),
contact_details: Set(serde_json::json!({})), contact_details: Set(serde_json::json!({})),
bakery_id: Set(Some(bakery.id.clone())), bakery_id: Set(Some(bakery.id.clone().unwrap())),
..Default::default() ..Default::default()
} }
.save(db) .save(db)
@ -64,7 +64,7 @@ async fn seed_data(db: &DatabaseConnection) {
price: Set(dec!(10.25)), price: Set(dec!(10.25)),
gluten_free: Set(false), gluten_free: Set(false),
serial: Set(Uuid::new_v4()), serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(bakery.id.clone())), bakery_id: Set(Some(bakery.id.clone().unwrap())),
..Default::default() ..Default::default()
}; };
@ -75,7 +75,7 @@ async fn seed_data(db: &DatabaseConnection) {
let cake_baker = cakes_bakers::ActiveModel { let cake_baker = cakes_bakers::ActiveModel {
cake_id: Set(cake_insert_res.last_insert_id as i32), cake_id: Set(cake_insert_res.last_insert_id as i32),
baker_id: Set(baker_1.id.clone()), baker_id: Set(baker_1.id.clone().unwrap()),
..Default::default() ..Default::default()
}; };
@ -97,8 +97,8 @@ async fn seed_data(db: &DatabaseConnection) {
.expect("could not insert customer"); .expect("could not insert customer");
let kate_order_1 = order::ActiveModel { let kate_order_1 = order::ActiveModel {
bakery_id: Set(bakery.id.clone()), bakery_id: Set(bakery.id.clone().unwrap()),
customer_id: Set(customer_kate.id.clone()), customer_id: Set(customer_kate.id.clone().unwrap()),
total: Set(dec!(99.95)), total: Set(dec!(99.95)),
placed_at: Set(Utc::now().naive_utc()), placed_at: Set(Utc::now().naive_utc()),
@ -112,7 +112,7 @@ async fn seed_data(db: &DatabaseConnection) {
cake_id: Set(cake_insert_res.last_insert_id as i32), cake_id: Set(cake_insert_res.last_insert_id as i32),
price: Set(dec!(10.00)), price: Set(dec!(10.00)),
quantity: Set(12), quantity: Set(12),
order_id: Set(kate_order_1.id.clone()), order_id: Set(kate_order_1.id.clone().unwrap()),
..Default::default() ..Default::default()
} }
.save(db) .save(db)
@ -123,7 +123,7 @@ async fn seed_data(db: &DatabaseConnection) {
cake_id: Set(cake_insert_res.last_insert_id as i32), cake_id: Set(cake_insert_res.last_insert_id as i32),
price: Set(dec!(50.00)), price: Set(dec!(50.00)),
quantity: Set(2), quantity: Set(2),
order_id: Set(kate_order_1.id.clone()), order_id: Set(kate_order_1.id.clone().unwrap()),
..Default::default() ..Default::default()
} }
.save(db) .save(db)
@ -243,7 +243,7 @@ async fn create_order(db: &DatabaseConnection, cake: cake::Model) {
let order = order::ActiveModel { let order = order::ActiveModel {
bakery_id: Set(cake.bakery_id.unwrap()), bakery_id: Set(cake.bakery_id.unwrap()),
customer_id: Set(another_customer.id.clone()), customer_id: Set(another_customer.id.clone().unwrap()),
total: Set(dec!(200.00)), total: Set(dec!(200.00)),
placed_at: Set(Utc::now().naive_utc()), placed_at: Set(Utc::now().naive_utc()),
@ -257,7 +257,7 @@ async fn create_order(db: &DatabaseConnection, cake: cake::Model) {
cake_id: Set(cake.id), cake_id: Set(cake.id),
price: Set(dec!(10.00)), price: Set(dec!(10.00)),
quantity: Set(300), quantity: Set(300),
order_id: Set(order.id.clone()), order_id: Set(order.id.clone().unwrap()),
..Default::default() ..Default::default()
} }
.save(db) .save(db)

View File

@ -24,14 +24,14 @@ pub async fn stream() -> Result<(), DbErr> {
.save(&ctx.db) .save(&ctx.db)
.await?; .await?;
let result = Bakery::find_by_id(bakery.id.clone()) let result = Bakery::find_by_id(bakery.id.clone().unwrap())
.stream(&ctx.db) .stream(&ctx.db)
.await? .await?
.next() .next()
.await .await
.unwrap()?; .unwrap()?;
assert_eq!(result.id, bakery.id); assert_eq!(result.id, bakery.id.unwrap());
ctx.delete().await; ctx.delete().await;