Refactor example
This commit is contained in:
parent
ca459e693b
commit
e30ce4932b
@ -1,4 +1,4 @@
|
|||||||
use sea_orm::{Database, EntityTrait};
|
use sea_orm::{Database, EntityTrait, QueryErr};
|
||||||
|
|
||||||
mod example_cake;
|
mod example_cake;
|
||||||
mod example_fruit;
|
mod example_fruit;
|
||||||
@ -9,15 +9,25 @@ use example_fruit as fruit;
|
|||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut db = Database::default();
|
let mut db = Database::default();
|
||||||
|
|
||||||
db.connect("mysql://sea:sea@localhost/bakery")
|
db.connect("mysql://sea:sea@localhost/bakery")
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
println!("{:?}", db);
|
println!("{:?}", db);
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
|
find_all(&db).await.unwrap();
|
||||||
|
|
||||||
|
find_one(&db).await.unwrap();
|
||||||
|
|
||||||
|
count_fruits_by_cake(&db).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_all(db: &Database) -> Result<(), QueryErr> {
|
||||||
print!("find all cakes: ");
|
print!("find all cakes: ");
|
||||||
|
|
||||||
let cakes = cake::Entity::find().all(&db).await.unwrap();
|
let cakes = cake::Entity::find().all(db).await?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for cc in cakes.iter() {
|
for cc in cakes.iter() {
|
||||||
@ -27,25 +37,7 @@ async fn main() {
|
|||||||
|
|
||||||
print!("find all fruits: ");
|
print!("find all fruits: ");
|
||||||
|
|
||||||
let fruits = fruit::Entity::find().all(&db).await.unwrap();
|
let fruits = fruit::Entity::find().all(db).await?;
|
||||||
|
|
||||||
println!();
|
|
||||||
for cc in fruits.iter() {
|
|
||||||
println!("{:?}", cc);
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
|
|
||||||
print!("find one by primary key: ");
|
|
||||||
|
|
||||||
let cheese = cake::Entity::find_by(1).one(&db).await.unwrap();
|
|
||||||
|
|
||||||
println!();
|
|
||||||
println!("{:?}", cheese);
|
|
||||||
println!();
|
|
||||||
|
|
||||||
print!("find models belong to: ");
|
|
||||||
|
|
||||||
let fruits = cheese.find_fruit().all(&db).await.unwrap();
|
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for ff in fruits.iter() {
|
for ff in fruits.iter() {
|
||||||
@ -53,10 +45,32 @@ async fn main() {
|
|||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
count_fruits_by_cake(&db).await;
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn count_fruits_by_cake(db: &Database) {
|
async fn find_one(db: &Database) -> Result<(), QueryErr> {
|
||||||
|
print!("find one by primary key: ");
|
||||||
|
|
||||||
|
let cheese = cake::Entity::find_by(1).one(db).await?;
|
||||||
|
|
||||||
|
println!();
|
||||||
|
println!("{:?}", cheese);
|
||||||
|
println!();
|
||||||
|
|
||||||
|
print!("find models belong to: ");
|
||||||
|
|
||||||
|
let fruits = cheese.find_fruit().all(db).await?;
|
||||||
|
|
||||||
|
println!();
|
||||||
|
for ff in fruits.iter() {
|
||||||
|
println!("{:?}", ff);
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct SelectResult {
|
struct SelectResult {
|
||||||
name: String,
|
name: String,
|
||||||
@ -96,11 +110,13 @@ async fn count_fruits_by_cake(db: &Database) {
|
|||||||
.group_by_col((cake::Entity, Cake::Name));
|
.group_by_col((cake::Entity, Cake::Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
let results = select.into_model::<SelectResult>().all(db).await.unwrap();
|
let results = select.into_model::<SelectResult>().all(db).await?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for rr in results.iter() {
|
for rr in results.iter() {
|
||||||
println!("{:?}", rr);
|
println!("{:?}", rr);
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user