group by inside relations ?

i want use groupBy inside a relations
const tire = await ctx.db.query.tire.findFirst({
where: (tire, { eq }) => eq(tire.id, Number(input.id)),

with: {
model: {
with: {
brand: true,
},
},
scanUrls: {
where: (scanUrls, { eq }) =>
eq(scanUrls.disabled, false),
with: {
website: true,
scanUrlHistory: {
extras: {
day: sql`concat(day(${schema.scanUrlHistory.createdAt}),'/',month(${schema.scanUrlHistory.createdAt}),'/',year(${schema.scanUrlHistory.createdAt}))`.as(
"day"
),
avg_price:
sql`AVG(${schema.scanUrlHistory.price})`.as(
"avg_price"
),
},

where: and(
gt(
schema.scanUrlHistory.createdAt,
dayjs().subtract(7, "day").format()
),
isNull(schema.scanUrlHistory.error)
),
},
},
},
},
});

const tire = await ctx.db.query.tire.findFirst({
where: (tire, { eq }) => eq(tire.id, Number(input.id)),

with: {
model: {
with: {
brand: true,
},
},
scanUrls: {
where: (scanUrls, { eq }) =>
eq(scanUrls.disabled, false),
with: {
website: true,
scanUrlHistory: {
extras: {
day: sql`concat(day(${schema.scanUrlHistory.createdAt}),'/',month(${schema.scanUrlHistory.createdAt}),'/',year(${schema.scanUrlHistory.createdAt}))`.as(
"day"
),
avg_price:
sql`AVG(${schema.scanUrlHistory.price})`.as(
"avg_price"
),
},

where: and(
gt(
schema.scanUrlHistory.createdAt,
dayjs().subtract(7, "day").format()
),
isNull(schema.scanUrlHistory.error)
),
},
},
},
},
});

i want to group by scanUrlHistory on the field day from my extras fields
4 Replies
Angelelz
Angelelz13mo ago
You'll need to drop to the crud API for this. I don't think groupBy is supported in the relational query builder
Noext
NoextOP13mo ago
so i need to write my query by hand ?
Noext
NoextOP13mo ago
with this operator : https://orm.drizzle.team/docs/sql ?
Magical sql operator 🪄 - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Angelelz
Angelelz13mo ago
SQL Select - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Want results from more Discord servers?
Add your server