Adicco
Adicco
Explore posts from servers
DTDrizzle Team
Created by Adicco on 1/2/2024 in #help
Aggregation - limitations and mapping results
Using the aggregation method proposed in the docs (https://orm.drizzle.team/docs/joins#aggregating-results) is fairly limited as it makes for instance the limiting of results quite obsolete: using the documentation's example of user and pets, if we wished to limit() the results for 10 users, but one user has 10 pets, we would only get that one user with their 10 pets, rather than the desired 10 users with however many pets they might have (unless we slice the resulting array, but this seems like a huge waste of resources in large datasets) One solution of course in a raw PG query is to do some kind of aggregation in the query, for instance:
const rows = db.select({
user: users,
pets: sql<SnakeCasePet[]>`json_agg(row_to_json(${pets}))`,
}).from(users).leftJoin(pets, eq(users.id, pets.ownerId)).limit(n);
const rows = db.select({
user: users,
pets: sql<SnakeCasePet[]>`json_agg(row_to_json(${pets}))`,
}).from(users).leftJoin(pets, eq(users.id, pets.ownerId)).limit(n);
What happens then is that the resulting pets, although being the aggregation I want, is of course not processed by Drizzle's column mapper. So I typically get snake case objects rather than my camelCase Drizzle mappings. Is there a way to somehow use Drizzle's internal API to parse aggregated JSON results and get the corresponding type inference etc? Or, alternatively, has anyone found another way of dealing with this? Thank you!
5 replies
EDSEthan's dev spot
Created by Adicco on 9/30/2023 in #questions
Has anyone managed to add auto refresh (hmr) on this stack?
I guess hot module reloading doesn't really make sense in this stack per se, but just a way to refresh the browser automatically when the server restarts after a change.
6 replies