gegi
gegi5d ago

Can someone explain please

I need a break down of this convex function. Somehow in the dashboard it says, it access 5 documents but I can't really understand where the fifith is coming from. Here is the function with comments, where different documents are getting perhaps accessed:
export const updateProgress = mutation({
args: {
episodeId: v.id("episodes"),
courseId: v.id("courses"),
progress: v.number(),
completed: v.boolean(),
},
returns: v.id("episodeProgress"),
handler: async (ctx, { episodeId, courseId, progress, completed }) => {
const identity = await authComponent.getAuthUser(ctx); // 1. access
if (!identity) {
throw new ConvexError({
message: "Not authenticated",
code: 401,
});
}

await rateLimiter.limit(ctx, "episodeProgress", { // 2. access
key: identity._id,
throws: true,
});

const userId = identity._id;
const updatedAt = Date.now();

const existingProgress = await ctx.db
.query("episodeProgress")
.withIndex("by_userId_episodeId", (q) =>
q.eq("userId", userId).eq("episodeId", episodeId),
)
.first(); // 3. access

if (existingProgress) {
await ctx.db.patch(existingProgress._id, { // 4. access conditionally
progress,
updatedAt,
});

return existingProgress._id;
}

return await ctx.db.insert("episodeProgress", { // 4. access conditionally
userId,
episodeId,
courseId,
progress,
});
},
});
export const updateProgress = mutation({
args: {
episodeId: v.id("episodes"),
courseId: v.id("courses"),
progress: v.number(),
completed: v.boolean(),
},
returns: v.id("episodeProgress"),
handler: async (ctx, { episodeId, courseId, progress, completed }) => {
const identity = await authComponent.getAuthUser(ctx); // 1. access
if (!identity) {
throw new ConvexError({
message: "Not authenticated",
code: 401,
});
}

await rateLimiter.limit(ctx, "episodeProgress", { // 2. access
key: identity._id,
throws: true,
});

const userId = identity._id;
const updatedAt = Date.now();

const existingProgress = await ctx.db
.query("episodeProgress")
.withIndex("by_userId_episodeId", (q) =>
q.eq("userId", userId).eq("episodeId", episodeId),
)
.first(); // 3. access

if (existingProgress) {
await ctx.db.patch(existingProgress._id, { // 4. access conditionally
progress,
updatedAt,
});

return existingProgress._id;
}

return await ctx.db.insert("episodeProgress", { // 4. access conditionally
userId,
episodeId,
courseId,
progress,
});
},
});
2 Replies
Convex Bot
Convex Bot5d ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
gegi
gegiOP5d ago
No description

Did you find this page helpful?