jeremychone / rust-modql

Rust implementation for Model Query Language support
Apache License 2.0
30 stars 7 forks source link

Are you considering adding support for uuid? #1

Closed fan-tastic-z closed 7 months ago

fan-tastic-z commented 1 year ago

If the filtered field is uuid, the condtion built through filter.try_into() cannot support uuid

fan-tastic-z commented 11 months ago
#[derive(FilterNodes, Deserialize, Default, Debug)]
pub struct TaskFilter {
    #[modql(to_sea_value_fn = "uuid_to_sea_value")]
    id: Option<OpValsValue>,
    #[modql(to_sea_value_fn = "uuid_to_sea_value")]
    project_id: Option<OpValsValue>,
    title: Option<OpValsString>,
    done: Option<OpValsBool>,

    #[modql(to_sea_value_fn = "uuid_to_sea_value")]
    cid: Option<OpValsValue>,
    #[modql(to_sea_value_fn = "time_to_sea_value")]
    ctime: Option<OpValsValue>,
    #[modql(to_sea_value_fn = "uuid_to_sea_value")]
    mid: Option<OpValsValue>,
    #[modql(to_sea_value_fn = "time_to_sea_value")]
    mtime: Option<OpValsValue>,
}

if id and project_id need to be uuid, Is it also possible to achieve this by defining uuid_to_sea_value for conversion?

pub fn uuid_to_sea_value(
    json_value: serde_json::Value,
) -> modql::filter::SeaResult<sea_query::Value> {
    let ret: Uuid = serde_json::from_value(json_value)?;
    Ok(ret.into())
}
Ali-Javanmardi commented 8 months ago

@jeremychone thanks for all your training and code you shared.

I have same question about uuid as id field. I hope there is a plan to add uuid soon.

mcamara commented 7 months ago

@fan-tastic-z @Ali-Javanmardi from version 0.3.7 you can use cast_as in Filters to cast a field to uuid. For example:

#[derive(Debug, Default, Deserialize, FilterNodes)]
pub struct PostFilter {
    #[modql(cast_as = "uuid")]
    pub user_id: Option<OpValsString>,
}

Thanks @jeremychone for your hard work!

jeremychone commented 7 months ago

All, sorry for the delay, and thanks for the kind words and encouragement.

So, a few things:

1) OpValsValue with to_sea_value_fn allows you to have full control over how to get into the sea-query value. (as mentioned by @fan-tastic-z)

2) OpValsString with cast_as as mentioned by @mcamara is another way to do this when the database has a primitive type to internal type scheme, which is the case for Postgres with UUID.

Eventually, I would like to support custom or extension type with the with-... feature flag. So, we might have something like OpsValExt<Uuid> or OpValsUuid with the with-uuid feature flag. But I wanted to provide the flexibility above first, to make sure we could have a type that was not included in modql.

By the way, I have been working on the modql 4.0.0, which now has the with-sea-query feature flag, and therefore makes sea-query optional. And some of the Fields capabilities are now sea-query free.

Feel free to jump in the discord: https://discord.gg/W2besKCzjx