mikeyseeM
Convex Community3y ago
6 replies
mikeysee

How would you structure this schema?

I have the following schema:

export default defineSchema({
  users: defineTable({
    name: v.string(),
    pictureUrl: v.union(v.string(), v.null()),
    tokenIdentifier: v.string(),
  }).index("by_token", ["tokenIdentifier"]),

  canvases: defineTable({
    svgDocument: v.string(),
    members: v.array(
      v.object({
        userId: v.id("users"),
        role: v.union(v.literal("owner"), v.literal("editor")),
      }),
    ),
  }).index("by_authorUserId", ["authorUserId"]), // want to be able to get canvases by authorUserId but I cant do that with this index
});


Note I want to be able to list all the canvases that a user is a member of but I cant.

Im guessing I have to reverse this structure and have a "canvasMembers" table that records the userId and canvasId and then index and query that first then join to get the canvases?

Just checking incase there is a better way.
Was this page helpful?