Timo
11/22/2021, 9:20 AMorderBy = [
{
timestamp: 'desc',
},
{
score: {value: 'desc'},
},
]
When I set timestamp first, it just orders by timestamp it seems. And when I set score first, it shows the oldest with the highest score. But I want the newest ones with the highest scores. How do I do that?Ryan
11/22/2021, 10:35 AMTimo
11/22/2021, 2:03 PMmodel Item {
id String @id @default(cuid()) @db.VarChar(80)
timestamp DateTime @default(now())
score Score?
}
model Score {
id String @id @default(cuid()) @db.VarChar(80)
value Float @default(0)
}
Timo
11/22/2021, 2:04 PMScore
could be something like likes
, shares
, views
, etcTimo
11/22/2021, 2:04 PMRyan
11/22/2021, 3:22 PMorderBy
and check for the timestamps?Maciek K
11/22/2021, 4:25 PMRyan
11/23/2021, 5:17 AMTimo
11/23/2021, 4:31 PMtimestamp: "2021-11-21T20:21:53.635Z"
value: 0
timestamp: "2021-11-20T15:56:46.861Z"
value: 0
timestamp: "2021-11-17T10:34:34.696Z"
value: 500
timestamp: "2021-11-12T17:44:04.251Z"
value: 50000
timestamp: "2021-11-08T09:18:05.407Z"
value: 0
Timo
11/23/2021, 4:31 PM[ { timestamp: 'desc' }, { score: { value: 'desc' } } ]
Maciek K
11/23/2021, 4:39 PMtimestamp: "2021-11-21T20:21:59.635Z"
value: 300
timestamp: "2021-11-21T20:21:53.635Z" <-- same timestamp
value: 5000
timestamp: "2021-11-21T20:21:53.635Z" <-- same timestamp
value: 4000
timestamp: "2021-11-21T20:21:53.635Z" <-- same timestamp
value: 500
But because it is very unlikely to have the same timestamps (the items would have to be added at the exact same time), your query is just sorting by timestamps. What's your use case exactly? What do you want to view in your app?Timo
11/24/2021, 10:43 AMMaciek K
11/25/2021, 7:17 AM// for the sake of dealing with dates
import dayjs from 'dayjs'
// Don't sort them yet
const items = await.prisma.item.findMany();
function modifyScore(currentItem) {
// if is from today multiply by some modifier etc
if (dayjs(currentItem.timestamp).isToday()) {
return currentItem.score * 2
}
if (dayjs(currentItem.timestamp).isYesterday()) {
return currentItem.score * 1.6
}
const aWeekAgo = dayjs().subtract(7, days);
if (dayjs(currentItem.timestamp).isAfter(aWeekAgo)) {
return currentItem.score * 1.2
}
if (dayjs(currentItem.timestamp).isBefore(aWeekAgo)) {
return currentItem.score * 0.8
}
}
// loop trhough items and modify score
const itemsMapped = items.map(item => ({...item, score: modifyScore(item)}));
// now sort
const itemsSorted = itemsMapped.sort((a,b) => a.score - b.score);
I typed it out just to give you an idea, I didn't test it, there might be errors.
But that might not be scalable as you need to load all items at initial fetch.
So there probably would be no use for that infinite scrolling of yours.
Another veeery wild idea would be to run a node js cron job everyday https://jobscheduler.net/#/
On that job modify all items score everday by some number like -100
(either fixed number or you provide an algorithm for that number or a modifer).
You could make a new field in datamodel if you don't want to modify the original score, like:
weightedScore Int
The older the item the more subtractions it got from each day.
Then you could query them gradually with infinite scroll.
But that is a wild idea. Altought not that bad if you think about it 😂
Hope that helps, good luck. 🤕Timo
12/01/2021, 9:15 AM