Skip to content

Commit

Permalink
update crud example
Browse files Browse the repository at this point in the history
  • Loading branch information
soulww committed Dec 28, 2023
1 parent a89f8fb commit 977cfa4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 1 addition & 3 deletions examples/crud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
luna-orm = "0.3.2"
#luna-orm-trait = "0.3.1"
#luna-orm-macro = "0.3.1"
luna-orm = "0.3.3"
sqlx = { version = "0.7", features = [ "runtime-tokio" ] }
tokio = { version = "1.35.1", features = ["full"] }
36 changes: 32 additions & 4 deletions examples/crud/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use luna_orm::prelude::*;
use luna_orm::LunaOrmResult;

#[derive(Entity, Clone, Debug)]
#[derive(Schema, Clone, Debug)]
#[TableName = "user"]
pub struct HelloEntity {
pub struct UserEntity {
#[PrimaryKey]
id: i32,
name: String,
Expand All @@ -19,18 +19,46 @@ async fn main() -> LunaOrmResult<()> {

let mut db: DB<SqliteDatabase> = SqliteDatabase::build(config).await.unwrap().into();

let result = db.execute_plain("DROP TABLE IF EXISTS `user`").await?;
let result = db.execute_plain(
"CREATE TABLE IF NOT EXISTS `user`(`id` INT PRIMARY KEY, `age` INT, `name` VARCHAR(64))",
).await?;

let entity = HelloEntity {
// 1. insert entity
let entity = UserEntity {
id: 1,
name: "Allen".to_string(),
age: Some(23),
};

let result = db.insert(&entity).await?;

// 2. update
let mutation = UserMutation {
name: None,
age: Some(24),
};
let primary = UserPrimary { id: 1 };
let result = db.update(&mutation, &primary).await?;

// 3. select
let selection = UserSelection {
id: false,
name: true,
age: true,
};
let entity: Option<UserSelectedEntity> = db.select(&primary, &selection).await?;
let expect_entity = UserSelectedEntity {
id: None,
name: Some("Allen".to_string()),
age: Some(24),
};
assert_eq!(entity, Some(expect_entity));

// 4. delete
let _ = db.delete(&primary).await?;
let entity: Option<UserSelectedEntity> = db.select(&primary, &selection).await?;
assert_eq!(entity, None);

println!("result: {}", result);
Ok(())
}
2 changes: 1 addition & 1 deletion examples/mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
luna-orm = "0.3.2"
luna-orm = "0.3.3"
sqlx = { version = "0.7", features = [ "runtime-tokio" ] }
tokio = { version = "1.35.1", features = ["full"] }
2 changes: 1 addition & 1 deletion examples/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
luna-orm = "0.3.2"
luna-orm = "0.3.3"
sqlx = { version = "0.7", features = [ "runtime-tokio" ] }
tokio = { version = "1.35.1", features = ["full"] }

0 comments on commit 977cfa4

Please sign in to comment.