Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper method ModelTrait::delete #396

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ let banana = banana.save(db).await?;
```
### Delete
```rust
// delete one
let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
let orange: fruit::ActiveModel = orange.unwrap().into();
let orange: fruit::Model = orange.unwrap();
fruit::Entity::delete(orange.into_active_model())
.exec(db)
.await?;

// delete one
fruit::Entity::delete(orange).exec(db).await?;
// or simply
let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
let orange: fruit::Model = orange.unwrap();
orange.delete(db).await?;

// delete many: DELETE FROM "fruit" WHERE "fruit"."name" LIKE 'Orange'
Expand Down
17 changes: 15 additions & 2 deletions src/entity/model.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::{
DbErr, EntityTrait, Linked, QueryFilter, QueryResult, Related, Select, SelectModel,
SelectorRaw, Statement,
ActiveModelBehavior, ActiveModelTrait, ConnectionTrait, DbErr, DeleteResult, EntityTrait,
IntoActiveModel, Linked, QueryFilter, QueryResult, Related, Select, SelectModel, SelectorRaw,
Statement,
};
use async_trait::async_trait;
pub use sea_query::Value;
use std::fmt::Debug;

/// A Trait for a Model
#[async_trait]
pub trait ModelTrait: Clone + Send + Debug {
#[allow(missing_docs)]
type Entity: EntityTrait;
Expand Down Expand Up @@ -33,6 +36,16 @@ pub trait ModelTrait: Clone + Send + Debug {
let tbl_alias = &format!("r{}", l.link().len() - 1);
l.find_linked().belongs_to_tbl_alias(self, tbl_alias)
}

/// Delete an model
async fn delete<'a, A, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>
where
Self: IntoActiveModel<A>,
C: ConnectionTrait<'a>,
A: ActiveModelTrait<Entity = Self::Entity> + ActiveModelBehavior + Send + 'a,
{
self.into_active_model().delete(db).await
}
}

/// A Trait for implementing a [QueryResult]
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,16 @@
//! ```
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
//! // delete one
//! let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
//! let orange: fruit::ActiveModel = orange.unwrap().into();
//! let orange: fruit::Model = orange.unwrap();
//! fruit::Entity::delete(orange.into_active_model())
//! .exec(db)
//! .await?;
//!
//! // delete one
//! fruit::Entity::delete(orange).exec(db).await?;
//! // or simply
//! # let orange: fruit::ActiveModel = Fruit::find_by_id(1).one(db).await.unwrap().unwrap().into();
//! let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
//! let orange: fruit::Model = orange.unwrap();
//! orange.delete(db).await?;
//!
//! // delete many: DELETE FROM "fruit" WHERE "fruit"."name" LIKE 'Orange'
Expand Down
2 changes: 1 addition & 1 deletion tests/active_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
.unwrap()
);

let res = model.into_active_model().delete(db).await?;
let res = model.delete(db).await?;

assert_eq!(res.rows_affected, 1);
assert_eq!(Entity::find().one(db).await?, None);
Expand Down
2 changes: 1 addition & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn crud_cake(db: &DbConn) -> Result<(), DbErr> {
apple
);

let apple: cake::ActiveModel = apple.unwrap().into();
let apple: cake::Model = apple.unwrap();

let result = apple.delete(db).await?;

Expand Down
12 changes: 2 additions & 10 deletions tests/crud/deletes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ pub async fn test_delete_cake(db: &DbConn) {
let cakes = Cake::find().all(db).await.unwrap();
assert_eq!(cakes.len(), initial_cakes + 1);

let _result = cake
.into_active_model()
.delete(db)
.await
.expect("failed to delete cake");
let _result = cake.delete(db).await.expect("failed to delete cake");

let cakes = Cake::find().all(db).await.unwrap();
assert_eq!(cakes.len(), initial_cakes);
Expand All @@ -56,11 +52,7 @@ pub async fn test_delete_bakery(db: &DbConn) {
initial_bakeries + 1
);

let _result = bakery
.into_active_model()
.delete(db)
.await
.expect("failed to delete bakery");
let _result = bakery.delete(db).await.expect("failed to delete bakery");

assert_eq!(
Bakery::find().all(db).await.unwrap().len(),
Expand Down
2 changes: 1 addition & 1 deletion tests/crud/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub async fn test_update_deleted_customer(db: &DbConn) {

let customer_id = customer.id;

let _ = customer.into_active_model().delete(db).await;
let _ = customer.delete(db).await;
assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers);

let customer = customer::ActiveModel {
Expand Down
3 changes: 1 addition & 2 deletions tests/sequential_op_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ pub async fn test_delete_bakery(db: &DatabaseConnection) {
}
.save(db)
.await
.expect("could not insert bakery")
.into_active_model();
.expect("could not insert bakery");

assert_eq!(
Bakery::find().all(db).await.unwrap().len(),
Expand Down
4 changes: 2 additions & 2 deletions tests/transaction_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub async fn transaction_with_active_model_behaviour() -> Result<(), DbErr> {
assert_eq!(cake::Entity::find().all(&txn).await?.len(), 2);

assert_eq!(
readonly_cake_1.into_active_model().delete(&txn).await.err(),
readonly_cake_1.delete(&txn).await.err(),
Some(DbErr::Custom(
"[before_delete] Cannot be deleted".to_owned()
))
Expand All @@ -428,7 +428,7 @@ pub async fn transaction_with_active_model_behaviour() -> Result<(), DbErr> {
assert_eq!(cake::Entity::find().all(&txn).await?.len(), 3);

assert_eq!(
readonly_cake_2.into_active_model().delete(&txn).await.err(),
readonly_cake_2.delete(&txn).await.err(),
Some(DbErr::Custom("[after_delete] Cannot be deleted".to_owned()))
);

Expand Down