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);
},
});
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);
},
});
Drizzle ORM - Select
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
No description
1 Reply
Eddy Vinck
Eddy VinckOP2mo ago
I just realized.. it probably means I should do the whole query with the db.select syntax, right?
Want results from more Discord servers?
Add your server