Skip to content

Commit

Permalink
Merge pull request #73 from nodecosmos/GB_add_find_rule_to_mv
Browse files Browse the repository at this point in the history
update: add find_model_rule helper to materialized views
  • Loading branch information
GoranBrkuljan authored Jan 29, 2025
2 parents 06b8624 + e40b7f2 commit d49ca60
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
4 changes: 2 additions & 2 deletions charybdis-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea

// Current model rules
let find_model_query_rule = find_model_query_rule(struct_name, &args, fields);
let find_model_rule = find_model_rule(struct_name, &args, fields);

// Associated functions
let find_by_primary_keys_functions = find_by_primary_keys_functions(struct_name, &args, fields);
Expand All @@ -195,8 +196,6 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea
CharybdisFields::strip_charybdis_attributes(&mut input);

let expanded = quote! {
use charybdis::scylla::serialize::SerializationError;

#[derive(charybdis::macros::scylla::SerializeRow)]
#[derive(charybdis::macros::scylla::DeserializeRow)]
#input
Expand Down Expand Up @@ -225,6 +224,7 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea
impl charybdis::model::MaterializedView for #struct_name {}

#find_model_query_rule
#find_model_rule
};

TokenStream::from(expanded)
Expand Down
58 changes: 51 additions & 7 deletions charybdis/tests/integrations/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use charybdis::stream::CharybdisModelStream;
use charybdis::types::{Boolean, Int, Text, Uuid};
use charybdis_macros::{charybdis_model, charybdis_udt_model, charybdis_view_model};

pub const SAMPLE_MODEL_COUNT: usize = 32;

#[derive(Debug, Default, Clone, PartialEq)]
#[charybdis_udt_model(type_name = address)]
pub struct Address {
Expand Down Expand Up @@ -46,7 +48,7 @@ partial_user!(UpdateUsernameUser, id, username);
impl User {
pub async fn populate_sample_users() {
let db_session = db_session().await;
let users = (0..32)
let users = (0..SAMPLE_MODEL_COUNT)
.map(|i| {
let id = Uuid::new_v4();
let mut new_user = User::homer(id);
Expand Down Expand Up @@ -157,15 +159,15 @@ pub struct Post {
}

impl Post {
pub async fn populate_sample_posts_per_partition(category_id: Uuid) {
pub async fn populate_sample_posts_per_partition(category_id: Uuid, author_id: Option<Uuid>) {
let db_session = db_session().await;
let posts = (0..32)
let posts = (0..SAMPLE_MODEL_COUNT)
.map(|i| Post {
category_id,
order_idx: i,
order_idx: i as Int,
title: format!("Post {}", i),
content: "Lorem ipsum dolor sit amet".to_string(),
author_id: Uuid::new_v4(),
author_id: author_id.unwrap_or_else(Uuid::new_v4),
})
.collect::<Vec<Post>>();

Expand Down Expand Up @@ -227,10 +229,10 @@ async fn find_various() -> Result<(), CharybdisError> {
let category_id = Uuid::new_v4();
let db_session = &db_session().await;

Post::populate_sample_posts_per_partition(category_id).await;
Post::populate_sample_posts_per_partition(category_id, None).await;

let posts: CharybdisModelStream<Post> = Post::find_by_category_id(category_id).execute(db_session).await?;
assert_eq!(posts.try_collect().await?.len(), 32);
assert_eq!(posts.try_collect().await?.len(), SAMPLE_MODEL_COUNT);

let posts: CharybdisModelStream<Post> = Post::find_by_category_id_and_order_idx(category_id, 1)
.execute(db_session)
Expand Down Expand Up @@ -299,3 +301,45 @@ async fn find_various() -> Result<(), CharybdisError> {

Ok(())
}

#[charybdis_view_model(
table_name=posts_by_author,
base_table = posts,
partition_keys = [author_id],
clustering_keys = [category_id, order_idx, title],
)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct PostByAuthor {
pub category_id: Uuid,
pub order_idx: Int,
pub title: Text,
pub content: Text,
pub author_id: Uuid,
}

#[tokio::test]
async fn post_by_author_find() -> Result<(), CharybdisError> {
let category_id = Uuid::new_v4();
let author_id = Uuid::new_v4();
let db_session = &db_session().await;

Post::populate_sample_posts_per_partition(category_id, Some(author_id)).await;

let posts_by_author = PostByAuthor::find_by_author_id(author_id)
.execute(db_session)
.await?
.try_collect()
.await?;

assert_eq!(posts_by_author.len(), SAMPLE_MODEL_COUNT);

let posts_by_author_with_limit = find_post_by_author!("author_id = ? LIMIT 10", (author_id,))
.execute(db_session)
.await?
.try_collect()
.await?;

assert_eq!(posts_by_author_with_limit.len(), 10);

Ok(())
}
6 changes: 3 additions & 3 deletions charybdis/tests/integrations/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::db_session;
use crate::custom_fields::AddressTypeCustomField;
use crate::model::{Post, User};
use crate::model::{Post, User, SAMPLE_MODEL_COUNT};
use charybdis::batch::ModelBatch;
use charybdis::errors::CharybdisError;
use charybdis::operations::{Delete, Find, Insert, Update};
Expand Down Expand Up @@ -97,7 +97,7 @@ async fn model_stream() {
.expect("Failed to find users");
let users_vec = users.try_collect().await.expect("Failed to collect users");

assert_eq!(users_vec.len(), 32);
assert_eq!(users_vec.len(), SAMPLE_MODEL_COUNT);

User::delete_batch()
.chunked_delete(&db_session, &users_vec, 100)
Expand All @@ -110,7 +110,7 @@ async fn model_paged() {
let db_session = db_session().await;
let category_id = uuid::Uuid::new_v4();

Post::populate_sample_posts_per_partition(category_id).await;
Post::populate_sample_posts_per_partition(category_id, None).await;

let (posts, paging_state_response) = Post::find_by_partition_key_value_paged((category_id,))
.page_size(3)
Expand Down

0 comments on commit d49ca60

Please sign in to comment.