nodecosmos / charybdis

Rust ORM for ScyllaDB and Apache Cassandra
MIT License
108 stars 6 forks source link

Find macro for materialised views #30

Closed Zk2u closed 2 months ago

Zk2u commented 2 months ago

Hi,

I have a PurchasesByChef MV. I want to query based on a date < ... but can't do so like with normal tables. Can this be implemented? date is a cluster key on both Purchases and the MV.

However, there is a find_purchases_by_chef_query macro but I'm not quite sure how to use these.

GoranBrkuljan commented 2 months ago

You need to provide partition key of the MV itself. Same as on original table. E.g. if you have table:

#[charybdis_model(
    table_name = posts,
    partition_keys = [date],
    clustering_keys = [category_id, title]
)]
pub struct Post {
    pub date: Date,
    pub category_id: Uuid,
    pub title: Text,
    pub user_id: Uuid,
}

And you want to create MV posts_by_user, you would do:

#[charybdis_view_model(
    table_name=posts_by_user,
    base_table=posts,
    partition_keys=[user_id],
    clustering_keys=[date, category_id, title]
)]
pub struct PostByUser {
    pub date: Date,
    pub category_id: Uuid,
    pub title: Text,
    pub user_id: Uuid,,
}

Now you would be able to query by find_by_user_id, find_by_user_id_and_date ...

Zk2u commented 2 months ago

Now you would be able to query by find_by_user_id, find_by_user_id_and_date ...

Do you mean that it adds these methods to the original Post struct?

I'm trying to do date < ? rather than date = ? which the regular find... doesn't work with (I think?).

Can I do this or am I using the find wrong?

PurchasesByChef::find(
      "chef = ? and date < ? and id < ?",
      (account_id, date, id),
  )
  .execute(&db)
  .await
GoranBrkuljan commented 2 months ago

Sorry I see now, you need to do following:

PurchasesByChef::find(
      find_purchases_by_chef_query!("chef = ? and date < ?"),
      (account_id, date),
  )
  .execute(&db)
  .await

find_purchases_by_chef_query! will build complete query as static str at compile time.

Zk2u commented 2 months ago

Awesome, thank you!