Zaimon
Zaimon12mo ago

Numbered tickets sale system

Hello! I'm working on a numbered ticket sales system where buyers are randomly assigned tickets after making a payment. Here's a breakdown of my current approach and the challenges I'm facing: Current Approach: Payment Validation: -An httpAction receives payment webhooks from the payment company. An Action validates the payment on your server. Initial Data Creation: -The database table is pre-populated with random numbers (0000-9999) and empty paymentID fields. Ticket Assignment: An internalMutation is triggered after successful payment validation.It receives buyer data and the payment ID. This mutation iterates through the desired number of tickets: It queries the database for an entry with an empty paymentID. If no entry is found, it returns an error indicating unavailability. Otherwise, it updates the found entry with the buyer's information and sets paymentID. InternalMutation code: the first problem I am facing is with more than 100 tickets I receive this error in the console: Uncaught Error: Uncaught Error: Too many documents read in a single function execution (limit: 16384) This is just an idea and I am learning so any help will be appreciated
No description
6 Replies
erquhart
erquhart12mo ago
You’re querying without an index, which means you’re doing a full table scan every time you read. You’ll want to start using indexes to avoid the error message you’re seeing. Here’s the docs for indexes: https://docs.convex.dev/database/indexes/
Indexes | Convex Developer Hub
Indexes are a data structure that allow you to speed up your
Zaimon
ZaimonOP12mo ago
Thank you so much! I wrongly assumed that take(1) will only read one row. Now, do you think this is a good approach I mean, is there any way to be sure an entry wont be overwrite after the query is executed, maybe between the query and the patch another user buys an entry and the db return that registry too?
erquhart
erquhart12mo ago
Convex mutations are transactions, so one mutation will never overwrite another. If there's a conflict between two concurrent mutations, Convex will settle it out. In addition to using an index, you'll want to use unique() instead of take() to assert that more than one entry should never be returned for a given mercadopagoId. But also note that your current approach doesn't assign a random ticket - is randomness actually important, or just ensuring no ticket number is assigned twice? Like, if the tickets were assigned in numeric order, would that be a problem?
Zaimon
ZaimonOP12mo ago
In order to make it random I pre-populate the db with the numbers in a random order. I am not using the number field in the index so it won't sort the number in the query. do you have any suggestion for the randomness?
erquhart
erquhart12mo ago
Ah, populating in random order does the trick 👍
Zaimon
ZaimonOP12mo ago
Thank you so much for your help!

Did you find this page helpful?