girayG
Convex Community10mo ago
1 reply
giray

Too many reads in a single function execution (limit: 4096)

I am getting below error for the first function that I am sharing below, I could not find the reason since all functions inside the function use indices. Can someone help me spot the problem?

Error:
Too many reads in a single function execution (limit: 4096). Consider using smaller limits in your queries, paginating your queries, or using indexed queries with a selective index range expressions.

export const createEmptySong = mutation({
  ...
  handler: async (ctx, args) => {
    const { ownerId, user } = await assertOperationAccessRightForSlug(ctx, args.ownerSlug, 'write');
    ...
    const songs = await ctx.db
      .query('songs')
      .withIndex('by_ownerId_order', (q) => q.eq('ownerId', ownerId))
      .take(50);

    ...

    const songWithMinOrder = await ctx.db
      .query('songs')
      .withIndex('by_ownerId_order', (q) => q.eq('ownerId', ownerId))
      .first();
    ...
  },
});


export const assertOperationAccessRightForSlug = async (
  ctx: MutationCtx | QueryCtx,
  ownerSlug: string,
  operation: 'read' | 'write' | 'manage',
) => {
  const user = await assertUser(ctx);

  let ownerId: string;
  let team: Doc<'teams'> | null = null;

  if (ownerSlug === 'personal') {
    ownerId = user._id;
  } else {
    team = await assertTeamBySlug(ctx, ownerSlug);
    await checkTeamAccess(ctx, team._id, user._id, operation);
    ownerId = team._id;
  }

  return { ownerId, user, team };
};

export const assertTeamBySlug = async (ctx: MutationCtx | QueryCtx, teamSlug: string) => {
  const team = await ctx.db
    .query('teams')
    .withIndex('slug', (q) => q.eq('slug', teamSlug))
    .unique();
  ...
};

export const checkTeamAccess = async (
  ctx: MutationCtx | QueryCtx,
  teamId: Id<'teams'>,
  userId: Id<'users'>,
  operation: 'read' | 'write' | 'manage',
) => {
  const teamMember = await ctx.db
    .query('teamMembers')
    .withIndex('teamId_userId', (q) => q.eq('teamId', teamId).eq('userId', userId))
    .unique();
  ...
};
Was this page helpful?