Mathias
Mathias7mo ago

Queries and best practices: How to query multiple tables and return object?

I have a few queries, that seems very large in terms of lines of code, and I was wondering if there is a better way to do this. This example is a get query to get multiple tables to then end up returning one object with a lot of information: See the code example here as Discord doesn't allow longer messages: https://stackoverflow.com/questions/78727640/queries-and-best-practices-how-to-query-multiple-tables-and-return-object-using An example of my flow table:
flows: defineTable({
title: v.string(),
content: v.optional(v.string()),
parentId: v.optional(v.string()),
teamId: v.string(),
pipelineId: v.string(),
statusId: v.optional(v.string()),
priorityId: v.optional(v.string()),
typeId: v.optional(v.string()),
})
.index("by_team", ["teamId"])
.index("by_parent", ["parentId"])
.index("by_pipeline", ["pipelineId"])
.index("by_status", ["statusId"])
.index("by_type", ["typeId"]),
flows: defineTable({
title: v.string(),
content: v.optional(v.string()),
parentId: v.optional(v.string()),
teamId: v.string(),
pipelineId: v.string(),
statusId: v.optional(v.string()),
priorityId: v.optional(v.string()),
typeId: v.optional(v.string()),
})
.index("by_team", ["teamId"])
.index("by_parent", ["parentId"])
.index("by_pipeline", ["pipelineId"])
.index("by_status", ["statusId"])
.index("by_type", ["typeId"]),
So, I essentially want to get the status, priority and type of the flow returned in the same object. I heard about the query convex-helpers/react but I'm not sure if that can be of any help. I was also wondering of abstracting some of the logic to other files and then importing them where I'm going to use it, is a good idea?
Stack Overflow
Queries and best practices: How to query multiple tables and return...
I have a few queries, that seems very large in terms of lines of code, and I was wondering if there is a better way to do this. This example is a get query from multiple tables to then end up retur...
5 Replies
ballingt
ballingt7mo ago
Hi @Mathias, I wrote an answer on StackOverflow; does this answer your question? The gist is "write helper functions, you don't have to write this all in one function."
ballingt
ballingt7mo ago
Stack Overflow
Queries and best practices: How to query multiple tables and return...
I have a few queries, that seems very large in terms of lines of code, and I was wondering if there is a better way to do this. This example is a get query from multiple tables to then end up retur...
ballingt
ballingt7mo ago
It sounds like you already know that you can do this:
I was also wondering of abstracting some of the logic to other files and then importing them where I'm going to use it, is a good idea?
Yes I think this is a good idea! But are you trying to get at something else here, not just that this is a lot of lines of code but that you'd like to do something else differently here?
Mathias
MathiasOP7mo ago
Hi @ballingt Thank you for putting so much time into my questions. It definitely helped getting me back on track. Much appreciated! I will try to write smaller helper function to sort my code more nicely, and perhaps it provides a better overview along the way. @ballingt I see that you are mentioning QueryCtx. Where are those coming from? I noticed when I'm creating helper functions, that my types no longer follow along.
ballingt
ballingt7mo ago
QueryCtx is imported from "./_generated/server"

Did you find this page helpful?