Skip to content

Aggregates definitions and their respective queries must be in the same file #25

@tomredman

Description

@tomredman

When I have my TableAggregate's defined in their own and attempt to use them in another file, I get an insurmountable typescript error: Type instantiation is excessively deep and possibly infinite.

This query results in the following error:

// convex/campaigns/queries.ts
export const getSentCountForCampaign = query({
  args: {
    campaignId: v.id("campaigns"),
  },
  handler: async (ctx, args) => {
    return await aggregateEmailEventsSent.count(ctx, {
      namespace: args.campaignId,
      bounds: {},
    });
  },
});

// Compilation error:
>  TypeScript typecheck via `tsc` failed.
> convex | To ignore failing typecheck, use `--typecheck=disable`.
> convex/campaigns/queries.ts:77:15 - error TS2589: Type instantiation is excessively deep and possibly infinite.
> convex |
> convex | 77       bounds: {},
> convex |                  ~~
> convex |
> convex | Found 1 error in convex/campaigns/queries.ts:77

I can't get this two-file structure to compile without the type instantiation error:

// convex/aggregates/campaigns.ts
import { v } from "convex/values";
import { components } from "../_generated/api";
import { DataModel, Id } from "../_generated/dataModel";
import { query } from "../_generated/server";
import { TableAggregate } from "@convex-dev/aggregate";

/*
 * Aggregates
 */
export const aggregateEmailEventsSent = new TableAggregate<{
  Namespace: Id<"campaigns">;
  Key: number;
  DataModel: DataModel;
  TableName: "emails";
}>(components.aggregateEmailEventsSent, {
  namespace: (doc) => doc.campaignId,
  sortKey: (doc) => doc._creationTime,
  sumValue: () => 1,
});
// convex/campaigns/queries.ts

export const getSentCountForCampaign = query({
  args: {
    campaignId: v.id("campaigns"),
  },
  handler: async (ctx, args) => {
    const forCampaign = {
      namespace: args.campaignId,
      bounds: {},
    };
    return await aggregateEmailEventsSent.count(ctx, forCampaign);
  },
});

However, if both of these are in the same file, it works:

// convex/aggregates/campaigns.ts
import { v } from "convex/values";
import { components } from "../_generated/api";
import { DataModel, Id } from "../_generated/dataModel";
import { query } from "../_generated/server";
import { TableAggregate } from "@convex-dev/aggregate";

/*
 * Aggregates
 */
export const aggregateEmailEventsSent = new TableAggregate<{
  Namespace: Id<"campaigns">;
  Key: number;
  DataModel: DataModel;
  TableName: "emails";
}>(components.aggregateEmailEventsSent, {
  namespace: (doc) => doc.campaignId,
  sortKey: (doc) => doc._creationTime,
  sumValue: () => 1,
});

export const getSentCountForCampaign = query({
  args: {
    campaignId: v.id("campaigns"),
  },
  handler: async (ctx, args) => {
    const forCampaign = {
      namespace: args.campaignId,
      bounds: {},
    };
    return await aggregateEmailEventsSent.count(ctx, forCampaign);
  },
});

Notably, this has been a little fickle. I got it to work in in the first way (two files) after deleting and regenerating the Convex code, in addition to changing this line (the error occurs no matter what when I try to inline the Bounds object):

This works in both cases:

const forCampaign = {
  namespace: args.campaignId,
  bounds: {},
};
return await aggregateEmailEventsSent.count(ctx, forCampaign);

This works only if defined in the same file as the aggregate:

return await aggregateEmailEventsSent.count(ctx, {
  namespace: args.campaignId,
  bounds: {},
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions