Find all the references to a Convex table
I need to change a table name in Convex. Using VS Code as my text editor, how do I find all the references to that table?
23 Replies
Corresponding Kapa thread: https://discord.com/channels/1019350475847499849/1260266752164630660
No working answer given
I think the closest answer is to search across the codebase for api.[tableName]
Table names are usually strings in the APIs that use them like
ctx.db.query("tableName")
Interesting that you say that, because that makes it sound like it's common to call ctx.db.query() throughout the codebase.
I had been primarily using exported variables for mutation() and query() in /convex/tableName.ts file. Maybe I have not been adhering to the spirit of Convex design
That seems nice!
I'm not sure I'm understanding right, are you exporing table names like
export const users = "users"
or are you exporting mutations and queries like export const getUsers = query(...)
? Either way, sounds fine.
I just meant that api.whatever
needn't be related to table names at all
that's the name of a mutation or query. If you want to rename a table, there's no need to modify these.I have been exporting mutations and queries like export const getUsers = query(...)
I started this habit because I mimicked the design I found in:
https://stack.convex.dev/gpt-streaming-with-persistent-reactivity
Stack
GPT Streaming With Persistent Reactivity
Stream GPT responses without brittle browser-based HTTP streaming.
Multiplayer reactivity, persistence, reactivity via Convex. Using OpenAI’s Node SDK...
By the way Tom, I liked your podcast episode with Backend Banter!
Nice, yeah that sounds good!
api.whatever may not necessarily be related, but if I were to export const getUsers = query(...) then it would be related
Ah and in this hypothetical you want to change users -> members?
Sure you can change these if you want, but a nice thing is that you can change your internal data representation (the tables) without changing the external API
ie just because it's called
getUsers
doesn't mean it needs to ctx.db.query("users")
a table called users, it can access whatever tables you wantI see, that decoupling is good to know.
Now that we're on the topic: do you recommend I export variables or use ctx.db extensively throughout the codebase?
I don't quite understand the difference
exporting variables like
export const users = "users";
?
because with that you'd be using ctx.db throughout the codebase anywayLike how Ian did in: https://stack.convex.dev/gpt-streaming-with-persistent-reactivity
Stack
GPT Streaming With Persistent Reactivity
Stream GPT responses without brittle browser-based HTTP streaming.
Multiplayer reactivity, persistence, reactivity via Convex. Using OpenAI’s Node SDK...
No, exporting variables for query or mutation function
Could you point to some code? I don't understand how the pattern he's using is difference
I see the normal pattern of exporting queries and mutations in https://github.com/ianmacartney/streaming-chat-gpt/blob/main/convex/messages.ts#L16
Okay, that's the answer, that is what normal is
So, I need to rename the table from messages to something else
yeah normal in Convex is creating mutations and queries, and in these mutations and queries you can write whatever
ctx.db.query("whateverTableYouWant")
Okay, then a global search for ctx.db.query("messages") is only going to return results in messages.ts, correct?
there's no correlation between the names of these files or the names of these exported mutations and queries with teh names of the tables
No,
ctx.db.query('message')
could happen anywhere in your convex directoryAh sure, but the spirit of Convex design is: Don't use ctx.db.query("messages") extensively throughout the page.tsx in your Next.js codebase. Instead, use the exported variable for the query or mutation in the /convex directory. Is my understanding correct?
Ah yeah that's not allowed, it's not possible to write
ctx.db.query('message')
outside of a Convex mutation or query.
Yeah, the idea is to define query and mutations functions (AKA "stored procedures") which you call remotely. These can be called whatever you want. Inside these query and mutation functions (or in other helper functions that these queries and mutations call) you can use ctx.db.query('messages')
to read from the database.Okay, so I think my original thought was correct. Search globally for api.messages to do a table name change throughout the codebase

Ah ok, so you're not changing the name of a table here. You're changing the name of a file in your convex directory.
You might also be changing the name of a table, but that's a separate thing
You can totally do both, I see, that makes sense
oh, that's clarifying. Yes I do want to change the filename too
Thanks Tom!
It's interesting that that query() a Convex developer defines is, AKA, a stored procedure under the hood. I've been listening to these Convex podcasts because I'm still trying to wrap my head around the meaning of running code in the Convex database