Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.83k stars 108 forks source link

Order by joined values #269

Closed Giftzwerg02 closed 1 year ago

Giftzwerg02 commented 1 year ago

I am having trouble ordering a selection by values from joined tables, it looks something like the following:

let fetched = client
     .a()
     .with(b::fetch()) // b might also join with c, etc.
     .order_by(/* some field from b, c, etc. */ );
     // ...

Digging through the generated code it seems to me that I can only provide columns directly from a and not from the joined values (also not by doing a::b::/*some_value*/. I also haven't found any examples or hints in the documentation. Is there any way to get around this issue?

Brendonovich commented 1 year ago

It looks like this is possible, maybe it could look something like this?

let fetched = client
     .a()
     .with(b::fetch())
     .order_by(a::b::order(b::c::order(Direction::Asc)));

https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#sort-by-relation

Giftzwerg02 commented 1 year ago

What would be the input type of the order function then? Since it currently takes a Direction but returns an OrderByParam (which is different per table).

Brendonovich commented 1 year ago

Since it currently takes a Direction but returns an OrderByParam

That's only true for scalar fields, but we're talking about relation fields here. The relation field order functions would take some sort of enum specific to that relation.

Brendonovich commented 1 year ago

here we go!

image