From e1fa9938366c893de52484892cc7f3fddc7388eb Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 27 Jun 2023 16:06:11 +0800 Subject: [PATCH] API DEMO: `find_with_linked` --- src/query/join.rs | 9 +++++ tests/relational_tests.rs | 77 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/query/join.rs b/src/query/join.rs index 09f2e3857..c57b50c40 100644 --- a/src/query/join.rs +++ b/src/query/join.rs @@ -107,6 +107,15 @@ where } select_two } + + pub fn find_with_linked(self, l: L) -> SelectTwoMany + where + L: Linked, + T: EntityTrait, + { + // TODO: Should construct the select and join expression below + self.select_with(T::default()) + } } #[cfg(test)] diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index 959f05577..2527a81b7 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -2,6 +2,7 @@ pub mod common; pub use chrono::offset::Utc; pub use common::{bakery_chain::*, setup::*, TestContext}; +use pretty_assertions::assert_eq; pub use rust_decimal::prelude::*; pub use rust_decimal_macros::dec; use sea_orm::{entity::*, query::*, DbErr, DerivePartialModel, FromQueryResult}; @@ -747,6 +748,82 @@ pub async fn linked() -> Result<(), DbErr> { }] ); + let select_baker_with_customer = Baker::find().find_with_linked(baker::BakedForCustomer); + + assert_eq!( + select_baker_with_customer + .build(sea_orm::DatabaseBackend::MySql) + .to_string(), + [ + // FIXME: This might be faulty! + "SELECT `baker`.`id` AS `A_id`,", + "`baker`.`name` AS `A_name`,", + "`baker`.`contact_details` AS `A_contact_details`,", + "`baker`.`bakery_id` AS `A_bakery_id`,", + "`r4`.`id` AS `B_id`,", + "`r4`.`name` AS `B_name`,", + "`r4`.`notes` AS `B_notes`", + "FROM `baker`", + "INNER JOIN `cakes_bakers` AS `r0` ON `r0`.`baker_id` = `baker`.`id`", + "INNER JOIN `cake` AS `r1` ON `r1`.`id` = `r0`.`cake_id`", + "INNER JOIN `lineitem` AS `r2` ON `r2`.`cake_id` = `r1`.`id`", + "INNER JOIN `order` AS `r3` ON `r3`.`id` = `r2`.`order_id`", + "INNER JOIN `customer` AS `r4` ON `r4`.`id` = `r3`.`customer_id`", + "ORDER BY `baker`.`id` ASC", + ] + .join(" ") + ); + + assert_eq!( + select_baker_with_customer.all(&ctx.db).await?, + [ + ( + baker::Model { + id: 1, + name: "Baker Bob".into(), + contact_details: serde_json::json!({ + "mobile": "+61424000000", + "home": "0395555555", + "address": "12 Test St, Testville, Vic, Australia", + }), + bakery_id: Some(1), + }, + vec![customer::Model { + id: 2, + name: "Kara".into(), + notes: Some("Loves all cakes".into()), + }] + ), + ( + baker::Model { + id: 1, + name: "Baker Bobby".into(), + contact_details: serde_json::json!({ + "mobile": "+85212345678", + }), + bakery_id: Some(1), + }, + vec![ + customer::Model { + id: 1, + name: "Kate".into(), + notes: Some("Loves cheese cake".into()), + }, + customer::Model { + id: 1, + name: "Kate".into(), + notes: Some("Loves cheese cake".into()), + }, + customer::Model { + id: 2, + name: "Kara".into(), + notes: Some("Loves all cakes".into()), + }, + ] + ), + ] + ); + ctx.delete().await; Ok(())