yh
yh•3mo ago

Hi all. Convex newbie here 👋

Hi all. Convex newbie here 👋 My app has "accounts" and "users" where each user is associated with an account. If a user is created (not via an invitation) then a new account is created and the user is associated with it. How should i approach this? I thought about using the afterUserCreatedOrUpdated callback in convexAuth but the user is already tried to be created but fails because it doesnt have an account associated with it. I dont want to make the account field optional. I thought about using the createOrUpdateUser callback but i dont want to be in charge of all the user creation process.
1 Reply
erquhart
erquhart•3mo ago
You can't have co-dependent documents in Convex. If your account requires a userId and user requires an accountId, one has to be created before the other, so one of them has to be optional. I handle this sort of thing by using shared functions to access data. So instead of my Convex functions accessing the users table directly, you have a function for getting one user, or a list of users, etc, and use that. The function can enforce the type you actually want, eg., check for an account id and throw an error if there isn't one. It can then return a narrowed user type with a required accountId field.

Did you find this page helpful?