© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•15mo ago
BereketeAb

Mapping Query Builder Results to Nested Objects in Drizzle

I'm transitioning to Drizzle ORM from a NestJS-TypeORM background :

TypeORM: Both the repository and query builder return similar structured results, making it easy to work with joined data.
Drizzle:
db.query() returns nicely structured nested data (e.g., users and their posts grouped together).
Query Builder (db.select()) returns raw, flattened data requiring additional processing to achieve a similar structure.

Example:
db.query result:

[{
"id": 10,
"name": "Dan",
"posts": [
{
"id": 1,
"content": "SQL is awesome",
"authorId": 10
},
{
"id": 2,
"content": "But check relational queries",
"authorId": 10
}
]
}]

Query Builder result:

const result: {
user: { id: number; name: string };
pets: { id: number; name: string; ownerId: number } | null;
}[] = await db
.select()
.from(users)
.leftJoin(pets, eq(users.id, pets.ownerId));

I'm looking for a mechanism or best practice to map Query Builder results into a nested format (similar to db.query) so that my API responses are consistent and clients don’t need to check different formats.

Any advice, patterns, or libraries you'd recommend for this? How do you handle this in your Drizzle-based projects?

Thanks in advance!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to raise nested fields in relational query results?
Drizzle TeamDTDrizzle Team / help
3y ago
How to use nested joins with relational query builder
Drizzle TeamDTDrizzle Team / help
2y ago
Guaranteed undefined for Nested Results? Type Mismatch in Query API
Drizzle TeamDTDrizzle Team / help
2y ago
Need help transform a nested prisma query to drizzle
Drizzle TeamDTDrizzle Team / help
3y ago