nodecosmos / charybdis

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

Missing find by secondary index #19

Closed Zk2u closed 3 months ago

Zk2u commented 3 months ago

Hi, not sure what's going on here but it looks like in this specific struct that the macros aren't generating the expected find_by_email associated function.

#[charybdis_model(
    table_name = chefs,
    partition_keys = [id],
    clustering_keys = [],
    global_secondary_indexes = [email],
    local_secondary_indexes = [],
    static_columns = []
)]
#[derive(Default)]
pub struct Chef {
    pub id: Uuid,
    pub email: Text,
    pub completed_signup: bool,
    pub first_name: Text,
    pub last_name: Text,
}
GoranBrkuljan commented 3 months ago

It's not implemented for global secondary indexes yet. It's implemented for local ones.

For now you can use custom find macro (find_first_chef in this case):

impl Chef {
    pub async fn find_by_email(db_session: &CachingSession, email: &String) -> Result<Chef, YourAppError> {
        find_first_chef!("email = ?", (email,))
            .execute(db_session)
            .await
            .map_err(YourAppError::from)
    }
}

I will implement associated macro generated funs for GSI in the next few days, so let's leave this issue open.

GoranBrkuljan commented 3 months ago

updated with release: https://github.com/nodecosmos/charybdis/releases/tag/v0.4.16

Zk2u commented 3 months ago
#[charybdis_model(
    table_name = emailed_codes,
    partition_keys = [email, emailed_code],
    clustering_keys = [],
    global_secondary_indexes = [email],
    local_secondary_indexes = [],
    static_columns = [],
    table_options = "default_time_to_live = 120;"
)]
#[derive(Default)]
pub struct EmailedCode {
    /// Email of the account that requested the challenge
    pub email: Text,
    /// Server emails an 8 character code to the user's email
    pub emailed_code: Text,
}

This is now failing:

error[E0592]: duplicate definitions with name `maybe_find_first_by_email`
  --> src/models/auth.rs:19:1
   |
19 | / #[charybdis_model(
20 | |     table_name = emailed_codes,
21 | |     partition_keys = [email, emailed_code],
22 | |     clustering_keys = [],
...  |
26 | |     table_options = "default_time_to_live = 120;"
27 | | )]
   | |  ^
   | |  |
   | |__duplicate definitions for `maybe_find_first_by_email`
   |    other definition for `maybe_find_first_by_email`
   |
   = note: this error originates in the attribute macro `charybdis_model` (in Nightly builds, run with -Z macro-backtrace for more info)

edit: i don't think i need to have a separate index for email?

GoranBrkuljan commented 3 months ago

edit: i don't think i need to have a separate index for email?

Probably it would be best if you move emailed_code to clustering keys, remove email from GSI , and then use find_first_by_email or maybe_find_first_by_email. We can not query by partial partition key, so we need to prevent this scenario in lib. In prev versions this was handled, but looks like it's removed after refactoring to use traits. Now I will restore it.

GoranBrkuljan commented 3 months ago

Fixed this as well in v0.4.17

Zk2u commented 3 months ago

Thanks, yes that makes sense. Then we can quickly view all codes sent to an email if needed. Have modified and am now using EmailedCode::maybe_find_first_by_email_and_emailed_code for quickly matching.