Can someone explain please
❔Advice
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:
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,
});
},
});