Setting `_id` when creating documents with optimistic update
We absolutely love optimistic updates and use them heavily on our app. There's however some places where we can't use them because we would require Convex to create a document with an id that we pass it which is equal to the id of the document created optimistically.
In our app, when you create a page, a document gets created and we navigate the user to
pages/<doc_id>
Doing this optimistically would break atm...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!
we have similar issues when dragging a block into a grid, where users have to wait until the document is actually stored in convex and the grid to rebuilt to be able to edit (e.g. resize) the dropped block (otherwise they'd be editing a document with an id that doesn't exist in convex and is just an id we generated in the optimistic update). Given that we have a pretty complicated schema which leads to longer validation times this issue is very easy to replicate.
We'd love if Convex supported this, similar to how Firestore lets you create a document reference/id before actually writing the document
I ran into something similar yesterday. I insert a document with a mutation. But then I need to query that same document in order to have a reactive subscription
Optimistic is not meant to be used everywhere. But if you insist on doing that, you can create another type such as _myId and pages/<myId> instead, nothing is stopping you 🙂
The issue of editing a doc that still hasn’t been stored in convex persists though
Yeah I'd love client side ID generation too so you can do more powerful optimistic updates like this -- we just haven't had the time to build it yet.
The current workaround (which admittedly isn't great) is the suggestion above of creating your own
id
field where you can control the ID generationthanks @sshader ! Is this something on your roadmap? just to be able to decide on how much effort is worth investing from our side on a workaround