Types of function context and arguments
let's say I want to create a utility function that does a query inside a function like this:
what's the correct typing on
db
and id
?12 Replies
You can go to the definition of
query
to see the context type. You can either use QueryCtx
for the whole context type or DatabaseReader
just for the db.
IDs will be Id<"tableName">
exported from _generated/server
.
Feel free to post in the support channel with concrete questions, might be easier for other folks to find later.Oh yeah sorry
No worries! Keep the feedback coming, we love it.
This is a good question, and we should have better docs here (right now it only mentions the ID type): https://docs.convex.dev/typescript
TypeScript | Convex Developer Hub
Move faster with end-to-end type safety.
btw your
loadGame
function might be easier to write with db.get(id)
(drive by comment, feel free to ignore 😛 )oh yeah I had a legacy id field from my previous stack and then switched out to native convex ids yesterday and just went from
id
to _id
but yeah reason for this question is

like vscode just tries to vomit the whole schema into the tooltip and I'm like... but what do I write as function arg type?
Yeah, definitely not obvious!
import {DatabaseWriter} from './_generated/server'
My idea comes from client side though and currently comes in as
args.id : v.string()
Do I typecast that for db.get(id)
or how can I make the function validate game ids?
nvm rtfmi would type it as
v.id("games")
and do the cast client-side if necessary
that way the server can validate that the id is actually valid and in the "games" table (otherwise, if you cast server-side, a rogue client could use this to get any document)I just hand the raw document row to the client with _id type as is is that bad practice?
sounds fine to me 🙂