Brendonovich / prisma-client-rust

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

Ordering by count #253

Closed gBasil closed 1 year ago

gBasil commented 1 year ago

Is it currently possible to do something like this?

const orderedPosts = await prisma.artist.findMany({
  orderBy: {
    songs: {
      count: 'asc'
    }
  }
});

My actual use-case is for seeing how many scrobbles a certain song has, and it would have another level of ordering―ordering the artists based on the total count of scrobbles across all of their songs.

Thank you for your time!

Brendonovich commented 1 year ago

Not currently, but this is definitely something I want to support. It's a bit long winded, but this could work?

artist::songs::count::order(Direction::Asc)

It may have to be _count for naming collision reasons but I'll see.

gBasil commented 1 year ago

Yeah, that would work for single "layers." But what about if you have a schema like this:

model Artist {
    id        Int        @id @default(autoincrement())
    name      String
    songs     Song[]
}

model Song {
    id        Int        @id @default(autoincrement())
    name      String
    artists   Artist[]
    scrobbles Scrobble[]
}

model Scrobble {
    id       Int      @id @default(autoincrement())
    song     Song     @relation(fields: [songId], references: [id])
    songId   Int
}

How would you sort the artists based on the number of scrobbles?

Brendonovich commented 1 year ago

Depends how you'd do it in TS, idk if Prisma supports that

gBasil commented 1 year ago

True, totally forgot.

Brendonovich commented 1 year ago

Just got this working with the following syntax:

user::posts::order(vec![
    post::_count::order(SortOrder::Asc)
])

I'm conflicted on whether I should spend the heap of effort it would take to make it like this:

user::posts::_count::order(SortOrder::Asc)

May be able to at a later update though.

gBasil commented 1 year ago

That's wonderful, thank you so much!