@lilac elm I have this query function, that will require processing thousands of documents. My current implementation produces this error:
4.3s
Uncaught Error: Uncaught Error: Too many bytes read in a single function execution (limit: 8388608 bytes). Consider using smaller limits in your queries, paginating your queries, or using indexed queries with a selective index range expressions.
the code:
export const adminRepopulateInsightsCountTrackerDetails = mutation({
args: {
userId: v.id("users"),
},
handler: async (ctx,args) => {
const { userId } = args;
const userDoc = await ctx.db.get(userId);
const relationshipId = userDoc?.relationshipIds?.[0];
// Get the existing insightsCountTracker
const insightsCountTracker = await ctx.db
.query("insightsCountTracker")
// Process each event type using pagination
const eventTypes = ["moment","conflict","need"] as const;
const PAGE_SIZE = 1; // Adjust based on your needs
for (const eventType of eventTypes) {
let hasMore = true;
let cursor = null;
while (hasMore) {
//@ts-ignore
const paginatedResults = await ctx.runQuery(internal.insightsCountTracker.getPaginatedEvents,{
userId,
eventType,
paginationOpts: {
numItems: PAGE_SIZE,
cursor,
},
});
// Process each event in the current page
for (const event of paginatedResults.page) {
await updateInsightsCountTrackerDetails({
perspective: args.perspective,
ctx,
userDoc,
relationshipId,
eventType: eventType === "moment" ? "everyday" : eventType,
eventData: event,
});
}
}
return 'success'
// Relationship perspective not supported yet
return 'error'
},
});