Type-safe subquery in "extras" of findMany query

extras: (fields) => {
const subquery = db.select({ … }).from(…).where(eq(…, fields.id))

return {
subquery: subquery.getSQL().as<Awaited<typeof subquery>>('subquery'),
}
},
extras: (fields) => {
const subquery = db.select({ … }).from(…).where(eq(…, fields.id))

return {
subquery: subquery.getSQL().as<Awaited<typeof subquery>>('subquery'),
}
},
Is this the right solution or is there a better way? Specifically, the getSQL().as<Awaited<typeof subquery>>(…) part for getting a type-safe subquery. I'm expecting Drizzle to return an array of objects as the subquery property of each result (executing the subquery once per result).
1 Reply
aleclarson
aleclarsonOP4mo ago
In case you wonder why I'm not using with instead of extras, it's because I have a many-to-many relationship with an orderBy clause.

Did you find this page helpful?