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!
2
Answers
After I saw this discussion, I managed to write a generic function for this
In Drizzle,
db.query
serves as a full phase ORM, whiledb.select
provides a lightweight query builder. Although you may encounter some issues, it operates much faster.Here’s the corrected version of your query:
Note that
db.select
does not support multiple nested layers; it only allows for one nested layer.