Short IDs for Rooms table
Hi there, I'm migrating off of Postgres to Convex.
In my application, I have a rooms table that contains a kind of "Code" you can use to join the room. It has 4 alphanumeric digits (just uppercase letters and numbers), so that's 36^4 combinations.
Previously I used the sequential room id and generated a short_id using pg_hashids, but as it seems there is no such thing in Convex, because every document id is globally (to the project) unique.
How should I go about generating a "random" 4-digit string that has to be unique in the rooms table?
PS: If you happen to know Jackbox, these kinds of room codes are exactly what I'm going for.
2 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!
I'd suggest you use something like this: https://github.com/simplyhexagonal/short-unique-id (there are many similar alternatives).
You wouldn't put this ID in the _id field but in a separate field (eg
room_id
) with an index on it. The convex way for doing unique index is to run a query on that index to make sure there's no row there before you you write your row. OCC will ensure you don't have a race condition where duplicates are inserted. See more here: https://docs.convex.dev/database/advanced/occGitHub
GitHub - simplyhexagonal/short-unique-id: Short Unique ID (UUID) ge...
Short Unique ID (UUID) generation library. Available in NPM. - simplyhexagonal/short-unique-id
OCC and Atomicity | Convex Developer Hub
Optimistic concurrency control and transaction atomicity in Convex