Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tomredman opened this issue Jan 15, 2025 · 1 comment

Comments

@tomredman
Copy link

tomredman commented Jan 15, 2025

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: {},
});
@ianmacartney
Copy link
Contributor

So the campaigns doesn't import from the queries otherwise? It sounds like a circular reference somewhere. To prove that out, does having the aggregate in its own file with nothing else fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants