drizzle-orm
drizzle-orm copied to clipboard
Feat: Allow subqueries in select fields
close #361
- [All] Implemented the types inference for subqueries in SelectFields
- [All] Implemented type tests
- [All] Implemented correct handling of subqueries in selected fields
- [All] Added integration tests
With this PR, a user will be able to select using subqueries. The following query will be possible with proper types and mapping:
const res = await db.select({
population: db.select({ count: count().as('count') }).from(users).where(
eq(users.cityId, cities.id),
).as('population'),
name: cities.name,
}).from(cities);
// And the resulting type will be:
type Res = {
population: number;
name: string;
}[]
Since the subquery can only contain one column, I moved the mapping from the column to the whole subquery.
That means that if the field in the subquery is a Date the user can expect a date in the types and the runtime value.
This is one of the few frictions I feel writing drizzle.
Would love to see this merged 😍
I'm hitting this a lot, would love this to get live! anything i can do to help?
any chance this could be resurrected and shipped? this is definitely a source of friction day to day. if not, is there a workaround to get the types working with some kind of custom helper function?
bump, @erikmunson I just spent 40 minutes trying to figure out why I couldn't use this subquery to find out this PR isn't merged