-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
187 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ pub enum PersonKind { | |
/// Represents a person in the database | ||
#[entity] | ||
#[upsert_query(table = "person", name = UpsertPerson)] | ||
#[upsert_query(table = "person", name = UpsertPersonWithTTL, ttl)] | ||
pub struct PersonEntity { | ||
/// The id of the person | ||
#[entity(primary_key)] | ||
|
@@ -109,4 +110,54 @@ mod test { | |
|
||
assert_eq!(values, result_values); | ||
} | ||
|
||
#[test] | ||
fn test_upsert_ttl() { | ||
let upsert = UpsertPersonWithTTL { | ||
id: v1_uuid(), | ||
email: MaybeUnset::Set("[email protected]".to_string()), | ||
age: MaybeUnset::Unset, | ||
kind: MaybeUnset::Set(PersonKind::Parent), | ||
data: MaybeUnset::Set(Some(PersonData { | ||
stripe_id: Some("stripe_id".to_string()), | ||
})), | ||
created_at: MaybeUnset::Unset, | ||
|
||
set_ttl: 300, | ||
}; | ||
|
||
let query = <UpsertPersonWithTTL as Query>::query(); | ||
let values = <UpsertPersonWithTTL as Query>::bind(&upsert).unwrap(); | ||
|
||
assert_eq!( | ||
query, | ||
r#"update person using ttl :set_ttl set "email" = :email, "age" = :age, "data" = :data, "kind" = :kind, "createdAt" = :created_at where "id" = :id;"# | ||
); | ||
|
||
let mut result_values = SerializedValues::new(); | ||
result_values | ||
.add_named_value("email", &upsert.email) | ||
.expect("failed to add value"); | ||
result_values | ||
.add_named_value("age", &upsert.age) | ||
.expect("failed to add value"); | ||
result_values | ||
.add_named_value("data", &upsert.data) | ||
.expect("failed to add value"); | ||
result_values | ||
.add_named_value("kind", &upsert.kind) | ||
.expect("failed to add value"); | ||
result_values | ||
.add_named_value("created_at", &upsert.created_at) | ||
.expect("failed to add value"); | ||
result_values | ||
.add_named_value("id", &upsert.id) | ||
.expect("failed to add value"); | ||
|
||
result_values | ||
.add_named_value("set_ttl", &upsert.set_ttl) | ||
.expect("failed to add value"); | ||
|
||
assert_eq!(values, result_values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
//! Example | ||
use example::entities::person::{ | ||
model::{PersonData, PersonKind, UpsertPerson}, | ||
use example::entities::{person::{ | ||
model::{PersonData, PersonKind, UpsertPerson, UpsertPersonWithTTL}, | ||
queries::{DeletePersonById, GetPeopleByIds, GetPersonByEmail, GetPersonById, PersonQueries}, | ||
}; | ||
}, PersonEntity}; | ||
use scyllax::prelude::*; | ||
use scyllax::{executor::create_session, util::v1_uuid}; | ||
use tracing_subscriber::prelude::*; | ||
|
@@ -22,33 +22,18 @@ async fn main() -> anyhow::Result<()> { | |
let session = create_session(known_nodes, default_keyspace).await?; | ||
let executor = Executor::<PersonQueries>::new(session).await?; | ||
|
||
let query = GetPersonByEmail { | ||
email: "[email protected]".to_string(), | ||
}; | ||
let res_one = executor | ||
.execute_read(&query) | ||
.await? | ||
.expect("person not found"); | ||
tracing::info!("GetPersonByEmail returned: {:?}", res_one); | ||
|
||
let query = GetPersonById { id: res_one.id }; | ||
let res_two = executor | ||
.execute_read(&query) | ||
.await? | ||
.expect("person not found"); | ||
tracing::info!("GetPersonById returned: {:?}", res_two); | ||
assert_eq!(res_one, res_two); | ||
let by_email_res = by_email(&executor, "[email protected]".to_string()).await?; | ||
let by_id_res = by_id(&executor, by_email_res.id).await?; | ||
assert_eq!(by_email_res, by_id_res); | ||
|
||
let ids = [ | ||
"e01e84d6-414c-11ee-be56-0242ac120002", | ||
"e01e880a-414c-11ee-be56-0242ac120002", | ||
] | ||
.iter() | ||
.map(|s| Uuid::parse_str(s).unwrap()) | ||
.collect::<Vec<_>>(); | ||
let query = GetPeopleByIds { ids, rowlimit: 10 }; | ||
let res = executor.execute_read(&query).await?; | ||
tracing::info!("GetPeopleByIds returned: {:?}", res); | ||
.iter() | ||
.map(|s| Uuid::parse_str(s).unwrap()) | ||
.collect::<Vec<_>>(); | ||
by_ids(&executor, ids).await?; | ||
|
||
let upsert_id = v1_uuid(); | ||
let query = UpsertPerson { | ||
|
@@ -68,5 +53,54 @@ async fn main() -> anyhow::Result<()> { | |
let res = executor.execute_write(&delete).await?; | ||
tracing::info!("DeletePersonById returned: {:?}", res); | ||
|
||
let upsert_ttl_id = v1_uuid(); | ||
let query = UpsertPersonWithTTL { | ||
id: upsert_ttl_id, | ||
email: "[email protected]".to_string().into(), | ||
age: MaybeUnset::Set(Some(42)), | ||
data: MaybeUnset::Set(Some(PersonData { | ||
stripe_id: Some("stripe_id".to_string()), | ||
})), | ||
kind: MaybeUnset::Set(PersonKind::Parent), | ||
created_at: MaybeUnset::Unset, | ||
|
||
// 5 minutes | ||
set_ttl: 300, | ||
}; | ||
let res = executor.execute_write(&query).await?; | ||
tracing::info!("UpsertPersonWithTTL returned: {:?}", res); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn by_email(executor: &Executor<PersonQueries>, email: String) -> anyhow::Result<PersonEntity> { | ||
let res = executor | ||
.execute_read(&GetPersonByEmail { | ||
}) | ||
.await? | ||
.expect("person not found"); | ||
|
||
tracing::info!("GetPersonByEmail returned: {:?}", res); | ||
|
||
Ok(res) | ||
} | ||
|
||
async fn by_id(executor: &Executor<PersonQueries>, id: Uuid) -> anyhow::Result<PersonEntity> { | ||
let res = executor | ||
.execute_read(&GetPersonById { id }) | ||
.await? | ||
.expect("person not found"); | ||
|
||
tracing::info!("GetPersonById returned: {:?}", res); | ||
|
||
Ok(res) | ||
} | ||
|
||
async fn by_ids(executor: &Executor<PersonQueries>, ids: Vec<Uuid>) -> anyhow::Result<Vec<PersonEntity>> { | ||
let res = executor.execute_read(&GetPeopleByIds { ids, rowlimit: 10 }).await?; | ||
|
||
tracing::info!("GetPeopleByIds returned: {:?}", res); | ||
|
||
Ok(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters