7 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
When paginating the fame leaderboard, I get duplicate entry errors, is this design incorrect?
i'm not sure if the
doc.to_user_id
is forced unique here or if there can be a duplicate there that can lead to non unique sort keys
but here's something to try to make it truly unique.
const fameAggregate = new TableAggregate<{
Key: [number, string]; // [to_user_id, fame_id]
DataModel: DataModel
TableName: 'fame'
}>(components.fameAggregate, {
sortKey: (doc) => [doc.to_user_id, doc._id],
sumValue: (doc) => (doc.is_positive ? 1 : -1)
})
that might solve the problem. also make sure to always use the cursor as it is returned.hm there might be multiple inserts of the same
doc.to_user_id
. Maybe i designed it wrongly for convex
my fames feature is like a "likes" feature for YouTube videos
but in my case, the fame is the amount of likes a user has
everytime, a user clicks "fame up", then the is_positive
field is true
. And so there are multiple records of fame ups and down per to_user_id
as multiple users can give them an up or down fame@ulysses ok i understand, i see you are using upper and lower bounds to get the user calculation. that'll give you duplicates as user id is stored multiple times when you insert. try the
.sum
with prefix to calculate totals.
something like this:
export async function getFameTotal(
ctx: QueryCtx,
userId: number
): Promise<number> {
// Use a
prefix query to sum all fame events for a given user
return await fameAggregate.sum(ctx, {
prefix: [userId]
});
}
hmm i get a type error for that:
Object with prefix has to be assigned to
bounds
property: