alfieqashwa
alfieqashwa7mo ago

A company has many users, but a user

A company has many users, but a user only has one company. (one2many) Here is my schema: import { defineSchema, defineTable } from "convex/server" import { Infer, v } from "convex/values" export const roleValidation = v.union( v.literal("DEWA"), v.literal("ADMIN"), v.literal("OWNER"), v.literal("MANAGER"), v.literal("CASHIER"), v.literal("USER"), ) export const companyValidation = v.object({ name: v.string(), slug: v.optional(v.string()), phone: v.string(), logo: v.optional(v.string()), isPublished: v.boolean(), location: v.string(), }) const userValidation = v.object({ clerkUser: v.any(), // this is UserJSON from @clerk/backend email: v.string(), role: roleValidation, firstName: v.union(v.string(), v.null()), lastName: v.union(v.string(), v.null()), imageUrl: v.string(), companyId: v.optional(v.id("companies")), }) export default defineSchema( { companies: defineTable(companyValidation), users: defineTable(userValidation) .index("by_clerk_id", ["clerkUser.id"]) .index("email", ["email"]) .index("companyId", ["companyId"]), }, )
10 Replies
erquhart
erquhart7mo ago
If you need all users returned with the company, what you're doing works (your earlier code seemed to indicate something like this). You can also index the users table on companyId and query the users for a company that way, and use pagination if the list is large.
alfieqashwa
alfieqashwaOP7mo ago
Yeah, that's what I want to. After collect() if I add dot ".", typescript infers an "include()" method, but I can't find in the documentation of "include()". I'm guessing if it's like as prisma syntax to include the company of each user. Isn't it? Or not?
erquhart
erquhart7mo ago
There's no include method, can you share a screenshot of that? If you wrap the awaited collect in parentheses, you get an array, and that would have an includes() method. Only thing I can think of. But you'll want to take a look at the docs to better understand Convex in a basic sense, api is different from Prisma.
alfieqashwa
alfieqashwaOP7mo ago
here
No description
alfieqashwa
alfieqashwaOP7mo ago
I'm gonna read one more time the documentation. Thank you for your response.
erquhart
erquhart7mo ago
that's really odd that it would suggest that, collect() returns a promise
v
v7mo ago
It would probably insert parentheses if he accepted the suggestion
alfieqashwa
alfieqashwaOP7mo ago
@v could you provide the example code using includes() ? or the documentation related to it.
v
v7mo ago
you would have to query all the items with collect. so everything would be loaded into memory you cant have an includes filter whats the end goal? or are you just learning if you wanna get all the users for a company you could have them as a field inside the schema as an array of user ids

Did you find this page helpful?