yarrichar
yarrichar2y ago

making an Id

Dumb question - what's the right way to create an Id<"whatever"> ?
5 Replies
lee
lee2y ago
the most common way is to call const id = await db.insert("whatever", {fields}); which would have type Id<"whatever">
yarrichar
yarricharOP2y ago
This is on the client side, with the id coming from the url. I'm wanting to pass that into a useQuery call So I have a string atm, but I want the id type
lee
lee2y ago
makes sense. in that case i would cast it mystr as Id<"whatever">. https://discord.com/channels/1019350475847499849/1122933700099833984/1130980984641310741
yarrichar
yarricharOP2y ago
Ok, cool, will give that a go thanks
sshader
sshader2y ago
Another option would be to keep the type as string and have your convex function call db.normalizeId to confirm it's actually an ID in the expected table and return the normalized ID in the query (since users could visit invalid URLs like my-app.com/fakeId).

Did you find this page helpful?