Simplify example

This commit is contained in:
Chris Tsang 2021-06-06 18:30:59 +08:00
parent 989845f422
commit 5e0176ef1a
9 changed files with 34 additions and 36 deletions

View File

@ -50,7 +50,7 @@ Model { id: 1, name: "New York Cheese" }
find one by like: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%chocolate%' LIMIT 1
Model { id: 2, name: "Chocolate Forest" }
Some(Model { id: 2, name: "Chocolate Forest" })
find models belong to: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id` WHERE `cake`.`id` = 1

View File

@ -0,0 +1,4 @@
pub use super::cake::Entity as Cake;
pub use super::cake_filling::Entity as CakeFilling;
pub use super::filling::Entity as Filling;
pub use super::fruit::Entity as Fruit;

View File

@ -79,6 +79,4 @@ impl Model {
}
}
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -59,6 +59,4 @@ impl RelationTrait for Relation {
}
}
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -62,6 +62,4 @@ impl Model {
}
}
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -49,6 +49,4 @@ impl RelationTrait for Relation {
}
}
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -1,5 +1,6 @@
use sea_orm::Database;
mod entities;
mod example_cake;
mod example_cake_filling;
mod example_filling;
@ -7,6 +8,7 @@ mod example_fruit;
mod operation;
mod select;
use entities::*;
use example_cake as cake;
use example_cake_filling as cake_filling;
use example_filling as filling;

View File

@ -1,5 +1,5 @@
use crate::*;
use sea_orm::{entity::*, query::*, Database};
use super::*;
pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> {
insert_and_update(db).await?;
@ -20,12 +20,12 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
name: Val::set("pear".to_owned()),
..Default::default()
};
let res = fruit::Entity::insert(pear).exec(db).await?;
let res = Fruit::insert(pear).exec(db).await?;
println!();
println!("Inserted: {:?}\n", res);
let pear = fruit::Entity::find_by(res.last_insert_id)
let pear = Fruit::find_by(res.last_insert_id)
.one(db)
.await
.map_err(|_| ExecErr)?;
@ -36,7 +36,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
let mut pear: fruit::ActiveModel = pear.unwrap().into();
pear.name = Val::set("Sweet pear".to_owned());
let res = fruit::Entity::update(pear).exec(db).await?;
let res = Fruit::update(pear).exec(db).await?;
println!();
println!("Updated: {:?}\n", res);

View File

@ -1,5 +1,5 @@
use crate::*;
use sea_orm::{entity::*, query::*, Database, FromQueryResult};
use super::*;
pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> {
find_all(db).await?;
@ -32,7 +32,7 @@ pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> {
async fn find_all(db: &Database) -> Result<(), QueryErr> {
print!("find all cakes: ");
let cakes = cake::Entity::find().all(db).await?;
let cakes = Cake::find().all(db).await?;
println!();
for cc in cakes.iter() {
@ -41,7 +41,7 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> {
print!("find all fruits: ");
let fruits = fruit::Entity::find().all(db).await?;
let fruits = Fruit::find().all(db).await?;
println!();
for ff in fruits.iter() {
@ -54,8 +54,8 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> {
async fn find_together(db: &Database) -> Result<(), QueryErr> {
print!("find cakes and fruits: ");
let both = cake::Entity::find()
.left_join_and_select(fruit::Entity)
let both = Cake::find()
.left_join_and_select(Fruit)
.all(db)
.await?;
@ -70,7 +70,7 @@ async fn find_together(db: &Database) -> Result<(), QueryErr> {
async fn find_one(db: &Database) -> Result<(), QueryErr> {
print!("find one by primary key: ");
let cheese: Option<cake::Model> = cake::Entity::find_by(1).one(db).await?;
let cheese: Option<cake::Model> = Cake::find_by(1).one(db).await?;
let cheese = cheese.unwrap();
println!();
@ -79,7 +79,7 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> {
print!("find one by like: ");
let chocolate = cake::Entity::find()
let chocolate = Cake::find()
.filter(cake::Column::Name.contains("chocolate"))
.one(db)
.await?;
@ -109,8 +109,8 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
print!("count fruits by cake: ");
let select = cake::Entity::find()
.left_join(fruit::Entity)
let select = Cake::find()
.left_join(Fruit)
.select_only()
.column(cake::Column::Name)
.column_as(fruit::Column::Id.count(), "num_of_fruits")
@ -129,8 +129,8 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
print!("find cakes and fillings: ");
let both = cake::Entity::find()
.left_join_and_select(filling::Entity)
let both = Cake::find()
.left_join_and_select(Filling)
.all(db)
.await?;
@ -141,7 +141,7 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
print!("find fillings for cheese cake: ");
let cheese = cake::Entity::find_by(1).one(db).await?;
let cheese = Cake::find_by(1).one(db).await?;
if let Some(cheese) = cheese {
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
@ -154,7 +154,7 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
print!("find cakes for lemon: ");
let lemon = filling::Entity::find_by(2).one(db).await?;
let lemon = Filling::find_by(2).one(db).await?;
if let Some(lemon) = lemon {
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
@ -185,13 +185,13 @@ async fn all_about_select_json(db: &Database) -> Result<(), QueryErr> {
async fn find_all_json(db: &Database) -> Result<(), QueryErr> {
print!("find all cakes: ");
let cakes = cake::Entity::find().into_json().all(db).await?;
let cakes = Cake::find().into_json().all(db).await?;
println!("\n{}\n", serde_json::to_string_pretty(&cakes).unwrap());
print!("find all fruits: ");
let fruits = fruit::Entity::find().into_json().all(db).await?;
let fruits = Fruit::find().into_json().all(db).await?;
println!("\n{}\n", serde_json::to_string_pretty(&fruits).unwrap());
@ -201,8 +201,8 @@ async fn find_all_json(db: &Database) -> Result<(), QueryErr> {
async fn find_together_json(db: &Database) -> Result<(), QueryErr> {
print!("find cakes and fruits: ");
let cakes_fruits = cake::Entity::find()
.left_join_and_select(fruit::Entity)
let cakes_fruits = Cake::find()
.left_join_and_select(Fruit)
.into_json()
.all(db)
.await?;
@ -218,8 +218,8 @@ async fn find_together_json(db: &Database) -> Result<(), QueryErr> {
async fn count_fruits_by_cake_json(db: &Database) -> Result<(), QueryErr> {
print!("count fruits by cake: ");
let count = cake::Entity::find()
.left_join(fruit::Entity)
let count = Cake::find()
.left_join(Fruit)
.select_only()
.column(cake::Column::Name)
.column_as(fruit::Column::Id.count(), "num_of_fruits")