Jinho Kim
CCConvex Community
•Created by Jinho Kim on 4/28/2024 in #support-community
migration with convex-ents
Thanks for sharing this, Michal😍👍
4 replies
CCConvex Community
•Created by Jinho Kim on 3/28/2024 in #support-community
How can I use timeout in mutation?
This is the code.
export const _update_or_create_by_clerk_membership = internalMutation({
args: { clerk_membership: v.any() },
async handler(
ctx,
{
clerk_membership,
}: {
clerk_membership: OrganizationMembershipJSON;
}
) {
const existing_membership = await query_by_clerk_org_id(ctx, clerk_membership.id);
let entity_id: Id<'users'> | null = null;
let workspace_id: Id<'workspaces'> | null = null;
const pollForWorkspaceCreation = async (clerkOrgId: string) => {
const maxAttempts = 10;
let attempts = 0;
while (attempts < maxAttempts) {
const workspace = await query_workspace_by_clerk_id(ctx, clerkOrgId);
if (workspace) {
return workspace;
}
await new Promise((resolve) => setTimeout(resolve, 2000));
attempts++;
}
throw new Error('Workspace creation timed out');
};
if (existing_membership) {
entity_id = existing_membership.entity_id as Id<'users'>;
workspace_id = existing_membership.workspace_id;
} else {
const user = await query_user_by_clerk_id(ctx, clerk_membership.public_user_data.user_id);
if (!user) {
throw new Error('Unauthorized');
}
const workspace = await pollForWorkspaceCreation(clerk_membership.organization.id);
if (workspace === null) {
throw new Error('Workspace not found after retries');
}
entity_id = user._id;
workspace_id = workspace._id;
}
const membership_data: {...}
if (existing_membership === null) {
await ctx.db.insert('workspace_memberships', membership_data);
} else {
await ctx.db.patch(existing_membership._id, membership_data);
}
},
});
export const _update_or_create_by_clerk_membership = internalMutation({
args: { clerk_membership: v.any() },
async handler(
ctx,
{
clerk_membership,
}: {
clerk_membership: OrganizationMembershipJSON;
}
) {
const existing_membership = await query_by_clerk_org_id(ctx, clerk_membership.id);
let entity_id: Id<'users'> | null = null;
let workspace_id: Id<'workspaces'> | null = null;
const pollForWorkspaceCreation = async (clerkOrgId: string) => {
const maxAttempts = 10;
let attempts = 0;
while (attempts < maxAttempts) {
const workspace = await query_workspace_by_clerk_id(ctx, clerkOrgId);
if (workspace) {
return workspace;
}
await new Promise((resolve) => setTimeout(resolve, 2000));
attempts++;
}
throw new Error('Workspace creation timed out');
};
if (existing_membership) {
entity_id = existing_membership.entity_id as Id<'users'>;
workspace_id = existing_membership.workspace_id;
} else {
const user = await query_user_by_clerk_id(ctx, clerk_membership.public_user_data.user_id);
if (!user) {
throw new Error('Unauthorized');
}
const workspace = await pollForWorkspaceCreation(clerk_membership.organization.id);
if (workspace === null) {
throw new Error('Workspace not found after retries');
}
entity_id = user._id;
workspace_id = workspace._id;
}
const membership_data: {...}
if (existing_membership === null) {
await ctx.db.insert('workspace_memberships', membership_data);
} else {
await ctx.db.patch(existing_membership._id, membership_data);
}
},
});
9 replies