4 ms·
If you were willing to give up a bit of magic, you could probably build this as a thin layer over PgTyped. The API could be something like this: Query.sql
by bijection 6y ago
If you were willing to give up a bit of magic, you could probably build this as a thin layer over PgTyped.
The API could be something like this:
Query.sql
SELECT * FROM user LEFT JOIN post ON user.id = post.id
Application.ts
const results = await Query()
const nested = nest(results, {
parentFields: ['userId', 'name'],
childFields: ['postId', 'title'],
childName: 'posts'
)
If you wanted, you wouldn't really have to specify child fields, since they'd just whatever wasn't a parent field. It'd take a bit more work to get it to do multiple levels of nesting, but after a point it doesn't make sense to write queries that return so much duplicate data anyway.
- kevsim 6y ago> but after a point it doesn't make sense to write queries that return so much duplicate data anyway This is what I'm constantly wondering. At what point does it stop being good to return the user table results again and again and just switch to, for example, an IN query to get the posts?