diff --git a/examples/crud/Cargo.toml b/examples/crud/Cargo.toml index 55e0d8a..a3e7c78 100644 --- a/examples/crud/Cargo.toml +++ b/examples/crud/Cargo.toml @@ -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"] } diff --git a/examples/crud/src/main.rs b/examples/crud/src/main.rs index ea35d1c..bb6fa85 100644 --- a/examples/crud/src/main.rs +++ b/examples/crud/src/main.rs @@ -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, @@ -19,18 +19,46 @@ async fn main() -> LunaOrmResult<()> { let mut db: DB = 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 = 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 = db.select(&primary, &selection).await?; + assert_eq!(entity, None); + println!("result: {}", result); Ok(()) } diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index eda9d9e..5e3d838 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -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"] } diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index 814e933..362aaba 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -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"] }