aggregation core queries in extras?

Saw this in the docs. How do I do a count() here? Does it require a separate query or can I still combine it into one request?

the core queries links to https://orm.drizzle.team/docs/select

Can I use this in the extras?

This is what I would like to add to the query:

const fineTunedModelsTotalThisMonthQuery = db
  .select({ count: count() })
  .from(fineTunedModels)
  .where(
    and(
      eq(fineTunedModels.userId, userId),
      sql`${fineTunedModels.createdAt} >= date_trunc('month', now()) AND ${fineTunedModels.createdAt} < date_trunc('month', now()) + interval '1 month')`,
    ),
  );
const fineTunedModelsTotalQuery = db
  .select({ fineTunedModelsTotal: count() })
  .from(fineTunedModels)
  .where(eq(fineTunedModels.userId, userId));

const dbUser = await db.query.users.findFirst({
  with: {
    fineTunedModels: {
      extras: {
        totalThisMonth: fineTunedModelsTotalThisMonthQuery,
        total: fineTunedModelsTotalQuery,
      }
    },      
  },
  where: (user, { eq }) => {
    return eq(user.id, userId);
  },
});
image.png
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Was this page helpful?