Luis Kuper
10/26/2021, 1:19 PMdistinct
& cursor-pagination
.
TLDR: How can i specify that distinct is applied before the cursor pagination steps (cursorId, skip, take) and not afterwards.
Example:
Having the following Model:
model Comments {
id String @id @default(uuid())
author User? @relation(fields: [authorId], references: [id])
authorId String?
cursorId String @unique
text String
}
and having that model filled with the following data:
{
authorId: "user1",
cursorId: "0001",
text: "text1",
},
{
authorId: "user2",
cursorId: "0002",
text: "text2",
},
{
authorId: "user3",
cursorId: "0003",
text: "text3",
},
{
authorId: "user4",
cursorId: "0004",
text: "text4",
},
{
authorId: "user1", // sidenote: this is the 2nd comment linked to user1
cursorId: "0005",
text: "text5",
}
The objective is to fetch the comments in a cursor-paginated way and recieving items that are distinct on the field authorId.
Considering 3 items per page, leads to the following query for the 2nd page:
const res = await prismaClient.blogPost.findMany({
distinct: ["authorId"],
orderBy: { cursorId: "asc" },
cursor: { cursorId: "0003" },
skip: 1,
take: 3,
});
This leads to the following res where the 2nd item is fetched although unfortunately in the first cursor-pagination fetch a item with that authorId "user1" has been fetched already before.
{
authorId: "user4",
cursorId: "0004",
text: "text4",
}
{
authorId: "user1",
cursorId: "0005",
text: "text5",
}
What i would like to have on that 2nd fetch is just the object with cursorId "0004".
I assume this could be solved by the possibility of distinct being applied before the cursor pagination. Is there the possibility to do so?
Thanks in advice.