Patolord
Patolord3w ago

I have to save a record on my table that

I have to save a record on my table that is the following structure, Group>SubGroup>Item. i have just a few categories (10s), subcategories(100s) and tags (1000s). Do i create a table for each, or denormalize, i have no idea to be honest , whats more eficient in convex?
1 Reply
Hmza
Hmza3w ago
with Convex, this decision depends on your query patterns. In your case with a few categories, sub-, and tags, i'd do it like this: save categoryId, sub-, and tagIds in the posts table as you are doing already, and put indexes on these for efficient queries. e.g.
export const posts = defineTable({
...
categoryId: v.id("categories"),
...others
})
.index("by_categoryId", ["categoryId"]);
export const posts = defineTable({
...
categoryId: v.id("categories"),
...others
})
.index("by_categoryId", ["categoryId"]);
Then just use:
const posts = await ctx.db
.query("posts")
.withIndex("by_categoryId", (q) =>
q.eq("categoryId", yourCategoryId)
)
.collect();
const posts = await ctx.db
.query("posts")
.withIndex("by_categoryId", (q) =>
q.eq("categoryId", yourCategoryId)
)
.collect();

Did you find this page helpful?