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'),
  }
},


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).
Was this page helpful?