Kent C. Dodds
07/28/2021, 10:32 PMconst result = await prisma.postRead.groupBy({
by: ['postSlug'],
orderBy: { postSlug: 'asc', },
_count: true,
take: limit,
})
EDIT AGAIN: I think this won't work with a limit because we're not ordering by the count, but by the postSlug. Anyone know if it's possible to order by the slug? Otherwise I have to get all the data and then limit it.Dominic Hadfield
07/29/2021, 8:09 AMThomas Ladd
07/30/2021, 2:21 PMKent C. Dodds
07/30/2021, 2:25 PMKent C. Dodds
07/30/2021, 6:43 PMasync function getMostPopularPostSlugs({
limit,
exclude,
}: {
limit: number
exclude: Array<string>
}) {
const result = await prisma.postRead.groupBy({
by: ['postSlug'],
_count: true,
orderBy: {
_count: {
postSlug: 'desc',
},
},
where: {
postSlug: {notIn: exclude},
},
take: limit,
})
return result.map(({postSlug}) => postSlug)
}
Kent C. Dodds
07/30/2021, 6:44 PMasync function getMostPopularPostSlugs({
limit,
exclude,
}: {
limit: number
exclude: Array<string>
}) {
const result = await prisma.postRead.groupBy({
by: ['postSlug'],
orderBy: {postSlug: 'asc'},
_count: true,
})
const filtered = result.filter(({postSlug}) => !exclude.includes(postSlug))
const sorted = sortBy(filtered, i => -i._count)
return sorted.slice(0, limit).map(({postSlug}) => postSlug)
}
This feels so much better!Thomas Ladd
07/30/2021, 6:45 PMDominic Hadfield
07/30/2021, 6:45 PM