oscklmO
Convex Community16mo ago
3 replies
oscklm

Designing a friend system with Convex

Hi, I’m building a friend system with Convex and am torn between two approaches:

1. Multiple indexes for better scalability.
2. Multiple entries in a single table for each friendship.

I’ve implemented lexicographical sorting for user1 and user2 when inserting friendships, but when querying friendships for a user, the following approach is limited by the compound index:
const friendshipsAsUser1 = await ctx.db
  .query('userFriend')
  .withIndex('by_userIds', (q) => q.eq('user1', userId))
  .collect();

const friendshipsAsUser2 = await ctx.db
  .query('userFriend')
  .withIndex('by_userIds', (q) => q.eq('user2', userId))
  .collect();


The problem is that compound indexes require the query order to match the order in which the fields were indexed (e.g., ['user1', 'user2']), which limits flexibility in querying.

I’ve come up with a solution where id just do multiple entries, 1 for each side of the friendship, but I’m concerned about scalability. Is there a better way to handle this without creating multiple entries for each friendship or excessive indexes?

I'm very curious how to tackle this, specifically if there is any way that goes great with Convex?
Was this page helpful?